blob: b854cafb6f746b68d8d01f254f2f07817dd93f48 [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
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)fa76eee2016-05-28 21:03:48 +000035#include <stdio.h> /* needed for ctermid() */
36
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037#ifdef __cplusplus
38extern "C" {
39#endif
40
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000042"This module provides access to operating system functionality that is\n\
43standardized by the C Standard and the POSIX standard (a thinly\n\
44disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000047
Ross Lagerwall4d076da2011-03-18 06:56:53 +020048#ifdef HAVE_SYS_UIO_H
49#include <sys/uio.h>
50#endif
51
Thomas Wouters0e3f5912006-08-11 14:57:12 +000052#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000053#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000054#endif /* HAVE_SYS_TYPES_H */
55
56#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000057#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000058#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000059
Guido van Rossum36bc6801995-06-14 22:54:23 +000060#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000061#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000062#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000063
Thomas Wouters0e3f5912006-08-11 14:57:12 +000064#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000065#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000067
Guido van Rossumb6775db1994-08-01 11:34:53 +000068#ifdef HAVE_FCNTL_H
69#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000070#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000071
Guido van Rossuma6535fd2001-10-18 19:44:10 +000072#ifdef HAVE_GRP_H
73#include <grp.h>
74#endif
75
Barry Warsaw5676bd12003-01-07 20:57:09 +000076#ifdef HAVE_SYSEXITS_H
77#include <sysexits.h>
78#endif /* HAVE_SYSEXITS_H */
79
Anthony Baxter8a560de2004-10-13 15:30:56 +000080#ifdef HAVE_SYS_LOADAVG_H
81#include <sys/loadavg.h>
82#endif
83
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000084#ifdef HAVE_LANGINFO_H
85#include <langinfo.h>
86#endif
87
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000088#ifdef HAVE_SYS_SENDFILE_H
89#include <sys/sendfile.h>
90#endif
91
Benjamin Peterson94b580d2011-08-02 17:30:04 -050092#ifdef HAVE_SCHED_H
93#include <sched.h>
94#endif
95
Benjamin Peterson2dbda072012-03-16 10:12:55 -050096#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
Benjamin Peterson7b51b8d2012-03-14 22:28:25 -050097#undef HAVE_SCHED_SETAFFINITY
98#endif
99
doko@ubuntu.com4a173bc2014-04-17 19:47:16 +0200100#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__)
Benjamin Peterson9428d532011-09-14 11:45:52 -0400101#define USE_XATTRS
102#endif
103
104#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400105#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400106#endif
107
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000108#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
109#ifdef HAVE_SYS_SOCKET_H
110#include <sys/socket.h>
111#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000112#endif
113
Victor Stinner8b905bd2011-10-25 13:34:04 +0200114#ifdef HAVE_DLFCN_H
115#include <dlfcn.h>
116#endif
117
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200118#ifdef __hpux
119#include <sys/mpctl.h>
120#endif
121
122#if defined(__DragonFly__) || \
123 defined(__OpenBSD__) || \
124 defined(__FreeBSD__) || \
125 defined(__NetBSD__) || \
126 defined(__APPLE__)
127#include <sys/sysctl.h>
128#endif
129
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100130#if defined(MS_WINDOWS)
131# define TERMSIZE_USE_CONIO
132#elif defined(HAVE_SYS_IOCTL_H)
133# include <sys/ioctl.h>
134# if defined(HAVE_TERMIOS_H)
135# include <termios.h>
136# endif
137# if defined(TIOCGWINSZ)
138# define TERMSIZE_USE_IOCTL
139# endif
140#endif /* MS_WINDOWS */
141
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000142/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000143/* XXX Gosh I wish these were all moved into pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000144#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000145#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000146#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000147#include <process.h>
148#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000149#ifdef _MSC_VER /* Microsoft compiler */
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000150#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000151#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000152#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000153#define HAVE_EXECV 1
154#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000155#define HAVE_SYSTEM 1
156#define HAVE_CWAIT 1
157#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000158#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000159#else
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000160/* Unix functions that the configure script doesn't check for */
161#define HAVE_EXECV 1
162#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000163#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000164#define HAVE_FORK1 1
165#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000166#define HAVE_GETEGID 1
167#define HAVE_GETEUID 1
168#define HAVE_GETGID 1
169#define HAVE_GETPPID 1
170#define HAVE_GETUID 1
171#define HAVE_KILL 1
172#define HAVE_OPENDIR 1
173#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000174#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000175#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000176#define HAVE_TTYNAME 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000177#endif /* _MSC_VER */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000178#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000179
Victor Stinnera2f7c002012-02-08 03:36:25 +0100180
Larry Hastings61272b72014-01-07 12:41:53 -0800181/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +1000182# one of the few times we lie about this name!
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800183module os
Larry Hastings61272b72014-01-07 12:41:53 -0800184[clinic start generated code]*/
Larry Hastings2f936352014-08-05 14:04:04 +1000185/*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/
Victor Stinnera2f7c002012-02-08 03:36:25 +0100186
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000187#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000188
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000189#if defined(__sgi)&&_COMPILER_VERSION>=700
190/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
191 (default) */
192extern char *ctermid_r(char *);
193#endif
194
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000195#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000196#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000197extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000198#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000199#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000200extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000201#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000202extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000203#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000204#endif
205#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000206extern int chdir(char *);
207extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000208#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000209extern int chdir(const char *);
210extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000211#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000212extern int chmod(const char *, mode_t);
Christian Heimes4e30a842007-11-30 22:12:06 +0000213/*#ifdef HAVE_FCHMOD
214extern int fchmod(int, mode_t);
215#endif*/
216/*#ifdef HAVE_LCHMOD
217extern int lchmod(const char *, mode_t);
218#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000219extern int chown(const char *, uid_t, gid_t);
220extern char *getcwd(char *, int);
221extern char *strerror(int);
222extern int link(const char *, const char *);
223extern int rename(const char *, const char *);
224extern int stat(const char *, struct stat *);
225extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000227extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000228#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000229#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000230extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000231#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000233
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000234#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235
Guido van Rossumb6775db1994-08-01 11:34:53 +0000236#ifdef HAVE_UTIME_H
237#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000238#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000240#ifdef HAVE_SYS_UTIME_H
241#include <sys/utime.h>
242#define HAVE_UTIME_H /* pretend we do for the rest of this file */
243#endif /* HAVE_SYS_UTIME_H */
244
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245#ifdef HAVE_SYS_TIMES_H
246#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000247#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248
249#ifdef HAVE_SYS_PARAM_H
250#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000251#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252
253#ifdef HAVE_SYS_UTSNAME_H
254#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000255#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000256
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000257#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000258#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000259#define NAMLEN(dirent) strlen((dirent)->d_name)
260#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000261#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000262#include <direct.h>
263#define NAMLEN(dirent) strlen((dirent)->d_name)
264#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000265#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000266#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000267#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000268#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000269#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000270#endif
271#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000273#endif
274#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000275#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000276#endif
277#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000278
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000279#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000280#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000281#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000282#endif
283#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000284#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000285#endif
286#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000287#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000288#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000289#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000290#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000291#endif
292#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000293#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000294#endif
295#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000296#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000297#endif
Tim Golden0321cf22014-05-05 19:46:17 +0100298#ifndef IO_REPARSE_TAG_MOUNT_POINT
299#define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L)
300#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000301#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000302#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000303#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000304#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000305#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000306#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
307#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000308static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000309#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000310#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000311
Tim Petersbc2e10e2002-03-03 23:17:02 +0000312#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000313#if defined(PATH_MAX) && PATH_MAX > 1024
314#define MAXPATHLEN PATH_MAX
315#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000316#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000317#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000318#endif /* MAXPATHLEN */
319
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000320#ifdef UNION_WAIT
321/* Emulate some macros on systems that have a union instead of macros */
322
323#ifndef WIFEXITED
324#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
325#endif
326
327#ifndef WEXITSTATUS
328#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
329#endif
330
331#ifndef WTERMSIG
332#define WTERMSIG(u_wait) ((u_wait).w_termsig)
333#endif
334
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000335#define WAIT_TYPE union wait
336#define WAIT_STATUS_INT(s) (s.w_status)
337
338#else /* !UNION_WAIT */
339#define WAIT_TYPE int
340#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000341#endif /* UNION_WAIT */
342
Greg Wardb48bc172000-03-01 21:51:56 +0000343/* Don't use the "_r" form if we don't need it (also, won't have a
344 prototype for it, at least on Solaris -- maybe others as well?). */
345#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
346#define USE_CTERMID_R
347#endif
348
Fred Drake699f3522000-06-29 21:12:41 +0000349/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000350#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000351#undef FSTAT
352#undef STRUCT_STAT
Victor Stinner14b9b112013-06-25 00:37:25 +0200353#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000354# define STAT win32_stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700355# define LSTAT win32_lstat
Victor Stinnere134a7f2015-03-30 10:09:31 +0200356# define FSTAT _Py_fstat_noraise
Steve Dowerf2f373f2015-02-21 08:44:05 -0800357# define STRUCT_STAT struct _Py_stat_struct
Fred Drake699f3522000-06-29 21:12:41 +0000358#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000359# define STAT stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700360# define LSTAT lstat
Victor Stinner8c62be82010-05-06 00:08:46 +0000361# define FSTAT fstat
362# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000363#endif
364
Tim Peters11b23062003-04-23 02:39:17 +0000365#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000366#include <sys/mkdev.h>
367#else
368#if defined(MAJOR_IN_SYSMACROS)
369#include <sys/sysmacros.h>
370#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000371#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
372#include <sys/mkdev.h>
373#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000374#endif
Fred Drake699f3522000-06-29 21:12:41 +0000375
Victor Stinner6edddfa2013-11-24 19:22:57 +0100376#define DWORD_MAX 4294967295U
377
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200378#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +0100379#define INITFUNC PyInit_nt
380#define MODNAME "nt"
381#else
382#define INITFUNC PyInit_posix
383#define MODNAME "posix"
384#endif
385
386#ifdef MS_WINDOWS
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200387/* defined in fileutils.c */
388PyAPI_FUNC(void) _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
389PyAPI_FUNC(void) _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *,
390 ULONG, struct _Py_stat_struct *);
391#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700392
393#ifdef MS_WINDOWS
394static int
395win32_warn_bytes_api()
396{
397 return PyErr_WarnEx(PyExc_DeprecationWarning,
398 "The Windows bytes API has been deprecated, "
399 "use Unicode filenames instead",
400 1);
401}
402#endif
403
404
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200405#ifndef MS_WINDOWS
406PyObject *
407_PyLong_FromUid(uid_t uid)
408{
409 if (uid == (uid_t)-1)
410 return PyLong_FromLong(-1);
411 return PyLong_FromUnsignedLong(uid);
412}
413
414PyObject *
415_PyLong_FromGid(gid_t gid)
416{
417 if (gid == (gid_t)-1)
418 return PyLong_FromLong(-1);
419 return PyLong_FromUnsignedLong(gid);
420}
421
422int
423_Py_Uid_Converter(PyObject *obj, void *p)
424{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700425 uid_t uid;
426 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200427 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200428 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700429 unsigned long uresult;
430
431 index = PyNumber_Index(obj);
432 if (index == NULL) {
433 PyErr_Format(PyExc_TypeError,
434 "uid should be integer, not %.200s",
435 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200436 return 0;
437 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700438
439 /*
440 * Handling uid_t is complicated for two reasons:
441 * * Although uid_t is (always?) unsigned, it still
442 * accepts -1.
443 * * We don't know its size in advance--it may be
444 * bigger than an int, or it may be smaller than
445 * a long.
446 *
447 * So a bit of defensive programming is in order.
448 * Start with interpreting the value passed
449 * in as a signed long and see if it works.
450 */
451
452 result = PyLong_AsLongAndOverflow(index, &overflow);
453
454 if (!overflow) {
455 uid = (uid_t)result;
456
457 if (result == -1) {
458 if (PyErr_Occurred())
459 goto fail;
460 /* It's a legitimate -1, we're done. */
461 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200462 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700463
464 /* Any other negative number is disallowed. */
465 if (result < 0)
466 goto underflow;
467
468 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200469 if (sizeof(uid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700470 (long)uid != result)
471 goto underflow;
472 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200473 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700474
475 if (overflow < 0)
476 goto underflow;
477
478 /*
479 * Okay, the value overflowed a signed long. If it
480 * fits in an *unsigned* long, it may still be okay,
481 * as uid_t may be unsigned long on this platform.
482 */
483 uresult = PyLong_AsUnsignedLong(index);
484 if (PyErr_Occurred()) {
485 if (PyErr_ExceptionMatches(PyExc_OverflowError))
486 goto overflow;
487 goto fail;
488 }
489
490 uid = (uid_t)uresult;
491
492 /*
493 * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
494 * but this value would get interpreted as (uid_t)-1 by chown
495 * and its siblings. That's not what the user meant! So we
496 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100497 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700498 */
499 if (uid == (uid_t)-1)
500 goto overflow;
501
502 /* Ensure the value wasn't truncated. */
503 if (sizeof(uid_t) < sizeof(long) &&
504 (unsigned long)uid != uresult)
505 goto overflow;
506 /* fallthrough */
507
508success:
509 Py_DECREF(index);
510 *(uid_t *)p = uid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200511 return 1;
512
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700513underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200514 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700515 "uid is less than minimum");
516 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200517
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700518overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200519 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700520 "uid is greater than maximum");
521 /* fallthrough */
522
523fail:
524 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200525 return 0;
526}
527
528int
529_Py_Gid_Converter(PyObject *obj, void *p)
530{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700531 gid_t gid;
532 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200533 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200534 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700535 unsigned long uresult;
536
537 index = PyNumber_Index(obj);
538 if (index == NULL) {
539 PyErr_Format(PyExc_TypeError,
540 "gid should be integer, not %.200s",
541 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200542 return 0;
543 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700544
545 /*
546 * Handling gid_t is complicated for two reasons:
547 * * Although gid_t is (always?) unsigned, it still
548 * accepts -1.
549 * * We don't know its size in advance--it may be
550 * bigger than an int, or it may be smaller than
551 * a long.
552 *
553 * So a bit of defensive programming is in order.
554 * Start with interpreting the value passed
555 * in as a signed long and see if it works.
556 */
557
558 result = PyLong_AsLongAndOverflow(index, &overflow);
559
560 if (!overflow) {
561 gid = (gid_t)result;
562
563 if (result == -1) {
564 if (PyErr_Occurred())
565 goto fail;
566 /* It's a legitimate -1, we're done. */
567 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200568 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700569
570 /* Any other negative number is disallowed. */
571 if (result < 0) {
572 goto underflow;
573 }
574
575 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200576 if (sizeof(gid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700577 (long)gid != result)
578 goto underflow;
579 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200580 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700581
582 if (overflow < 0)
583 goto underflow;
584
585 /*
586 * Okay, the value overflowed a signed long. If it
587 * fits in an *unsigned* long, it may still be okay,
588 * as gid_t may be unsigned long on this platform.
589 */
590 uresult = PyLong_AsUnsignedLong(index);
591 if (PyErr_Occurred()) {
592 if (PyErr_ExceptionMatches(PyExc_OverflowError))
593 goto overflow;
594 goto fail;
595 }
596
597 gid = (gid_t)uresult;
598
599 /*
600 * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
601 * but this value would get interpreted as (gid_t)-1 by chown
602 * and its siblings. That's not what the user meant! So we
603 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100604 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700605 */
606 if (gid == (gid_t)-1)
607 goto overflow;
608
609 /* Ensure the value wasn't truncated. */
610 if (sizeof(gid_t) < sizeof(long) &&
611 (unsigned long)gid != uresult)
612 goto overflow;
613 /* fallthrough */
614
615success:
616 Py_DECREF(index);
617 *(gid_t *)p = gid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200618 return 1;
619
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700620underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200621 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700622 "gid is less than minimum");
623 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200624
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700625overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200626 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700627 "gid is greater than maximum");
628 /* fallthrough */
629
630fail:
631 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200632 return 0;
633}
634#endif /* MS_WINDOWS */
635
636
Gregory P. Smith702dada2015-01-28 16:07:52 -0800637#ifdef HAVE_LONG_LONG
638# define _PyLong_FromDev PyLong_FromLongLong
639#else
640# define _PyLong_FromDev PyLong_FromLong
641#endif
642
643
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200644#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
645static int
646_Py_Dev_Converter(PyObject *obj, void *p)
647{
648#ifdef HAVE_LONG_LONG
649 *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj);
650#else
651 *((dev_t *)p) = PyLong_AsUnsignedLong(obj);
652#endif
653 if (PyErr_Occurred())
654 return 0;
655 return 1;
656}
Gregory P. Smith702dada2015-01-28 16:07:52 -0800657#endif /* HAVE_MKNOD && HAVE_MAKEDEV */
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200658
659
Larry Hastings9cf065c2012-06-22 16:30:09 -0700660#ifdef AT_FDCWD
Trent Nelson9a461052012-09-18 21:50:06 -0400661/*
662 * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
663 * without the int cast, the value gets interpreted as uint (4291925331),
664 * which doesn't play nicely with all the initializer lines in this file that
665 * look like this:
666 * int dir_fd = DEFAULT_DIR_FD;
667 */
668#define DEFAULT_DIR_FD (int)AT_FDCWD
Larry Hastings9cf065c2012-06-22 16:30:09 -0700669#else
670#define DEFAULT_DIR_FD (-100)
671#endif
672
673static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200674_fd_converter(PyObject *o, int *p, const char *allowed)
675{
676 int overflow;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700677 long long_value;
678
679 PyObject *index = PyNumber_Index(o);
680 if (index == NULL) {
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200681 PyErr_Format(PyExc_TypeError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700682 "argument should be %s, not %.200s",
683 allowed, Py_TYPE(o)->tp_name);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700684 return 0;
685 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700686
687 long_value = PyLong_AsLongAndOverflow(index, &overflow);
688 Py_DECREF(index);
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200689 if (overflow > 0 || long_value > INT_MAX) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700690 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700691 "fd is greater than maximum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700692 return 0;
693 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200694 if (overflow < 0 || long_value < INT_MIN) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700695 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700696 "fd is less than minimum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700697 return 0;
698 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700699
Larry Hastings9cf065c2012-06-22 16:30:09 -0700700 *p = (int)long_value;
701 return 1;
702}
703
704static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200705dir_fd_converter(PyObject *o, void *p)
706{
707 if (o == Py_None) {
708 *(int *)p = DEFAULT_DIR_FD;
709 return 1;
710 }
711 return _fd_converter(o, (int *)p, "integer");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700712}
713
714
Larry Hastings9cf065c2012-06-22 16:30:09 -0700715/*
716 * A PyArg_ParseTuple "converter" function
717 * that handles filesystem paths in the manner
718 * preferred by the os module.
719 *
720 * path_converter accepts (Unicode) strings and their
721 * subclasses, and bytes and their subclasses. What
722 * it does with the argument depends on the platform:
723 *
724 * * On Windows, if we get a (Unicode) string we
725 * extract the wchar_t * and return it; if we get
726 * bytes we extract the char * and return that.
727 *
728 * * On all other platforms, strings are encoded
729 * to bytes using PyUnicode_FSConverter, then we
730 * extract the char * from the bytes object and
731 * return that.
732 *
733 * path_converter also optionally accepts signed
734 * integers (representing open file descriptors) instead
735 * of path strings.
736 *
737 * Input fields:
738 * path.nullable
739 * If nonzero, the path is permitted to be None.
740 * path.allow_fd
741 * If nonzero, the path is permitted to be a file handle
742 * (a signed int) instead of a string.
743 * path.function_name
744 * If non-NULL, path_converter will use that as the name
745 * of the function in error messages.
Larry Hastings31826802013-10-19 00:09:25 -0700746 * (If path.function_name is NULL it omits the function name.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700747 * path.argument_name
748 * If non-NULL, path_converter will use that as the name
749 * of the parameter in error messages.
750 * (If path.argument_name is NULL it uses "path".)
751 *
752 * Output fields:
753 * path.wide
754 * Points to the path if it was expressed as Unicode
755 * and was not encoded. (Only used on Windows.)
756 * path.narrow
757 * Points to the path if it was expressed as bytes,
758 * or it was Unicode and was encoded to bytes.
759 * path.fd
760 * Contains a file descriptor if path.accept_fd was true
761 * and the caller provided a signed integer instead of any
762 * sort of string.
763 *
764 * WARNING: if your "path" parameter is optional, and is
765 * unspecified, path_converter will never get called.
766 * So if you set allow_fd, you *MUST* initialize path.fd = -1
767 * yourself!
768 * path.length
769 * The length of the path in characters, if specified as
770 * a string.
771 * path.object
772 * The original object passed in.
773 * path.cleanup
774 * For internal use only. May point to a temporary object.
775 * (Pay no attention to the man behind the curtain.)
776 *
777 * At most one of path.wide or path.narrow will be non-NULL.
778 * If path was None and path.nullable was set,
779 * or if path was an integer and path.allow_fd was set,
780 * both path.wide and path.narrow will be NULL
781 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +0200782 *
Larry Hastings9cf065c2012-06-22 16:30:09 -0700783 * path_converter takes care to not write to the path_t
784 * unless it's successful. However it must reset the
785 * "cleanup" field each time it's called.
786 *
787 * Use as follows:
788 * path_t path;
789 * memset(&path, 0, sizeof(path));
790 * PyArg_ParseTuple(args, "O&", path_converter, &path);
791 * // ... use values from path ...
792 * path_cleanup(&path);
793 *
794 * (Note that if PyArg_Parse fails you don't need to call
795 * path_cleanup(). However it is safe to do so.)
796 */
797typedef struct {
Victor Stinner292c8352012-10-30 02:17:38 +0100798 const char *function_name;
799 const char *argument_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700800 int nullable;
801 int allow_fd;
802 wchar_t *wide;
803 char *narrow;
804 int fd;
805 Py_ssize_t length;
806 PyObject *object;
807 PyObject *cleanup;
808} path_t;
809
Larry Hastings2f936352014-08-05 14:04:04 +1000810#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
811 {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL}
Larry Hastings31826802013-10-19 00:09:25 -0700812
Larry Hastings9cf065c2012-06-22 16:30:09 -0700813static void
814path_cleanup(path_t *path) {
815 if (path->cleanup) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200816 Py_CLEAR(path->cleanup);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700817 }
818}
819
820static int
821path_converter(PyObject *o, void *p) {
822 path_t *path = (path_t *)p;
823 PyObject *unicode, *bytes;
824 Py_ssize_t length;
825 char *narrow;
826
827#define FORMAT_EXCEPTION(exc, fmt) \
828 PyErr_Format(exc, "%s%s" fmt, \
829 path->function_name ? path->function_name : "", \
830 path->function_name ? ": " : "", \
831 path->argument_name ? path->argument_name : "path")
832
833 /* Py_CLEANUP_SUPPORTED support */
834 if (o == NULL) {
835 path_cleanup(path);
836 return 1;
837 }
838
839 /* ensure it's always safe to call path_cleanup() */
840 path->cleanup = NULL;
841
842 if (o == Py_None) {
843 if (!path->nullable) {
844 FORMAT_EXCEPTION(PyExc_TypeError,
845 "can't specify None for %s argument");
846 return 0;
847 }
848 path->wide = NULL;
849 path->narrow = NULL;
850 path->length = 0;
851 path->object = o;
852 path->fd = -1;
853 return 1;
854 }
855
856 unicode = PyUnicode_FromObject(o);
857 if (unicode) {
858#ifdef MS_WINDOWS
859 wchar_t *wide;
Victor Stinner59799a82013-11-13 14:17:30 +0100860
861 wide = PyUnicode_AsUnicodeAndSize(unicode, &length);
862 if (!wide) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700863 Py_DECREF(unicode);
864 return 0;
865 }
Victor Stinner59799a82013-11-13 14:17:30 +0100866 if (length > 32767) {
867 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700868 Py_DECREF(unicode);
869 return 0;
870 }
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300871 if (wcslen(wide) != length) {
Serhiy Storchaka7e9d1d12015-04-20 10:12:28 +0300872 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character");
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300873 Py_DECREF(unicode);
874 return 0;
875 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700876
877 path->wide = wide;
878 path->narrow = NULL;
879 path->length = length;
880 path->object = o;
881 path->fd = -1;
882 path->cleanup = unicode;
883 return Py_CLEANUP_SUPPORTED;
884#else
885 int converted = PyUnicode_FSConverter(unicode, &bytes);
886 Py_DECREF(unicode);
887 if (!converted)
888 bytes = NULL;
889#endif
890 }
891 else {
892 PyErr_Clear();
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200893 if (PyObject_CheckBuffer(o))
894 bytes = PyBytes_FromObject(o);
895 else
896 bytes = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700897 if (!bytes) {
898 PyErr_Clear();
899 if (path->allow_fd) {
900 int fd;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200901 int result = _fd_converter(o, &fd,
902 "string, bytes or integer");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700903 if (result) {
904 path->wide = NULL;
905 path->narrow = NULL;
906 path->length = 0;
907 path->object = o;
908 path->fd = fd;
909 return result;
910 }
911 }
912 }
913 }
914
915 if (!bytes) {
916 if (!PyErr_Occurred())
917 FORMAT_EXCEPTION(PyExc_TypeError, "illegal type for %s parameter");
918 return 0;
919 }
920
921#ifdef MS_WINDOWS
922 if (win32_warn_bytes_api()) {
923 Py_DECREF(bytes);
924 return 0;
925 }
926#endif
927
928 length = PyBytes_GET_SIZE(bytes);
929#ifdef MS_WINDOWS
Victor Stinner75875072013-11-24 19:23:25 +0100930 if (length > MAX_PATH-1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700931 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
932 Py_DECREF(bytes);
933 return 0;
934 }
935#endif
936
937 narrow = PyBytes_AS_STRING(bytes);
Victor Stinner706768c2014-08-16 01:03:39 +0200938 if ((size_t)length != strlen(narrow)) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +0300939 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700940 Py_DECREF(bytes);
941 return 0;
942 }
943
944 path->wide = NULL;
945 path->narrow = narrow;
946 path->length = length;
947 path->object = o;
948 path->fd = -1;
949 path->cleanup = bytes;
950 return Py_CLEANUP_SUPPORTED;
951}
952
953static void
954argument_unavailable_error(char *function_name, char *argument_name) {
955 PyErr_Format(PyExc_NotImplementedError,
956 "%s%s%s unavailable on this platform",
957 (function_name != NULL) ? function_name : "",
958 (function_name != NULL) ? ": ": "",
959 argument_name);
960}
961
962static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200963dir_fd_unavailable(PyObject *o, void *p)
964{
965 int dir_fd;
966 if (!dir_fd_converter(o, &dir_fd))
Larry Hastings9cf065c2012-06-22 16:30:09 -0700967 return 0;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200968 if (dir_fd != DEFAULT_DIR_FD) {
969 argument_unavailable_error(NULL, "dir_fd");
970 return 0;
971 }
972 *(int *)p = dir_fd;
973 return 1;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700974}
975
976static int
977fd_specified(char *function_name, int fd) {
978 if (fd == -1)
979 return 0;
980
981 argument_unavailable_error(function_name, "fd");
982 return 1;
983}
984
985static int
986follow_symlinks_specified(char *function_name, int follow_symlinks) {
987 if (follow_symlinks)
988 return 0;
989
990 argument_unavailable_error(function_name, "follow_symlinks");
991 return 1;
992}
993
994static int
995path_and_dir_fd_invalid(char *function_name, path_t *path, int dir_fd) {
996 if (!path->narrow && !path->wide && (dir_fd != DEFAULT_DIR_FD)) {
997 PyErr_Format(PyExc_ValueError,
998 "%s: can't specify dir_fd without matching path",
999 function_name);
1000 return 1;
1001 }
1002 return 0;
1003}
1004
1005static int
1006dir_fd_and_fd_invalid(char *function_name, int dir_fd, int fd) {
1007 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
1008 PyErr_Format(PyExc_ValueError,
1009 "%s: can't specify both dir_fd and fd",
1010 function_name);
1011 return 1;
1012 }
1013 return 0;
1014}
1015
1016static int
1017fd_and_follow_symlinks_invalid(char *function_name, int fd,
1018 int follow_symlinks) {
1019 if ((fd > 0) && (!follow_symlinks)) {
1020 PyErr_Format(PyExc_ValueError,
1021 "%s: cannot use fd and follow_symlinks together",
1022 function_name);
1023 return 1;
1024 }
1025 return 0;
1026}
1027
1028static int
1029dir_fd_and_follow_symlinks_invalid(char *function_name, int dir_fd,
1030 int follow_symlinks) {
1031 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
1032 PyErr_Format(PyExc_ValueError,
1033 "%s: cannot use dir_fd and follow_symlinks together",
1034 function_name);
1035 return 1;
1036 }
1037 return 0;
1038}
1039
Larry Hastings2f936352014-08-05 14:04:04 +10001040#ifdef MS_WINDOWS
1041 typedef PY_LONG_LONG Py_off_t;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001042#else
Larry Hastings2f936352014-08-05 14:04:04 +10001043 typedef off_t Py_off_t;
1044#endif
1045
1046static int
1047Py_off_t_converter(PyObject *arg, void *addr)
1048{
1049#ifdef HAVE_LARGEFILE_SUPPORT
1050 *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
1051#else
1052 *((Py_off_t *)addr) = PyLong_AsLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001053#endif
1054 if (PyErr_Occurred())
1055 return 0;
1056 return 1;
1057}
Larry Hastings2f936352014-08-05 14:04:04 +10001058
1059static PyObject *
1060PyLong_FromPy_off_t(Py_off_t offset)
1061{
1062#ifdef HAVE_LARGEFILE_SUPPORT
1063 return PyLong_FromLongLong(offset);
1064#else
1065 return PyLong_FromLong(offset);
Ross Lagerwallb1e5d592011-09-19 08:30:43 +02001066#endif
Larry Hastings2f936352014-08-05 14:04:04 +10001067}
1068
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001069
Steve Dowerd81431f2015-03-06 14:47:02 -08001070#if defined _MSC_VER && _MSC_VER >= 1400 && _MSC_VER < 1900
1071/* Legacy implementation of _PyVerify_fd_dup2 while transitioning to
1072 * MSVC 14.0. This should eventually be removed. (issue23524)
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00001073 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00001074#define IOINFO_L2E 5
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00001075#define IOINFO_ARRAYS 64
Steve Dower65e4cb12014-11-22 12:54:57 -08001076#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00001077#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00001078#define _NO_CONSOLE_FILENO (intptr_t)-2
1079
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00001080/* the special case of checking dup2. The target fd must be in a sensible range */
1081static int
1082_PyVerify_fd_dup2(int fd1, int fd2)
1083{
Victor Stinner8c62be82010-05-06 00:08:46 +00001084 if (!_PyVerify_fd(fd1))
1085 return 0;
1086 if (fd2 == _NO_CONSOLE_FILENO)
1087 return 0;
1088 if ((unsigned)fd2 < _NHANDLE_)
1089 return 1;
1090 else
1091 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00001092}
1093#else
Steve Dowerd81431f2015-03-06 14:47:02 -08001094#define _PyVerify_fd_dup2(fd1, fd2) (_PyVerify_fd(fd1) && (fd2) >= 0)
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00001095#endif
1096
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001097#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +00001098
1099static int
Brian Curtind25aef52011-06-13 15:16:04 -05001100win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001101{
1102 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1103 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
1104 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001105
1106 if (0 == DeviceIoControl(
1107 reparse_point_handle,
1108 FSCTL_GET_REPARSE_POINT,
1109 NULL, 0, /* in buffer */
1110 target_buffer, sizeof(target_buffer),
1111 &n_bytes_returned,
1112 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -05001113 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001114
1115 if (reparse_tag)
1116 *reparse_tag = rdb->ReparseTag;
1117
Brian Curtind25aef52011-06-13 15:16:04 -05001118 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001119}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001120
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001121#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001122
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001123/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001124#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +00001125/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001126** environ directly, we must obtain it with _NSGetEnviron(). See also
1127** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +00001128*/
1129#include <crt_externs.h>
1130static char **environ;
1131#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001132extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001133#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001134
Barry Warsaw53699e91996-12-10 23:23:01 +00001135static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001136convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001137{
Victor Stinner8c62be82010-05-06 00:08:46 +00001138 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001139#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001140 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001141#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001142 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001143#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00001144
Victor Stinner8c62be82010-05-06 00:08:46 +00001145 d = PyDict_New();
1146 if (d == NULL)
1147 return NULL;
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001148#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Victor Stinner8c62be82010-05-06 00:08:46 +00001149 if (environ == NULL)
1150 environ = *_NSGetEnviron();
1151#endif
1152#ifdef MS_WINDOWS
1153 /* _wenviron must be initialized in this way if the program is started
1154 through main() instead of wmain(). */
1155 _wgetenv(L"");
1156 if (_wenviron == NULL)
1157 return d;
1158 /* This part ignores errors */
1159 for (e = _wenviron; *e != NULL; e++) {
1160 PyObject *k;
1161 PyObject *v;
1162 wchar_t *p = wcschr(*e, L'=');
1163 if (p == NULL)
1164 continue;
1165 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1166 if (k == NULL) {
1167 PyErr_Clear();
1168 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001169 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001170 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1171 if (v == NULL) {
1172 PyErr_Clear();
1173 Py_DECREF(k);
1174 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001175 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001176 if (PyDict_GetItem(d, k) == NULL) {
1177 if (PyDict_SetItem(d, k, v) != 0)
1178 PyErr_Clear();
1179 }
1180 Py_DECREF(k);
1181 Py_DECREF(v);
1182 }
1183#else
1184 if (environ == NULL)
1185 return d;
1186 /* This part ignores errors */
1187 for (e = environ; *e != NULL; e++) {
1188 PyObject *k;
1189 PyObject *v;
1190 char *p = strchr(*e, '=');
1191 if (p == NULL)
1192 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +00001193 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +00001194 if (k == NULL) {
1195 PyErr_Clear();
1196 continue;
1197 }
Victor Stinner84ae1182010-05-06 22:05:07 +00001198 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +00001199 if (v == NULL) {
1200 PyErr_Clear();
1201 Py_DECREF(k);
1202 continue;
1203 }
1204 if (PyDict_GetItem(d, k) == NULL) {
1205 if (PyDict_SetItem(d, k, v) != 0)
1206 PyErr_Clear();
1207 }
1208 Py_DECREF(k);
1209 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001210 }
1211#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001212 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001213}
1214
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001215/* Set a POSIX-specific error from errno, and return NULL */
1216
Barry Warsawd58d7641998-07-23 16:14:40 +00001217static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001218posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001219{
Victor Stinner8c62be82010-05-06 00:08:46 +00001220 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001221}
Mark Hammondef8b6542001-05-13 08:04:26 +00001222
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001223#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001224static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +00001225win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001226{
Victor Stinner8c62be82010-05-06 00:08:46 +00001227 /* XXX We should pass the function name along in the future.
1228 (winreg.c also wants to pass the function name.)
1229 This would however require an additional param to the
1230 Windows error object, which is non-trivial.
1231 */
1232 errno = GetLastError();
1233 if (filename)
1234 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1235 else
1236 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001237}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001238
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001239static PyObject *
Victor Stinnereb5657a2011-09-30 01:44:27 +02001240win32_error_object(char* function, PyObject* filename)
1241{
1242 /* XXX - see win32_error for comments on 'function' */
1243 errno = GetLastError();
1244 if (filename)
1245 return PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001246 PyExc_OSError,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001247 errno,
1248 filename);
1249 else
1250 return PyErr_SetFromWindowsErr(errno);
1251}
1252
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001253#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001254
Larry Hastings9cf065c2012-06-22 16:30:09 -07001255static PyObject *
Victor Stinner292c8352012-10-30 02:17:38 +01001256path_error(path_t *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001257{
1258#ifdef MS_WINDOWS
Victor Stinner292c8352012-10-30 02:17:38 +01001259 return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError,
1260 0, path->object);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001261#else
Victor Stinner292c8352012-10-30 02:17:38 +01001262 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001263#endif
1264}
1265
Larry Hastings31826802013-10-19 00:09:25 -07001266
Larry Hastingsb0827312014-02-09 22:05:19 -08001267static PyObject *
1268path_error2(path_t *path, path_t *path2)
1269{
1270#ifdef MS_WINDOWS
1271 return PyErr_SetExcFromWindowsErrWithFilenameObjects(PyExc_OSError,
1272 0, path->object, path2->object);
1273#else
1274 return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError,
1275 path->object, path2->object);
1276#endif
1277}
1278
1279
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001280/* POSIX generic methods */
1281
Larry Hastings2f936352014-08-05 14:04:04 +10001282static int
1283fildes_converter(PyObject *o, void *p)
Fred Drake4d1e64b2002-04-15 19:40:07 +00001284{
Victor Stinner8c62be82010-05-06 00:08:46 +00001285 int fd;
Larry Hastings2f936352014-08-05 14:04:04 +10001286 int *pointer = (int *)p;
1287 fd = PyObject_AsFileDescriptor(o);
Victor Stinner8c62be82010-05-06 00:08:46 +00001288 if (fd < 0)
Larry Hastings2f936352014-08-05 14:04:04 +10001289 return 0;
Larry Hastings2f936352014-08-05 14:04:04 +10001290 *pointer = fd;
1291 return 1;
1292}
1293
1294static PyObject *
1295posix_fildes_fd(int fd, int (*func)(int))
1296{
1297 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001298 int async_err = 0;
1299
Steve Dower8fc89802015-04-12 00:26:27 -04001300 if (!_PyVerify_fd(fd))
1301 return posix_error();
1302
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001303 do {
1304 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001305 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001306 res = (*func)(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001307 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001308 Py_END_ALLOW_THREADS
1309 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1310 if (res != 0)
1311 return (!async_err) ? posix_error() : NULL;
1312 Py_RETURN_NONE;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001313}
Guido van Rossum21142a01999-01-08 21:05:37 +00001314
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001315
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001316#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001317/* This is a reimplementation of the C library's chdir function,
1318 but one that produces Win32 errors instead of DOS error codes.
1319 chdir is essentially a wrapper around SetCurrentDirectory; however,
1320 it also needs to set "magic" environment variables indicating
1321 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001322static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001323win32_chdir(LPCSTR path)
1324{
Victor Stinner75875072013-11-24 19:23:25 +01001325 char new_path[MAX_PATH];
Victor Stinner8c62be82010-05-06 00:08:46 +00001326 int result;
1327 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001328
Victor Stinner8c62be82010-05-06 00:08:46 +00001329 if(!SetCurrentDirectoryA(path))
1330 return FALSE;
Victor Stinner75875072013-11-24 19:23:25 +01001331 result = GetCurrentDirectoryA(Py_ARRAY_LENGTH(new_path), new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001332 if (!result)
1333 return FALSE;
1334 /* In the ANSI API, there should not be any paths longer
Victor Stinner75875072013-11-24 19:23:25 +01001335 than MAX_PATH-1 (not including the final null character). */
1336 assert(result < Py_ARRAY_LENGTH(new_path));
Victor Stinner8c62be82010-05-06 00:08:46 +00001337 if (strncmp(new_path, "\\\\", 2) == 0 ||
1338 strncmp(new_path, "//", 2) == 0)
1339 /* UNC path, nothing to do. */
1340 return TRUE;
1341 env[1] = new_path[0];
1342 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001343}
1344
1345/* The Unicode version differs from the ANSI version
1346 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001347static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001348win32_wchdir(LPCWSTR path)
1349{
Victor Stinnered537822015-12-13 21:40:26 +01001350 wchar_t path_buf[MAX_PATH], *new_path = path_buf;
Victor Stinner8c62be82010-05-06 00:08:46 +00001351 int result;
1352 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001353
Victor Stinner8c62be82010-05-06 00:08:46 +00001354 if(!SetCurrentDirectoryW(path))
1355 return FALSE;
Victor Stinnered537822015-12-13 21:40:26 +01001356 result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001357 if (!result)
1358 return FALSE;
Victor Stinnere847d712015-12-14 00:21:50 +01001359 if (result > Py_ARRAY_LENGTH(path_buf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001360 new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00001361 if (!new_path) {
1362 SetLastError(ERROR_OUTOFMEMORY);
1363 return FALSE;
1364 }
1365 result = GetCurrentDirectoryW(result, new_path);
1366 if (!result) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001367 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001368 return FALSE;
1369 }
1370 }
1371 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1372 wcsncmp(new_path, L"//", 2) == 0)
1373 /* UNC path, nothing to do. */
1374 return TRUE;
1375 env[1] = new_path[0];
1376 result = SetEnvironmentVariableW(env, new_path);
Victor Stinnered537822015-12-13 21:40:26 +01001377 if (new_path != path_buf)
Victor Stinnerb6404912013-07-07 16:21:41 +02001378 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001379 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001380}
1381#endif
1382
Martin v. Löwis14694662006-02-03 12:54:16 +00001383#ifdef MS_WINDOWS
1384/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1385 - time stamps are restricted to second resolution
1386 - file modification times suffer from forth-and-back conversions between
1387 UTC and local time
1388 Therefore, we implement our own stat, based on the Win32 API directly.
1389*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001390#define HAVE_STAT_NSEC 1
Zachary Ware63f277b2014-06-19 09:46:37 -05001391#define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001392
Guido van Rossumd8faa362007-04-27 19:54:29 +00001393static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001394attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001395{
Victor Stinner8c62be82010-05-06 00:08:46 +00001396 HANDLE hFindFile;
1397 WIN32_FIND_DATAA FileData;
1398 hFindFile = FindFirstFileA(pszFile, &FileData);
1399 if (hFindFile == INVALID_HANDLE_VALUE)
1400 return FALSE;
1401 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001402 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001403 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001404 info->dwFileAttributes = FileData.dwFileAttributes;
1405 info->ftCreationTime = FileData.ftCreationTime;
1406 info->ftLastAccessTime = FileData.ftLastAccessTime;
1407 info->ftLastWriteTime = FileData.ftLastWriteTime;
1408 info->nFileSizeHigh = FileData.nFileSizeHigh;
1409 info->nFileSizeLow = FileData.nFileSizeLow;
1410/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001411 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1412 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001413 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001414}
1415
Victor Stinner6036e442015-03-08 01:58:04 +01001416static void
1417find_data_to_file_info_w(WIN32_FIND_DATAW *pFileData,
1418 BY_HANDLE_FILE_INFORMATION *info,
1419 ULONG *reparse_tag)
1420{
1421 memset(info, 0, sizeof(*info));
1422 info->dwFileAttributes = pFileData->dwFileAttributes;
1423 info->ftCreationTime = pFileData->ftCreationTime;
1424 info->ftLastAccessTime = pFileData->ftLastAccessTime;
1425 info->ftLastWriteTime = pFileData->ftLastWriteTime;
1426 info->nFileSizeHigh = pFileData->nFileSizeHigh;
1427 info->nFileSizeLow = pFileData->nFileSizeLow;
1428/* info->nNumberOfLinks = 1; */
1429 if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1430 *reparse_tag = pFileData->dwReserved0;
1431 else
1432 *reparse_tag = 0;
1433}
1434
Guido van Rossumd8faa362007-04-27 19:54:29 +00001435static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001436attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001437{
Victor Stinner8c62be82010-05-06 00:08:46 +00001438 HANDLE hFindFile;
1439 WIN32_FIND_DATAW FileData;
1440 hFindFile = FindFirstFileW(pszFile, &FileData);
1441 if (hFindFile == INVALID_HANDLE_VALUE)
1442 return FALSE;
1443 FindClose(hFindFile);
Victor Stinner6036e442015-03-08 01:58:04 +01001444 find_data_to_file_info_w(&FileData, info, reparse_tag);
Victor Stinner8c62be82010-05-06 00:08:46 +00001445 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001446}
1447
Brian Curtind25aef52011-06-13 15:16:04 -05001448static BOOL
1449get_target_path(HANDLE hdl, wchar_t **target_path)
1450{
1451 int buf_size, result_length;
1452 wchar_t *buf;
1453
1454 /* We have a good handle to the target, use it to determine
1455 the target path name (then we'll call lstat on it). */
Steve Dower2ea51c92015-03-20 21:49:12 -07001456 buf_size = GetFinalPathNameByHandleW(hdl, 0, 0,
1457 VOLUME_NAME_DOS);
Brian Curtind25aef52011-06-13 15:16:04 -05001458 if(!buf_size)
1459 return FALSE;
1460
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02001461 buf = PyMem_New(wchar_t, buf_size+1);
Brian Curtinc8be8402011-06-14 09:52:50 -05001462 if (!buf) {
1463 SetLastError(ERROR_OUTOFMEMORY);
1464 return FALSE;
1465 }
1466
Steve Dower2ea51c92015-03-20 21:49:12 -07001467 result_length = GetFinalPathNameByHandleW(hdl,
Brian Curtind25aef52011-06-13 15:16:04 -05001468 buf, buf_size, VOLUME_NAME_DOS);
1469
1470 if(!result_length) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001471 PyMem_Free(buf);
Brian Curtind25aef52011-06-13 15:16:04 -05001472 return FALSE;
1473 }
1474
1475 if(!CloseHandle(hdl)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001476 PyMem_Free(buf);
Brian Curtind25aef52011-06-13 15:16:04 -05001477 return FALSE;
1478 }
1479
1480 buf[result_length] = 0;
1481
1482 *target_path = buf;
1483 return TRUE;
1484}
1485
1486static int
Steve Dowerf2f373f2015-02-21 08:44:05 -08001487win32_xstat_impl_w(const wchar_t *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001488 BOOL traverse);
1489static int
Steve Dowerf2f373f2015-02-21 08:44:05 -08001490win32_xstat_impl(const char *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001491 BOOL traverse)
1492{
Victor Stinner26de69d2011-06-17 15:15:38 +02001493 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001494 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001495 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001496 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001497 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001498 const char *dot;
1499
1500 hFile = CreateFileA(
1501 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001502 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001503 0, /* share mode */
1504 NULL, /* security attributes */
1505 OPEN_EXISTING,
1506 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001507 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1508 Because of this, calls like GetFinalPathNameByHandle will return
R David Murrayfc069992013-12-13 20:52:19 -05001509 the symlink path again and not the actual final path. */
Brian Curtind25aef52011-06-13 15:16:04 -05001510 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1511 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001512 NULL);
1513
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001514 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001515 /* Either the target doesn't exist, or we don't have access to
1516 get a handle to it. If the former, we need to return an error.
1517 If the latter, we can use attributes_from_dir. */
1518 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001519 return -1;
1520 /* Could not get attributes on open file. Fall back to
1521 reading the directory. */
1522 if (!attributes_from_dir(path, &info, &reparse_tag))
1523 /* Very strange. This should not fail now */
1524 return -1;
1525 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1526 if (traverse) {
1527 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001528 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001529 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001530 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001531 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001532 } else {
1533 if (!GetFileInformationByHandle(hFile, &info)) {
1534 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001535 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001536 }
1537 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001538 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1539 return -1;
1540
1541 /* Close the outer open file handle now that we're about to
1542 reopen it with different flags. */
1543 if (!CloseHandle(hFile))
1544 return -1;
1545
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001546 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001547 /* In order to call GetFinalPathNameByHandle we need to open
1548 the file without the reparse handling flag set. */
1549 hFile2 = CreateFileA(
1550 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1551 NULL, OPEN_EXISTING,
1552 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1553 NULL);
1554 if (hFile2 == INVALID_HANDLE_VALUE)
1555 return -1;
1556
1557 if (!get_target_path(hFile2, &target_path))
1558 return -1;
1559
1560 code = win32_xstat_impl_w(target_path, result, FALSE);
Victor Stinnerb6404912013-07-07 16:21:41 +02001561 PyMem_Free(target_path);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001562 return code;
1563 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001564 } else
1565 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001566 }
Steve Dowera2af1a52015-02-21 10:04:10 -08001567 _Py_attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001568
1569 /* Set S_IEXEC if it is an .exe, .bat, ... */
1570 dot = strrchr(path, '.');
1571 if (dot) {
1572 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1573 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1574 result->st_mode |= 0111;
1575 }
1576 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001577}
1578
1579static int
Steve Dowerf2f373f2015-02-21 08:44:05 -08001580win32_xstat_impl_w(const wchar_t *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001581 BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001582{
1583 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001584 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001585 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001586 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001587 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001588 const wchar_t *dot;
1589
1590 hFile = CreateFileW(
1591 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001592 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001593 0, /* share mode */
1594 NULL, /* security attributes */
1595 OPEN_EXISTING,
1596 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001597 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1598 Because of this, calls like GetFinalPathNameByHandle will return
R David Murrayfc069992013-12-13 20:52:19 -05001599 the symlink path again and not the actual final path. */
Victor Stinner26de69d2011-06-17 15:15:38 +02001600 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
Brian Curtind25aef52011-06-13 15:16:04 -05001601 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001602 NULL);
1603
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001604 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001605 /* Either the target doesn't exist, or we don't have access to
1606 get a handle to it. If the former, we need to return an error.
1607 If the latter, we can use attributes_from_dir. */
1608 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001609 return -1;
1610 /* Could not get attributes on open file. Fall back to
1611 reading the directory. */
1612 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1613 /* Very strange. This should not fail now */
1614 return -1;
1615 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1616 if (traverse) {
1617 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001618 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001619 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001620 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001621 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001622 } else {
1623 if (!GetFileInformationByHandle(hFile, &info)) {
1624 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001625 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001626 }
1627 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001628 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1629 return -1;
1630
1631 /* Close the outer open file handle now that we're about to
1632 reopen it with different flags. */
1633 if (!CloseHandle(hFile))
1634 return -1;
1635
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001636 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001637 /* In order to call GetFinalPathNameByHandle we need to open
1638 the file without the reparse handling flag set. */
1639 hFile2 = CreateFileW(
1640 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1641 NULL, OPEN_EXISTING,
1642 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1643 NULL);
1644 if (hFile2 == INVALID_HANDLE_VALUE)
1645 return -1;
1646
1647 if (!get_target_path(hFile2, &target_path))
1648 return -1;
1649
1650 code = win32_xstat_impl_w(target_path, result, FALSE);
Victor Stinnerb6404912013-07-07 16:21:41 +02001651 PyMem_Free(target_path);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001652 return code;
1653 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001654 } else
1655 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001656 }
Steve Dowera2af1a52015-02-21 10:04:10 -08001657 _Py_attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001658
1659 /* Set S_IEXEC if it is an .exe, .bat, ... */
1660 dot = wcsrchr(path, '.');
1661 if (dot) {
1662 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1663 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1664 result->st_mode |= 0111;
1665 }
1666 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001667}
1668
1669static int
Steve Dowerf2f373f2015-02-21 08:44:05 -08001670win32_xstat(const char *path, struct _Py_stat_struct *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001671{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001672 /* Protocol violation: we explicitly clear errno, instead of
1673 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001674 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001675 errno = 0;
1676 return code;
1677}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001678
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001679static int
Steve Dowerf2f373f2015-02-21 08:44:05 -08001680win32_xstat_w(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001681{
1682 /* Protocol violation: we explicitly clear errno, instead of
1683 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001684 int code = win32_xstat_impl_w(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001685 errno = 0;
1686 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001687}
Brian Curtind25aef52011-06-13 15:16:04 -05001688/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001689
1690 In Posix, stat automatically traverses symlinks and returns the stat
1691 structure for the target. In Windows, the equivalent GetFileAttributes by
1692 default does not traverse symlinks and instead returns attributes for
1693 the symlink.
1694
1695 Therefore, win32_lstat will get the attributes traditionally, and
1696 win32_stat will first explicitly resolve the symlink target and then will
1697 call win32_lstat on that result.
1698
Ezio Melotti4969f702011-03-15 05:59:46 +02001699 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001700
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001701static int
Steve Dowerf2f373f2015-02-21 08:44:05 -08001702win32_lstat(const char* path, struct _Py_stat_struct *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001703{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001704 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001705}
1706
Victor Stinner8c62be82010-05-06 00:08:46 +00001707static int
Steve Dowerf2f373f2015-02-21 08:44:05 -08001708win32_lstat_w(const wchar_t* path, struct _Py_stat_struct *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001709{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001710 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001711}
1712
1713static int
Steve Dowerf2f373f2015-02-21 08:44:05 -08001714win32_stat(const char* path, struct _Py_stat_struct *result)
Brian Curtind40e6f72010-07-08 21:39:08 +00001715{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001716 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001717}
1718
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001719static int
Steve Dowerf2f373f2015-02-21 08:44:05 -08001720win32_stat_w(const wchar_t* path, struct _Py_stat_struct *result)
Brian Curtind40e6f72010-07-08 21:39:08 +00001721{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001722 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001723}
1724
Martin v. Löwis14694662006-02-03 12:54:16 +00001725#endif /* MS_WINDOWS */
1726
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001727PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07001728"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001729This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001730 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001731or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1732\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001733Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1734or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001735\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001736See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001737
1738static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001739 {"st_mode", "protection bits"},
1740 {"st_ino", "inode"},
1741 {"st_dev", "device"},
1742 {"st_nlink", "number of hard links"},
1743 {"st_uid", "user ID of owner"},
1744 {"st_gid", "group ID of owner"},
1745 {"st_size", "total size, in bytes"},
1746 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1747 {NULL, "integer time of last access"},
1748 {NULL, "integer time of last modification"},
1749 {NULL, "integer time of last change"},
1750 {"st_atime", "time of last access"},
1751 {"st_mtime", "time of last modification"},
1752 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07001753 {"st_atime_ns", "time of last access in nanoseconds"},
1754 {"st_mtime_ns", "time of last modification in nanoseconds"},
1755 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001756#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001757 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001758#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001759#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001760 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001761#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001762#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001763 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001764#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001765#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001766 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001767#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001768#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001769 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001770#endif
1771#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001772 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001773#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05001774#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1775 {"st_file_attributes", "Windows file attribute bits"},
1776#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001777 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001778};
1779
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001780#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07001781#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001782#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07001783#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001784#endif
1785
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001786#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001787#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1788#else
1789#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1790#endif
1791
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001792#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001793#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1794#else
1795#define ST_RDEV_IDX ST_BLOCKS_IDX
1796#endif
1797
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001798#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1799#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1800#else
1801#define ST_FLAGS_IDX ST_RDEV_IDX
1802#endif
1803
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001804#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001805#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001806#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001807#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001808#endif
1809
1810#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1811#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1812#else
1813#define ST_BIRTHTIME_IDX ST_GEN_IDX
1814#endif
1815
Zachary Ware63f277b2014-06-19 09:46:37 -05001816#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1817#define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1)
1818#else
1819#define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX
1820#endif
1821
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001822static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001823 "stat_result", /* name */
1824 stat_result__doc__, /* doc */
1825 stat_result_fields,
1826 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001827};
1828
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001829PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001830"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1831This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001832 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001833or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001834\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001835See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001836
1837static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001838 {"f_bsize", },
1839 {"f_frsize", },
1840 {"f_blocks", },
1841 {"f_bfree", },
1842 {"f_bavail", },
1843 {"f_files", },
1844 {"f_ffree", },
1845 {"f_favail", },
1846 {"f_flag", },
1847 {"f_namemax",},
1848 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001849};
1850
1851static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001852 "statvfs_result", /* name */
1853 statvfs_result__doc__, /* doc */
1854 statvfs_result_fields,
1855 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001856};
1857
Ross Lagerwall7807c352011-03-17 20:20:30 +02001858#if defined(HAVE_WAITID) && !defined(__APPLE__)
1859PyDoc_STRVAR(waitid_result__doc__,
1860"waitid_result: Result from waitid.\n\n\
1861This object may be accessed either as a tuple of\n\
1862 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1863or via the attributes si_pid, si_uid, and so on.\n\
1864\n\
1865See os.waitid for more information.");
1866
1867static PyStructSequence_Field waitid_result_fields[] = {
1868 {"si_pid", },
1869 {"si_uid", },
1870 {"si_signo", },
1871 {"si_status", },
1872 {"si_code", },
1873 {0}
1874};
1875
1876static PyStructSequence_Desc waitid_result_desc = {
1877 "waitid_result", /* name */
1878 waitid_result__doc__, /* doc */
1879 waitid_result_fields,
1880 5
1881};
1882static PyTypeObject WaitidResultType;
1883#endif
1884
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001885static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001886static PyTypeObject StatResultType;
1887static PyTypeObject StatVFSResultType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001888#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001889static PyTypeObject SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001890#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001891static newfunc structseq_new;
1892
1893static PyObject *
1894statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1895{
Victor Stinner8c62be82010-05-06 00:08:46 +00001896 PyStructSequence *result;
1897 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001898
Victor Stinner8c62be82010-05-06 00:08:46 +00001899 result = (PyStructSequence*)structseq_new(type, args, kwds);
1900 if (!result)
1901 return NULL;
1902 /* If we have been initialized from a tuple,
1903 st_?time might be set to None. Initialize it
1904 from the int slots. */
1905 for (i = 7; i <= 9; i++) {
1906 if (result->ob_item[i+3] == Py_None) {
1907 Py_DECREF(Py_None);
1908 Py_INCREF(result->ob_item[i]);
1909 result->ob_item[i+3] = result->ob_item[i];
1910 }
1911 }
1912 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001913}
1914
1915
1916
1917/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001918static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001919
1920PyDoc_STRVAR(stat_float_times__doc__,
1921"stat_float_times([newval]) -> oldval\n\n\
1922Determine whether os.[lf]stat represents time stamps as float objects.\n\
Larry Hastings2f936352014-08-05 14:04:04 +10001923\n\
1924If value is True, future calls to stat() return floats; if it is False,\n\
1925future calls return ints.\n\
1926If value is omitted, return the current setting.\n");
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001927
Larry Hastings2f936352014-08-05 14:04:04 +10001928/* AC 3.5: the public default value should be None, not ready for that yet */
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001929static PyObject*
1930stat_float_times(PyObject* self, PyObject *args)
1931{
Victor Stinner8c62be82010-05-06 00:08:46 +00001932 int newval = -1;
1933 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1934 return NULL;
Victor Stinner034d0aa2012-06-05 01:22:15 +02001935 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1936 "stat_float_times() is deprecated",
1937 1))
1938 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001939 if (newval == -1)
1940 /* Return old value */
1941 return PyBool_FromLong(_stat_float_times);
1942 _stat_float_times = newval;
1943 Py_INCREF(Py_None);
1944 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001945}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001946
Larry Hastings6fe20b32012-04-19 15:07:49 -07001947static PyObject *billion = NULL;
1948
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001949static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01001950fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001951{
Larry Hastings6fe20b32012-04-19 15:07:49 -07001952 PyObject *s = _PyLong_FromTime_t(sec);
1953 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
1954 PyObject *s_in_ns = NULL;
1955 PyObject *ns_total = NULL;
1956 PyObject *float_s = NULL;
1957
1958 if (!(s && ns_fractional))
1959 goto exit;
1960
1961 s_in_ns = PyNumber_Multiply(s, billion);
1962 if (!s_in_ns)
1963 goto exit;
1964
1965 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
1966 if (!ns_total)
1967 goto exit;
1968
Victor Stinner4195b5c2012-02-08 23:03:19 +01001969 if (_stat_float_times) {
Larry Hastings6fe20b32012-04-19 15:07:49 -07001970 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
1971 if (!float_s)
1972 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00001973 }
Larry Hastings6fe20b32012-04-19 15:07:49 -07001974 else {
1975 float_s = s;
1976 Py_INCREF(float_s);
1977 }
1978
1979 PyStructSequence_SET_ITEM(v, index, s);
1980 PyStructSequence_SET_ITEM(v, index+3, float_s);
1981 PyStructSequence_SET_ITEM(v, index+6, ns_total);
1982 s = NULL;
1983 float_s = NULL;
1984 ns_total = NULL;
1985exit:
1986 Py_XDECREF(s);
1987 Py_XDECREF(ns_fractional);
1988 Py_XDECREF(s_in_ns);
1989 Py_XDECREF(ns_total);
1990 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001991}
1992
Tim Peters5aa91602002-01-30 05:46:57 +00001993/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001994 (used by posix_stat() and posix_fstat()) */
1995static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01001996_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001997{
Victor Stinner8c62be82010-05-06 00:08:46 +00001998 unsigned long ansec, mnsec, cnsec;
1999 PyObject *v = PyStructSequence_New(&StatResultType);
2000 if (v == NULL)
2001 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00002002
Victor Stinner8c62be82010-05-06 00:08:46 +00002003 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00002004#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00002005 PyStructSequence_SET_ITEM(v, 1,
2006 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00002007#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002008 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00002009#endif
Serhiy Storchaka404fa922013-01-02 18:22:23 +02002010#ifdef MS_WINDOWS
2011 PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002012#else
Serhiy Storchakab2653b32015-01-18 11:12:11 +02002013 PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002014#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002015 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002016#if defined(MS_WINDOWS)
2017 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
2018 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
2019#else
2020 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
2021 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
2022#endif
Fred Drake699f3522000-06-29 21:12:41 +00002023#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00002024 PyStructSequence_SET_ITEM(v, 6,
2025 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00002026#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002027 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00002028#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002029
Martin v. Löwis14694662006-02-03 12:54:16 +00002030#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002031 ansec = st->st_atim.tv_nsec;
2032 mnsec = st->st_mtim.tv_nsec;
2033 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002034#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002035 ansec = st->st_atimespec.tv_nsec;
2036 mnsec = st->st_mtimespec.tv_nsec;
2037 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002038#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002039 ansec = st->st_atime_nsec;
2040 mnsec = st->st_mtime_nsec;
2041 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002042#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002043 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002044#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002045 fill_time(v, 7, st->st_atime, ansec);
2046 fill_time(v, 8, st->st_mtime, mnsec);
2047 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002048
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002049#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002050 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2051 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002052#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002053#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002054 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2055 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002056#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002057#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002058 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2059 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002060#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002061#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002062 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2063 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002064#endif
2065#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002066 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002067 PyObject *val;
2068 unsigned long bsec,bnsec;
2069 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002070#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002071 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002072#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002073 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002074#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002075 if (_stat_float_times) {
2076 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
2077 } else {
2078 val = PyLong_FromLong((long)bsec);
2079 }
2080 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2081 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002082 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002083#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002084#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002085 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2086 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002087#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05002088#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2089 PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX,
2090 PyLong_FromUnsignedLong(st->st_file_attributes));
2091#endif
Fred Drake699f3522000-06-29 21:12:41 +00002092
Victor Stinner8c62be82010-05-06 00:08:46 +00002093 if (PyErr_Occurred()) {
2094 Py_DECREF(v);
2095 return NULL;
2096 }
Fred Drake699f3522000-06-29 21:12:41 +00002097
Victor Stinner8c62be82010-05-06 00:08:46 +00002098 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002099}
2100
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002101/* POSIX methods */
2102
Guido van Rossum94f6f721999-01-06 18:42:14 +00002103
2104static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07002105posix_do_stat(char *function_name, path_t *path,
2106 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002107{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002108 STRUCT_STAT st;
2109 int result;
2110
2111#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2112 if (follow_symlinks_specified(function_name, follow_symlinks))
2113 return NULL;
2114#endif
2115
2116 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2117 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2118 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2119 return NULL;
2120
2121 Py_BEGIN_ALLOW_THREADS
2122 if (path->fd != -1)
2123 result = FSTAT(path->fd, &st);
2124 else
2125#ifdef MS_WINDOWS
2126 if (path->wide) {
2127 if (follow_symlinks)
2128 result = win32_stat_w(path->wide, &st);
2129 else
2130 result = win32_lstat_w(path->wide, &st);
2131 }
2132 else
2133#endif
2134#if defined(HAVE_LSTAT) || defined(MS_WINDOWS)
2135 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2136 result = LSTAT(path->narrow, &st);
2137 else
2138#endif
2139#ifdef HAVE_FSTATAT
2140 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
2141 result = fstatat(dir_fd, path->narrow, &st,
2142 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2143 else
2144#endif
2145 result = STAT(path->narrow, &st);
2146 Py_END_ALLOW_THREADS
2147
Victor Stinner292c8352012-10-30 02:17:38 +01002148 if (result != 0) {
2149 return path_error(path);
2150 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002151
2152 return _pystat_fromstructstat(&st);
2153}
2154
Larry Hastings2f936352014-08-05 14:04:04 +10002155/*[python input]
2156
2157for s in """
2158
2159FACCESSAT
2160FCHMODAT
2161FCHOWNAT
2162FSTATAT
2163LINKAT
2164MKDIRAT
2165MKFIFOAT
2166MKNODAT
2167OPENAT
2168READLINKAT
2169SYMLINKAT
2170UNLINKAT
2171
2172""".strip().split():
2173 s = s.strip()
2174 print("""
2175#ifdef HAVE_{s}
2176 #define {s}_DIR_FD_CONVERTER dir_fd_converter
Larry Hastings31826802013-10-19 00:09:25 -07002177#else
Larry Hastings2f936352014-08-05 14:04:04 +10002178 #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings31826802013-10-19 00:09:25 -07002179#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002180""".rstrip().format(s=s))
2181
2182for s in """
2183
2184FCHDIR
2185FCHMOD
2186FCHOWN
2187FDOPENDIR
2188FEXECVE
2189FPATHCONF
2190FSTATVFS
2191FTRUNCATE
2192
2193""".strip().split():
2194 s = s.strip()
2195 print("""
2196#ifdef HAVE_{s}
2197 #define PATH_HAVE_{s} 1
2198#else
2199 #define PATH_HAVE_{s} 0
2200#endif
2201
2202""".rstrip().format(s=s))
2203[python start generated code]*/
2204
2205#ifdef HAVE_FACCESSAT
2206 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
2207#else
2208 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
2209#endif
2210
2211#ifdef HAVE_FCHMODAT
2212 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
2213#else
2214 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
2215#endif
2216
2217#ifdef HAVE_FCHOWNAT
2218 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
2219#else
2220 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
2221#endif
2222
2223#ifdef HAVE_FSTATAT
2224 #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
2225#else
2226 #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
2227#endif
2228
2229#ifdef HAVE_LINKAT
2230 #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
2231#else
2232 #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2233#endif
2234
2235#ifdef HAVE_MKDIRAT
2236 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
2237#else
2238 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
2239#endif
2240
2241#ifdef HAVE_MKFIFOAT
2242 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
2243#else
2244 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
2245#endif
2246
2247#ifdef HAVE_MKNODAT
2248 #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
2249#else
2250 #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
2251#endif
2252
2253#ifdef HAVE_OPENAT
2254 #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
2255#else
2256 #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
2257#endif
2258
2259#ifdef HAVE_READLINKAT
2260 #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
2261#else
2262 #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2263#endif
2264
2265#ifdef HAVE_SYMLINKAT
2266 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
2267#else
2268 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2269#endif
2270
2271#ifdef HAVE_UNLINKAT
2272 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
2273#else
2274 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2275#endif
2276
2277#ifdef HAVE_FCHDIR
2278 #define PATH_HAVE_FCHDIR 1
2279#else
2280 #define PATH_HAVE_FCHDIR 0
2281#endif
2282
2283#ifdef HAVE_FCHMOD
2284 #define PATH_HAVE_FCHMOD 1
2285#else
2286 #define PATH_HAVE_FCHMOD 0
2287#endif
2288
2289#ifdef HAVE_FCHOWN
2290 #define PATH_HAVE_FCHOWN 1
2291#else
2292 #define PATH_HAVE_FCHOWN 0
2293#endif
2294
2295#ifdef HAVE_FDOPENDIR
2296 #define PATH_HAVE_FDOPENDIR 1
2297#else
2298 #define PATH_HAVE_FDOPENDIR 0
2299#endif
2300
2301#ifdef HAVE_FEXECVE
2302 #define PATH_HAVE_FEXECVE 1
2303#else
2304 #define PATH_HAVE_FEXECVE 0
2305#endif
2306
2307#ifdef HAVE_FPATHCONF
2308 #define PATH_HAVE_FPATHCONF 1
2309#else
2310 #define PATH_HAVE_FPATHCONF 0
2311#endif
2312
2313#ifdef HAVE_FSTATVFS
2314 #define PATH_HAVE_FSTATVFS 1
2315#else
2316 #define PATH_HAVE_FSTATVFS 0
2317#endif
2318
2319#ifdef HAVE_FTRUNCATE
2320 #define PATH_HAVE_FTRUNCATE 1
2321#else
2322 #define PATH_HAVE_FTRUNCATE 0
2323#endif
2324/*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
Larry Hastings31826802013-10-19 00:09:25 -07002325
Steve Dowerfe0a41a2015-03-20 19:50:46 -07002326#ifdef MS_WINDOWS
2327 #undef PATH_HAVE_FTRUNCATE
2328 #define PATH_HAVE_FTRUNCATE 1
2329#endif
Larry Hastings31826802013-10-19 00:09:25 -07002330
Larry Hastings61272b72014-01-07 12:41:53 -08002331/*[python input]
Larry Hastings31826802013-10-19 00:09:25 -07002332
2333class path_t_converter(CConverter):
2334
2335 type = "path_t"
2336 impl_by_reference = True
2337 parse_by_reference = True
2338
2339 converter = 'path_converter'
2340
2341 def converter_init(self, *, allow_fd=False, nullable=False):
Larry Hastings31826802013-10-19 00:09:25 -07002342 # right now path_t doesn't support default values.
2343 # to support a default value, you'll need to override initialize().
Larry Hastings2f936352014-08-05 14:04:04 +10002344 if self.default not in (unspecified, None):
Larry Hastings7726ac92014-01-31 22:03:12 -08002345 fail("Can't specify a default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002346
Larry Hastings2f936352014-08-05 14:04:04 +10002347 if self.c_default not in (None, 'Py_None'):
2348 raise RuntimeError("Can't specify a c_default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002349
2350 self.nullable = nullable
2351 self.allow_fd = allow_fd
2352
Larry Hastings7726ac92014-01-31 22:03:12 -08002353 def pre_render(self):
2354 def strify(value):
Larry Hastings2f936352014-08-05 14:04:04 +10002355 if isinstance(value, str):
2356 return value
Larry Hastings7726ac92014-01-31 22:03:12 -08002357 return str(int(bool(value)))
2358
2359 # add self.py_name here when merging with posixmodule conversion
Larry Hastings2f936352014-08-05 14:04:04 +10002360 self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format(
Larry Hastings31826802013-10-19 00:09:25 -07002361 self.function.name,
Larry Hastings2f936352014-08-05 14:04:04 +10002362 self.name,
Larry Hastings7726ac92014-01-31 22:03:12 -08002363 strify(self.nullable),
2364 strify(self.allow_fd),
Larry Hastings31826802013-10-19 00:09:25 -07002365 )
2366
2367 def cleanup(self):
2368 return "path_cleanup(&" + self.name + ");\n"
2369
2370
2371class dir_fd_converter(CConverter):
2372 type = 'int'
Larry Hastings31826802013-10-19 00:09:25 -07002373
Larry Hastings2f936352014-08-05 14:04:04 +10002374 def converter_init(self, requires=None):
Larry Hastings31826802013-10-19 00:09:25 -07002375 if self.default in (unspecified, None):
2376 self.c_default = 'DEFAULT_DIR_FD'
Larry Hastings2f936352014-08-05 14:04:04 +10002377 if isinstance(requires, str):
2378 self.converter = requires.upper() + '_DIR_FD_CONVERTER'
2379 else:
2380 self.converter = 'dir_fd_converter'
Larry Hastings31826802013-10-19 00:09:25 -07002381
Larry Hastings2f936352014-08-05 14:04:04 +10002382class fildes_converter(CConverter):
2383 type = 'int'
2384 converter = 'fildes_converter'
2385
2386class uid_t_converter(CConverter):
2387 type = "uid_t"
2388 converter = '_Py_Uid_Converter'
2389
2390class gid_t_converter(CConverter):
2391 type = "gid_t"
2392 converter = '_Py_Gid_Converter'
2393
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02002394class dev_t_converter(CConverter):
2395 type = 'dev_t'
2396 converter = '_Py_Dev_Converter'
2397
2398class dev_t_return_converter(unsigned_long_return_converter):
2399 type = 'dev_t'
2400 conversion_fn = '_PyLong_FromDev'
2401 unsigned_cast = '(dev_t)'
2402
Larry Hastings2f936352014-08-05 14:04:04 +10002403class FSConverter_converter(CConverter):
2404 type = 'PyObject *'
2405 converter = 'PyUnicode_FSConverter'
2406 def converter_init(self):
2407 if self.default is not unspecified:
2408 fail("FSConverter_converter does not support default values")
2409 self.c_default = 'NULL'
2410
2411 def cleanup(self):
2412 return "Py_XDECREF(" + self.name + ");\n"
2413
2414class pid_t_converter(CConverter):
2415 type = 'pid_t'
2416 format_unit = '" _Py_PARSE_PID "'
2417
2418class idtype_t_converter(int_converter):
2419 type = 'idtype_t'
2420
2421class id_t_converter(CConverter):
2422 type = 'id_t'
2423 format_unit = '" _Py_PARSE_PID "'
2424
2425class Py_intptr_t_converter(CConverter):
2426 type = 'Py_intptr_t'
2427 format_unit = '" _Py_PARSE_INTPTR "'
2428
2429class Py_off_t_converter(CConverter):
2430 type = 'Py_off_t'
2431 converter = 'Py_off_t_converter'
2432
2433class Py_off_t_return_converter(long_return_converter):
2434 type = 'Py_off_t'
2435 conversion_fn = 'PyLong_FromPy_off_t'
2436
2437class path_confname_converter(CConverter):
2438 type="int"
2439 converter="conv_path_confname"
2440
2441class confstr_confname_converter(path_confname_converter):
2442 converter='conv_confstr_confname'
2443
2444class sysconf_confname_converter(path_confname_converter):
2445 converter="conv_sysconf_confname"
2446
2447class sched_param_converter(CConverter):
2448 type = 'struct sched_param'
2449 converter = 'convert_sched_param'
2450 impl_by_reference = True;
Larry Hastings31826802013-10-19 00:09:25 -07002451
Larry Hastings61272b72014-01-07 12:41:53 -08002452[python start generated code]*/
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02002453/*[python end generated code: output=da39a3ee5e6b4b0d input=affe68316f160401]*/
Larry Hastings31826802013-10-19 00:09:25 -07002454
Larry Hastings61272b72014-01-07 12:41:53 -08002455/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002456
Larry Hastings2a727912014-01-16 11:32:01 -08002457os.stat
Larry Hastings31826802013-10-19 00:09:25 -07002458
2459 path : path_t(allow_fd=True)
2460 Path to be examined; can be string, bytes, or open-file-descriptor int.
2461
2462 *
2463
Larry Hastings2f936352014-08-05 14:04:04 +10002464 dir_fd : dir_fd(requires='fstatat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002465 If not None, it should be a file descriptor open to a directory,
2466 and path should be a relative string; path will then be relative to
2467 that directory.
2468
2469 follow_symlinks: bool = True
2470 If False, and the last element of the path is a symbolic link,
2471 stat will examine the symbolic link itself instead of the file
2472 the link points to.
2473
2474Perform a stat system call on the given path.
2475
2476dir_fd and follow_symlinks may not be implemented
2477 on your platform. If they are unavailable, using them will raise a
2478 NotImplementedError.
2479
2480It's an error to use dir_fd or follow_symlinks when specifying path as
2481 an open file descriptor.
2482
Larry Hastings61272b72014-01-07 12:41:53 -08002483[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002484
Larry Hastings31826802013-10-19 00:09:25 -07002485static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002486os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
2487/*[clinic end generated code: output=7d4976e6f18a59c5 input=099d356c306fa24a]*/
Larry Hastings31826802013-10-19 00:09:25 -07002488{
2489 return posix_do_stat("stat", path, dir_fd, follow_symlinks);
2490}
2491
Larry Hastings2f936352014-08-05 14:04:04 +10002492
2493/*[clinic input]
2494os.lstat
2495
2496 path : path_t
2497
2498 *
2499
2500 dir_fd : dir_fd(requires='fstatat') = None
2501
2502Perform a stat system call on the given path, without following symbolic links.
2503
2504Like stat(), but do not follow symbolic links.
2505Equivalent to stat(path, follow_symlinks=False).
2506[clinic start generated code]*/
2507
Larry Hastings2f936352014-08-05 14:04:04 +10002508static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002509os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
2510/*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002511{
2512 int follow_symlinks = 0;
2513 return posix_do_stat("lstat", path, dir_fd, follow_symlinks);
2514}
Larry Hastings31826802013-10-19 00:09:25 -07002515
Larry Hastings2f936352014-08-05 14:04:04 +10002516
Larry Hastings61272b72014-01-07 12:41:53 -08002517/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10002518os.access -> bool
Larry Hastings31826802013-10-19 00:09:25 -07002519
2520 path: path_t(allow_fd=True)
2521 Path to be tested; can be string, bytes, or open-file-descriptor int.
2522
2523 mode: int
2524 Operating-system mode bitfield. Can be F_OK to test existence,
2525 or the inclusive-OR of R_OK, W_OK, and X_OK.
2526
2527 *
2528
Larry Hastings2f936352014-08-05 14:04:04 +10002529 dir_fd : dir_fd(requires='faccessat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002530 If not None, it should be a file descriptor open to a directory,
2531 and path should be relative; path will then be relative to that
2532 directory.
2533
2534 effective_ids: bool = False
2535 If True, access will use the effective uid/gid instead of
2536 the real uid/gid.
2537
2538 follow_symlinks: bool = True
2539 If False, and the last element of the path is a symbolic link,
2540 access will examine the symbolic link itself instead of the file
2541 the link points to.
2542
2543Use the real uid/gid to test for access to a path.
2544
2545{parameters}
2546dir_fd, effective_ids, and follow_symlinks may not be implemented
2547 on your platform. If they are unavailable, using them will raise a
2548 NotImplementedError.
2549
2550Note that most operations will use the effective uid/gid, therefore this
2551 routine can be used in a suid/sgid environment to test if the invoking user
2552 has the specified access to the path.
2553
Larry Hastings61272b72014-01-07 12:41:53 -08002554[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002555
Larry Hastings2f936352014-08-05 14:04:04 +10002556static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002557os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002558 int effective_ids, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002559/*[clinic end generated code: output=cf84158bc90b1a77 input=b75a756797af45ec]*/
Larry Hastings31826802013-10-19 00:09:25 -07002560{
Larry Hastings2f936352014-08-05 14:04:04 +10002561 int return_value;
Victor Stinner8c62be82010-05-06 00:08:46 +00002562
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002563#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002564 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002565#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002566 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002567#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002568
Larry Hastings9cf065c2012-06-22 16:30:09 -07002569#ifndef HAVE_FACCESSAT
2570 if (follow_symlinks_specified("access", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002571 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002572
2573 if (effective_ids) {
2574 argument_unavailable_error("access", "effective_ids");
Larry Hastings2f936352014-08-05 14:04:04 +10002575 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002576 }
2577#endif
2578
2579#ifdef MS_WINDOWS
2580 Py_BEGIN_ALLOW_THREADS
Christian Heimesebe83f92013-10-19 22:36:17 +02002581 if (path->wide != NULL)
2582 attr = GetFileAttributesW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002583 else
Christian Heimesebe83f92013-10-19 22:36:17 +02002584 attr = GetFileAttributesA(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002585 Py_END_ALLOW_THREADS
2586
2587 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002588 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002589 * * we didn't get a -1, and
2590 * * write access wasn't requested,
2591 * * or the file isn't read-only,
2592 * * or it's a directory.
2593 * (Directories cannot be read-only on Windows.)
2594 */
Larry Hastings2f936352014-08-05 14:04:04 +10002595 return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002596 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002597 !(attr & FILE_ATTRIBUTE_READONLY) ||
Larry Hastings2f936352014-08-05 14:04:04 +10002598 (attr & FILE_ATTRIBUTE_DIRECTORY));
Larry Hastings9cf065c2012-06-22 16:30:09 -07002599#else
2600
2601 Py_BEGIN_ALLOW_THREADS
2602#ifdef HAVE_FACCESSAT
2603 if ((dir_fd != DEFAULT_DIR_FD) ||
2604 effective_ids ||
2605 !follow_symlinks) {
2606 int flags = 0;
2607 if (!follow_symlinks)
2608 flags |= AT_SYMLINK_NOFOLLOW;
2609 if (effective_ids)
2610 flags |= AT_EACCESS;
Larry Hastings31826802013-10-19 00:09:25 -07002611 result = faccessat(dir_fd, path->narrow, mode, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002612 }
2613 else
2614#endif
Larry Hastings31826802013-10-19 00:09:25 -07002615 result = access(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002616 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10002617 return_value = !result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002618#endif
2619
Larry Hastings9cf065c2012-06-22 16:30:09 -07002620 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002621}
2622
Guido van Rossumd371ff11999-01-25 16:12:23 +00002623#ifndef F_OK
2624#define F_OK 0
2625#endif
2626#ifndef R_OK
2627#define R_OK 4
2628#endif
2629#ifndef W_OK
2630#define W_OK 2
2631#endif
2632#ifndef X_OK
2633#define X_OK 1
2634#endif
2635
Larry Hastings31826802013-10-19 00:09:25 -07002636
Guido van Rossumd371ff11999-01-25 16:12:23 +00002637#ifdef HAVE_TTYNAME
Larry Hastings61272b72014-01-07 12:41:53 -08002638/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002639os.ttyname -> DecodeFSDefault
2640
2641 fd: int
2642 Integer file descriptor handle.
2643
2644 /
2645
2646Return the name of the terminal device connected to 'fd'.
Larry Hastings61272b72014-01-07 12:41:53 -08002647[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002648
Larry Hastings31826802013-10-19 00:09:25 -07002649static char *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002650os_ttyname_impl(PyObject *module, int fd)
2651/*[clinic end generated code: output=ed16ad216d813591 input=5f72ca83e76b3b45]*/
Larry Hastings31826802013-10-19 00:09:25 -07002652{
2653 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002654
Larry Hastings31826802013-10-19 00:09:25 -07002655 ret = ttyname(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00002656 if (ret == NULL)
Larry Hastings31826802013-10-19 00:09:25 -07002657 posix_error();
2658 return ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002659}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002660#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002661
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002662#ifdef HAVE_CTERMID
Larry Hastings2f936352014-08-05 14:04:04 +10002663/*[clinic input]
2664os.ctermid
2665
2666Return the name of the controlling terminal for this process.
2667[clinic start generated code]*/
2668
Larry Hastings2f936352014-08-05 14:04:04 +10002669static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002670os_ctermid_impl(PyObject *module)
2671/*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002672{
Victor Stinner8c62be82010-05-06 00:08:46 +00002673 char *ret;
2674 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002675
Greg Wardb48bc172000-03-01 21:51:56 +00002676#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002677 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002678#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002679 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002680#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002681 if (ret == NULL)
2682 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002683 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002684}
Larry Hastings2f936352014-08-05 14:04:04 +10002685#endif /* HAVE_CTERMID */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002686
Larry Hastings2f936352014-08-05 14:04:04 +10002687
2688/*[clinic input]
2689os.chdir
2690
2691 path: path_t(allow_fd='PATH_HAVE_FCHDIR')
2692
2693Change the current working directory to the specified path.
2694
2695path may always be specified as a string.
2696On some platforms, path may also be specified as an open file descriptor.
2697 If this functionality is unavailable, using it raises an exception.
2698[clinic start generated code]*/
2699
Larry Hastings2f936352014-08-05 14:04:04 +10002700static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002701os_chdir_impl(PyObject *module, path_t *path)
2702/*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002703{
2704 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002705
2706 Py_BEGIN_ALLOW_THREADS
2707#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10002708 if (path->wide)
2709 result = win32_wchdir(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002710 else
Larry Hastings2f936352014-08-05 14:04:04 +10002711 result = win32_chdir(path->narrow);
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03002712 result = !result; /* on unix, success = 0, on windows, success = !0 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002713#else
2714#ifdef HAVE_FCHDIR
Larry Hastings2f936352014-08-05 14:04:04 +10002715 if (path->fd != -1)
2716 result = fchdir(path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002717 else
2718#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002719 result = chdir(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002720#endif
2721 Py_END_ALLOW_THREADS
2722
2723 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002724 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002725 }
2726
Larry Hastings2f936352014-08-05 14:04:04 +10002727 Py_RETURN_NONE;
2728}
2729
2730
2731#ifdef HAVE_FCHDIR
2732/*[clinic input]
2733os.fchdir
2734
2735 fd: fildes
2736
2737Change to the directory of the given file descriptor.
2738
2739fd must be opened on a directory, not a file.
2740Equivalent to os.chdir(fd).
2741
2742[clinic start generated code]*/
2743
Fred Drake4d1e64b2002-04-15 19:40:07 +00002744static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002745os_fchdir_impl(PyObject *module, int fd)
2746/*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
Fred Drake4d1e64b2002-04-15 19:40:07 +00002747{
Larry Hastings2f936352014-08-05 14:04:04 +10002748 return posix_fildes_fd(fd, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002749}
2750#endif /* HAVE_FCHDIR */
2751
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002752
Larry Hastings2f936352014-08-05 14:04:04 +10002753/*[clinic input]
2754os.chmod
2755
2756 path: path_t(allow_fd='PATH_HAVE_FCHMOD')
2757 Path to be modified. May always be specified as a str or bytes.
2758 On some platforms, path may also be specified as an open file descriptor.
2759 If this functionality is unavailable, using it raises an exception.
2760
2761 mode: int
2762 Operating-system mode bitfield.
2763
2764 *
2765
2766 dir_fd : dir_fd(requires='fchmodat') = None
2767 If not None, it should be a file descriptor open to a directory,
2768 and path should be relative; path will then be relative to that
2769 directory.
2770
2771 follow_symlinks: bool = True
2772 If False, and the last element of the path is a symbolic link,
2773 chmod will modify the symbolic link itself instead of the file
2774 the link points to.
2775
2776Change the access permissions of a file.
2777
2778It is an error to use dir_fd or follow_symlinks when specifying path as
2779 an open file descriptor.
2780dir_fd and follow_symlinks may not be implemented on your platform.
2781 If they are unavailable, using them will raise a NotImplementedError.
2782
2783[clinic start generated code]*/
2784
Larry Hastings2f936352014-08-05 14:04:04 +10002785static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002786os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002787 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002788/*[clinic end generated code: output=5cf6a94915cc7bff input=7f1618e5e15cc196]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002789{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002790 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002791
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002792#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002793 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002794#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002795
Larry Hastings9cf065c2012-06-22 16:30:09 -07002796#ifdef HAVE_FCHMODAT
2797 int fchmodat_nofollow_unsupported = 0;
2798#endif
2799
Larry Hastings9cf065c2012-06-22 16:30:09 -07002800#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
2801 if (follow_symlinks_specified("chmod", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002802 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002803#endif
2804
2805#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002806 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10002807 if (path->wide)
2808 attr = GetFileAttributesW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002809 else
Larry Hastings2f936352014-08-05 14:04:04 +10002810 attr = GetFileAttributesA(path->narrow);
Tim Golden23005082013-10-25 11:22:37 +01002811 if (attr == INVALID_FILE_ATTRIBUTES)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002812 result = 0;
2813 else {
2814 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00002815 attr &= ~FILE_ATTRIBUTE_READONLY;
2816 else
2817 attr |= FILE_ATTRIBUTE_READONLY;
Larry Hastings2f936352014-08-05 14:04:04 +10002818 if (path->wide)
2819 result = SetFileAttributesW(path->wide, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002820 else
Larry Hastings2f936352014-08-05 14:04:04 +10002821 result = SetFileAttributesA(path->narrow, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002822 }
2823 Py_END_ALLOW_THREADS
2824
2825 if (!result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002826 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002827 }
2828#else /* MS_WINDOWS */
2829 Py_BEGIN_ALLOW_THREADS
2830#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002831 if (path->fd != -1)
2832 result = fchmod(path->fd, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002833 else
2834#endif
2835#ifdef HAVE_LCHMOD
2836 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10002837 result = lchmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002838 else
2839#endif
2840#ifdef HAVE_FCHMODAT
2841 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
2842 /*
2843 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
2844 * The documentation specifically shows how to use it,
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002845 * and then says it isn't implemented yet.
2846 * (true on linux with glibc 2.15, and openindiana 3.x)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002847 *
2848 * Once it is supported, os.chmod will automatically
2849 * support dir_fd and follow_symlinks=False. (Hopefully.)
2850 * Until then, we need to be careful what exception we raise.
2851 */
Larry Hastings2f936352014-08-05 14:04:04 +10002852 result = fchmodat(dir_fd, path->narrow, mode,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002853 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2854 /*
2855 * But wait! We can't throw the exception without allowing threads,
2856 * and we can't do that in this nested scope. (Macro trickery, sigh.)
2857 */
2858 fchmodat_nofollow_unsupported =
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002859 result &&
2860 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
2861 !follow_symlinks;
Victor Stinner8c62be82010-05-06 00:08:46 +00002862 }
2863 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002864#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002865 result = chmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002866 Py_END_ALLOW_THREADS
2867
2868 if (result) {
2869#ifdef HAVE_FCHMODAT
2870 if (fchmodat_nofollow_unsupported) {
2871 if (dir_fd != DEFAULT_DIR_FD)
2872 dir_fd_and_follow_symlinks_invalid("chmod",
2873 dir_fd, follow_symlinks);
2874 else
2875 follow_symlinks_specified("chmod", follow_symlinks);
2876 }
2877 else
2878#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002879 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002880 }
2881#endif
2882
Larry Hastings2f936352014-08-05 14:04:04 +10002883 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002884}
2885
Larry Hastings9cf065c2012-06-22 16:30:09 -07002886
Christian Heimes4e30a842007-11-30 22:12:06 +00002887#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002888/*[clinic input]
2889os.fchmod
2890
2891 fd: int
2892 mode: int
2893
2894Change the access permissions of the file given by file descriptor fd.
2895
2896Equivalent to os.chmod(fd, mode).
2897[clinic start generated code]*/
2898
Larry Hastings2f936352014-08-05 14:04:04 +10002899static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002900os_fchmod_impl(PyObject *module, int fd, int mode)
2901/*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002902{
2903 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002904 int async_err = 0;
2905
2906 do {
2907 Py_BEGIN_ALLOW_THREADS
2908 res = fchmod(fd, mode);
2909 Py_END_ALLOW_THREADS
2910 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
2911 if (res != 0)
2912 return (!async_err) ? posix_error() : NULL;
2913
Victor Stinner8c62be82010-05-06 00:08:46 +00002914 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002915}
2916#endif /* HAVE_FCHMOD */
2917
Larry Hastings2f936352014-08-05 14:04:04 +10002918
Christian Heimes4e30a842007-11-30 22:12:06 +00002919#ifdef HAVE_LCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002920/*[clinic input]
2921os.lchmod
2922
2923 path: path_t
2924 mode: int
2925
2926Change the access permissions of a file, without following symbolic links.
2927
2928If path is a symlink, this affects the link itself rather than the target.
2929Equivalent to chmod(path, mode, follow_symlinks=False)."
2930[clinic start generated code]*/
2931
Larry Hastings2f936352014-08-05 14:04:04 +10002932static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002933os_lchmod_impl(PyObject *module, path_t *path, int mode)
2934/*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002935{
Victor Stinner8c62be82010-05-06 00:08:46 +00002936 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00002937 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10002938 res = lchmod(path->narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00002939 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01002940 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10002941 path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01002942 return NULL;
2943 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002944 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002945}
2946#endif /* HAVE_LCHMOD */
2947
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002948
Thomas Wouterscf297e42007-02-23 15:07:44 +00002949#ifdef HAVE_CHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10002950/*[clinic input]
2951os.chflags
2952
2953 path: path_t
2954 flags: unsigned_long(bitwise=True)
2955 follow_symlinks: bool=True
2956
2957Set file flags.
2958
2959If follow_symlinks is False, and the last element of the path is a symbolic
2960 link, chflags will change flags on the symbolic link itself instead of the
2961 file the link points to.
2962follow_symlinks may not be implemented on your platform. If it is
2963unavailable, using it will raise a NotImplementedError.
2964
2965[clinic start generated code]*/
2966
Larry Hastings2f936352014-08-05 14:04:04 +10002967static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002968os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
Larry Hastings89964c42015-04-14 18:07:59 -04002969 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002970/*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002971{
2972 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002973
2974#ifndef HAVE_LCHFLAGS
2975 if (follow_symlinks_specified("chflags", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002976 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002977#endif
2978
Victor Stinner8c62be82010-05-06 00:08:46 +00002979 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002980#ifdef HAVE_LCHFLAGS
2981 if (!follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +10002982 result = lchflags(path->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002983 else
2984#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002985 result = chflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00002986 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002987
Larry Hastings2f936352014-08-05 14:04:04 +10002988 if (result)
2989 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002990
Larry Hastings2f936352014-08-05 14:04:04 +10002991 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002992}
2993#endif /* HAVE_CHFLAGS */
2994
Larry Hastings2f936352014-08-05 14:04:04 +10002995
Thomas Wouterscf297e42007-02-23 15:07:44 +00002996#ifdef HAVE_LCHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10002997/*[clinic input]
2998os.lchflags
2999
3000 path: path_t
3001 flags: unsigned_long(bitwise=True)
3002
3003Set file flags.
3004
3005This function will not follow symbolic links.
3006Equivalent to chflags(path, flags, follow_symlinks=False).
3007[clinic start generated code]*/
3008
Larry Hastings2f936352014-08-05 14:04:04 +10003009static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003010os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
3011/*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003012{
Victor Stinner8c62be82010-05-06 00:08:46 +00003013 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003014 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003015 res = lchflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003016 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003017 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003018 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003019 }
Victor Stinner292c8352012-10-30 02:17:38 +01003020 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003021}
3022#endif /* HAVE_LCHFLAGS */
3023
Larry Hastings2f936352014-08-05 14:04:04 +10003024
Martin v. Löwis244edc82001-10-04 22:44:26 +00003025#ifdef HAVE_CHROOT
Larry Hastings2f936352014-08-05 14:04:04 +10003026/*[clinic input]
3027os.chroot
3028 path: path_t
3029
3030Change root directory to path.
3031
3032[clinic start generated code]*/
3033
Larry Hastings2f936352014-08-05 14:04:04 +10003034static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003035os_chroot_impl(PyObject *module, path_t *path)
3036/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003037{
3038 int res;
3039 Py_BEGIN_ALLOW_THREADS
3040 res = chroot(path->narrow);
3041 Py_END_ALLOW_THREADS
3042 if (res < 0)
3043 return path_error(path);
3044 Py_RETURN_NONE;
3045}
3046#endif /* HAVE_CHROOT */
3047
Martin v. Löwis244edc82001-10-04 22:44:26 +00003048
Guido van Rossum21142a01999-01-08 21:05:37 +00003049#ifdef HAVE_FSYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003050/*[clinic input]
3051os.fsync
3052
3053 fd: fildes
3054
3055Force write of fd to disk.
3056[clinic start generated code]*/
3057
Larry Hastings2f936352014-08-05 14:04:04 +10003058static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003059os_fsync_impl(PyObject *module, int fd)
3060/*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003061{
3062 return posix_fildes_fd(fd, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003063}
3064#endif /* HAVE_FSYNC */
3065
Larry Hastings2f936352014-08-05 14:04:04 +10003066
Ross Lagerwall7807c352011-03-17 20:20:30 +02003067#ifdef HAVE_SYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003068/*[clinic input]
3069os.sync
3070
3071Force write of everything to disk.
3072[clinic start generated code]*/
3073
Larry Hastings2f936352014-08-05 14:04:04 +10003074static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003075os_sync_impl(PyObject *module)
3076/*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
Ross Lagerwall7807c352011-03-17 20:20:30 +02003077{
3078 Py_BEGIN_ALLOW_THREADS
3079 sync();
3080 Py_END_ALLOW_THREADS
3081 Py_RETURN_NONE;
3082}
Larry Hastings2f936352014-08-05 14:04:04 +10003083#endif /* HAVE_SYNC */
3084
Ross Lagerwall7807c352011-03-17 20:20:30 +02003085
Guido van Rossum21142a01999-01-08 21:05:37 +00003086#ifdef HAVE_FDATASYNC
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00003087#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00003088extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
3089#endif
3090
Larry Hastings2f936352014-08-05 14:04:04 +10003091/*[clinic input]
3092os.fdatasync
3093
3094 fd: fildes
3095
3096Force write of fd to disk without forcing update of metadata.
3097[clinic start generated code]*/
3098
Larry Hastings2f936352014-08-05 14:04:04 +10003099static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003100os_fdatasync_impl(PyObject *module, int fd)
3101/*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003102{
3103 return posix_fildes_fd(fd, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003104}
3105#endif /* HAVE_FDATASYNC */
3106
3107
Fredrik Lundh10723342000-07-10 16:38:09 +00003108#ifdef HAVE_CHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003109/*[clinic input]
3110os.chown
3111
3112 path : path_t(allow_fd='PATH_HAVE_FCHOWN')
3113 Path to be examined; can be string, bytes, or open-file-descriptor int.
3114
3115 uid: uid_t
3116
3117 gid: gid_t
3118
3119 *
3120
3121 dir_fd : dir_fd(requires='fchownat') = None
3122 If not None, it should be a file descriptor open to a directory,
3123 and path should be relative; path will then be relative to that
3124 directory.
3125
3126 follow_symlinks: bool = True
3127 If False, and the last element of the path is a symbolic link,
3128 stat will examine the symbolic link itself instead of the file
3129 the link points to.
3130
3131Change the owner and group id of path to the numeric uid and gid.\
3132
3133path may always be specified as a string.
3134On some platforms, path may also be specified as an open file descriptor.
3135 If this functionality is unavailable, using it raises an exception.
3136If dir_fd is not None, it should be a file descriptor open to a directory,
3137 and path should be relative; path will then be relative to that directory.
3138If follow_symlinks is False, and the last element of the path is a symbolic
3139 link, chown will modify the symbolic link itself instead of the file the
3140 link points to.
3141It is an error to use dir_fd or follow_symlinks when specifying path as
3142 an open file descriptor.
3143dir_fd and follow_symlinks may not be implemented on your platform.
3144 If they are unavailable, using them will raise a NotImplementedError.
3145
3146[clinic start generated code]*/
3147
Larry Hastings2f936352014-08-05 14:04:04 +10003148static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003149os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
Larry Hastings89964c42015-04-14 18:07:59 -04003150 int dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003151/*[clinic end generated code: output=4beadab0db5f70cd input=a61cc35574814d5d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003152{
3153 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003154
3155#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
3156 if (follow_symlinks_specified("chown", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003157 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003158#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003159 if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
3160 fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
3161 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003162
3163#ifdef __APPLE__
3164 /*
3165 * This is for Mac OS X 10.3, which doesn't have lchown.
3166 * (But we still have an lchown symbol because of weak-linking.)
3167 * It doesn't have fchownat either. So there's no possibility
3168 * of a graceful failover.
Georg Brandlf7875592012-06-24 13:58:31 +02003169 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003170 if ((!follow_symlinks) && (lchown == NULL)) {
3171 follow_symlinks_specified("chown", follow_symlinks);
Larry Hastings2f936352014-08-05 14:04:04 +10003172 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003173 }
3174#endif
3175
Victor Stinner8c62be82010-05-06 00:08:46 +00003176 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003177#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003178 if (path->fd != -1)
3179 result = fchown(path->fd, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003180 else
3181#endif
3182#ifdef HAVE_LCHOWN
3183 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003184 result = lchown(path->narrow, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003185 else
3186#endif
3187#ifdef HAVE_FCHOWNAT
3188 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003189 result = fchownat(dir_fd, path->narrow, uid, gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003190 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3191 else
3192#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003193 result = chown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003194 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003195
Larry Hastings2f936352014-08-05 14:04:04 +10003196 if (result)
3197 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003198
Larry Hastings2f936352014-08-05 14:04:04 +10003199 Py_RETURN_NONE;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003200}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003201#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003202
Larry Hastings2f936352014-08-05 14:04:04 +10003203
Christian Heimes4e30a842007-11-30 22:12:06 +00003204#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003205/*[clinic input]
3206os.fchown
3207
3208 fd: int
3209 uid: uid_t
3210 gid: gid_t
3211
3212Change the owner and group id of the file specified by file descriptor.
3213
3214Equivalent to os.chown(fd, uid, gid).
3215
3216[clinic start generated code]*/
3217
Larry Hastings2f936352014-08-05 14:04:04 +10003218static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003219os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
3220/*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003221{
Victor Stinner8c62be82010-05-06 00:08:46 +00003222 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003223 int async_err = 0;
3224
3225 do {
3226 Py_BEGIN_ALLOW_THREADS
3227 res = fchown(fd, uid, gid);
3228 Py_END_ALLOW_THREADS
3229 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3230 if (res != 0)
3231 return (!async_err) ? posix_error() : NULL;
3232
Victor Stinner8c62be82010-05-06 00:08:46 +00003233 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003234}
3235#endif /* HAVE_FCHOWN */
3236
Larry Hastings2f936352014-08-05 14:04:04 +10003237
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003238#ifdef HAVE_LCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003239/*[clinic input]
3240os.lchown
3241
3242 path : path_t
3243 uid: uid_t
3244 gid: gid_t
3245
3246Change the owner and group id of path to the numeric uid and gid.
3247
3248This function will not follow symbolic links.
3249Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
3250[clinic start generated code]*/
3251
Larry Hastings2f936352014-08-05 14:04:04 +10003252static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003253os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
3254/*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003255{
Victor Stinner8c62be82010-05-06 00:08:46 +00003256 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003257 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003258 res = lchown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003259 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003260 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003261 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003262 }
Larry Hastings2f936352014-08-05 14:04:04 +10003263 Py_RETURN_NONE;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003264}
3265#endif /* HAVE_LCHOWN */
3266
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003267
Barry Warsaw53699e91996-12-10 23:23:01 +00003268static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003269posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003270{
Victor Stinner4403d7d2015-04-25 00:16:10 +02003271 char *buf, *tmpbuf;
3272 char *cwd;
3273 const size_t chunk = 1024;
3274 size_t buflen = 0;
3275 PyObject *obj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003276
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003277#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003278 if (!use_bytes) {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003279 wchar_t wbuf[MAXPATHLEN];
Victor Stinner8c62be82010-05-06 00:08:46 +00003280 wchar_t *wbuf2 = wbuf;
3281 PyObject *resobj;
3282 DWORD len;
3283 Py_BEGIN_ALLOW_THREADS
Victor Stinner75875072013-11-24 19:23:25 +01003284 len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003285 /* If the buffer is large enough, len does not include the
3286 terminating \0. If the buffer is too small, len includes
3287 the space needed for the terminator. */
Victor Stinner75875072013-11-24 19:23:25 +01003288 if (len >= Py_ARRAY_LENGTH(wbuf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003289 wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003290 if (wbuf2)
3291 len = GetCurrentDirectoryW(len, wbuf2);
3292 }
3293 Py_END_ALLOW_THREADS
3294 if (!wbuf2) {
3295 PyErr_NoMemory();
3296 return NULL;
3297 }
3298 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003299 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003300 PyMem_RawFree(wbuf2);
Victor Stinnerb024e842012-10-31 22:24:06 +01003301 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003302 }
3303 resobj = PyUnicode_FromWideChar(wbuf2, len);
Victor Stinnerb024e842012-10-31 22:24:06 +01003304 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003305 PyMem_RawFree(wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003306 return resobj;
3307 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003308
3309 if (win32_warn_bytes_api())
3310 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003311#endif
3312
Victor Stinner4403d7d2015-04-25 00:16:10 +02003313 buf = cwd = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003314 Py_BEGIN_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003315 do {
3316 buflen += chunk;
3317 tmpbuf = PyMem_RawRealloc(buf, buflen);
3318 if (tmpbuf == NULL)
3319 break;
3320
3321 buf = tmpbuf;
3322 cwd = getcwd(buf, buflen);
3323 } while (cwd == NULL && errno == ERANGE);
Victor Stinner8c62be82010-05-06 00:08:46 +00003324 Py_END_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003325
3326 if (cwd == NULL) {
3327 PyMem_RawFree(buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003328 return posix_error();
Victor Stinner4403d7d2015-04-25 00:16:10 +02003329 }
3330
Victor Stinner8c62be82010-05-06 00:08:46 +00003331 if (use_bytes)
Victor Stinner4403d7d2015-04-25 00:16:10 +02003332 obj = PyBytes_FromStringAndSize(buf, strlen(buf));
3333 else
3334 obj = PyUnicode_DecodeFSDefault(buf);
3335 PyMem_RawFree(buf);
3336
3337 return obj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003338}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003339
Larry Hastings2f936352014-08-05 14:04:04 +10003340
3341/*[clinic input]
3342os.getcwd
3343
3344Return a unicode string representing the current working directory.
3345[clinic start generated code]*/
3346
Larry Hastings2f936352014-08-05 14:04:04 +10003347static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003348os_getcwd_impl(PyObject *module)
3349/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003350{
3351 return posix_getcwd(0);
3352}
3353
Larry Hastings2f936352014-08-05 14:04:04 +10003354
3355/*[clinic input]
3356os.getcwdb
3357
3358Return a bytes string representing the current working directory.
3359[clinic start generated code]*/
3360
Larry Hastings2f936352014-08-05 14:04:04 +10003361static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003362os_getcwdb_impl(PyObject *module)
3363/*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003364{
3365 return posix_getcwd(1);
3366}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003367
Larry Hastings2f936352014-08-05 14:04:04 +10003368
Larry Hastings9cf065c2012-06-22 16:30:09 -07003369#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3370#define HAVE_LINK 1
3371#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003372
Guido van Rossumb6775db1994-08-01 11:34:53 +00003373#ifdef HAVE_LINK
Larry Hastings2f936352014-08-05 14:04:04 +10003374/*[clinic input]
3375
3376os.link
3377
3378 src : path_t
3379 dst : path_t
3380 *
3381 src_dir_fd : dir_fd = None
3382 dst_dir_fd : dir_fd = None
3383 follow_symlinks: bool = True
3384
3385Create a hard link to a file.
3386
3387If either src_dir_fd or dst_dir_fd is not None, it should be a file
3388 descriptor open to a directory, and the respective path string (src or dst)
3389 should be relative; the path will then be relative to that directory.
3390If follow_symlinks is False, and the last element of src is a symbolic
3391 link, link will create a link to the symbolic link itself instead of the
3392 file the link points to.
3393src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
3394 platform. If they are unavailable, using them will raise a
3395 NotImplementedError.
3396[clinic start generated code]*/
3397
Larry Hastings2f936352014-08-05 14:04:04 +10003398static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003399os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003400 int dst_dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003401/*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003402{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003403#ifdef MS_WINDOWS
3404 BOOL result;
3405#else
3406 int result;
3407#endif
3408
Larry Hastings9cf065c2012-06-22 16:30:09 -07003409#ifndef HAVE_LINKAT
3410 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3411 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10003412 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003413 }
3414#endif
3415
Larry Hastings2f936352014-08-05 14:04:04 +10003416 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003417 PyErr_SetString(PyExc_NotImplementedError,
3418 "link: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10003419 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003420 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003421
Brian Curtin1b9df392010-11-24 20:24:31 +00003422#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003423 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003424 if (src->wide)
3425 result = CreateHardLinkW(dst->wide, src->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003426 else
Larry Hastings2f936352014-08-05 14:04:04 +10003427 result = CreateHardLinkA(dst->narrow, src->narrow, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003428 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003429
Larry Hastings2f936352014-08-05 14:04:04 +10003430 if (!result)
3431 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003432#else
3433 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003434#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003435 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3436 (dst_dir_fd != DEFAULT_DIR_FD) ||
3437 (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003438 result = linkat(src_dir_fd, src->narrow,
3439 dst_dir_fd, dst->narrow,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003440 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3441 else
3442#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003443 result = link(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003444 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003445
Larry Hastings2f936352014-08-05 14:04:04 +10003446 if (result)
3447 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003448#endif
3449
Larry Hastings2f936352014-08-05 14:04:04 +10003450 Py_RETURN_NONE;
Brian Curtin1b9df392010-11-24 20:24:31 +00003451}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003452#endif
3453
Brian Curtin1b9df392010-11-24 20:24:31 +00003454
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003455#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003456static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003457_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003458{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003459 PyObject *v;
3460 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3461 BOOL result;
3462 WIN32_FIND_DATA FileData;
Victor Stinner75875072013-11-24 19:23:25 +01003463 char namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003464 char *bufptr = namebuf;
3465 /* only claim to have space for MAX_PATH */
Victor Stinner75875072013-11-24 19:23:25 +01003466 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003467 PyObject *po = NULL;
3468 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003469
Gregory P. Smith40a21602013-03-20 20:52:50 -07003470 if (!path->narrow) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003471 WIN32_FIND_DATAW wFileData;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003472 wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003473
Gregory P. Smith40a21602013-03-20 20:52:50 -07003474 if (!path->wide) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00003475 po_wchars = L".";
3476 len = 1;
3477 } else {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003478 po_wchars = path->wide;
3479 len = wcslen(path->wide);
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00003480 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003481 /* The +5 is so we can append "\\*.*\0" */
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02003482 wnamebuf = PyMem_New(wchar_t, len + 5);
Victor Stinner8c62be82010-05-06 00:08:46 +00003483 if (!wnamebuf) {
3484 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003485 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003486 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00003487 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003488 if (len > 0) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003489 wchar_t wch = wnamebuf[len-1];
Tim Golden781bbeb2013-10-25 20:24:06 +01003490 if (wch != SEP && wch != ALTSEP && wch != L':')
3491 wnamebuf[len++] = SEP;
Victor Stinner8c62be82010-05-06 00:08:46 +00003492 wcscpy(wnamebuf + len, L"*.*");
3493 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003494 if ((list = PyList_New(0)) == NULL) {
3495 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003496 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00003497 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003498 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003499 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003500 if (hFindFile == INVALID_HANDLE_VALUE) {
3501 int error = GetLastError();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003502 if (error == ERROR_FILE_NOT_FOUND)
3503 goto exit;
3504 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003505 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003506 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003507 }
3508 do {
3509 /* Skip over . and .. */
3510 if (wcscmp(wFileData.cFileName, L".") != 0 &&
3511 wcscmp(wFileData.cFileName, L"..") != 0) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003512 v = PyUnicode_FromWideChar(wFileData.cFileName,
3513 wcslen(wFileData.cFileName));
Victor Stinner8c62be82010-05-06 00:08:46 +00003514 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003515 Py_DECREF(list);
3516 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003517 break;
3518 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003519 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003520 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003521 Py_DECREF(list);
3522 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003523 break;
3524 }
3525 Py_DECREF(v);
3526 }
3527 Py_BEGIN_ALLOW_THREADS
3528 result = FindNextFileW(hFindFile, &wFileData);
3529 Py_END_ALLOW_THREADS
3530 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3531 it got to the end of the directory. */
3532 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003533 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003534 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003535 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003536 }
3537 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003538
Larry Hastings9cf065c2012-06-22 16:30:09 -07003539 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003540 }
Gregory P. Smith40a21602013-03-20 20:52:50 -07003541 strcpy(namebuf, path->narrow);
3542 len = path->length;
Victor Stinner8c62be82010-05-06 00:08:46 +00003543 if (len > 0) {
3544 char ch = namebuf[len-1];
Tim Golden781bbeb2013-10-25 20:24:06 +01003545 if (ch != '\\' && ch != '/' && ch != ':')
3546 namebuf[len++] = '\\';
Victor Stinner8c62be82010-05-06 00:08:46 +00003547 strcpy(namebuf + len, "*.*");
3548 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00003549
Larry Hastings9cf065c2012-06-22 16:30:09 -07003550 if ((list = PyList_New(0)) == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00003551 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003552
Antoine Pitroub73caab2010-08-09 23:39:31 +00003553 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003554 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003555 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003556 if (hFindFile == INVALID_HANDLE_VALUE) {
3557 int error = GetLastError();
3558 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003559 goto exit;
3560 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003561 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003562 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003563 }
3564 do {
3565 /* Skip over . and .. */
3566 if (strcmp(FileData.cFileName, ".") != 0 &&
3567 strcmp(FileData.cFileName, "..") != 0) {
3568 v = PyBytes_FromString(FileData.cFileName);
3569 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003570 Py_DECREF(list);
3571 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003572 break;
3573 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003574 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003575 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003576 Py_DECREF(list);
3577 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003578 break;
3579 }
3580 Py_DECREF(v);
3581 }
3582 Py_BEGIN_ALLOW_THREADS
3583 result = FindNextFile(hFindFile, &FileData);
3584 Py_END_ALLOW_THREADS
3585 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3586 it got to the end of the directory. */
3587 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003588 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003589 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003590 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003591 }
3592 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003593
Larry Hastings9cf065c2012-06-22 16:30:09 -07003594exit:
3595 if (hFindFile != INVALID_HANDLE_VALUE) {
3596 if (FindClose(hFindFile) == FALSE) {
3597 if (list != NULL) {
3598 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003599 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003600 }
3601 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003602 }
Victor Stinnerb6404912013-07-07 16:21:41 +02003603 PyMem_Free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003604
Larry Hastings9cf065c2012-06-22 16:30:09 -07003605 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003606} /* end of _listdir_windows_no_opendir */
3607
3608#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
3609
3610static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003611_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003612{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003613 PyObject *v;
3614 DIR *dirp = NULL;
3615 struct dirent *ep;
3616 int return_str; /* if false, return bytes */
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003617#ifdef HAVE_FDOPENDIR
3618 int fd = -1;
3619#endif
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003620
Victor Stinner8c62be82010-05-06 00:08:46 +00003621 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003622#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07003623 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003624 /* closedir() closes the FD, so we duplicate it */
Victor Stinnerdaf45552013-08-28 00:53:59 +02003625 fd = _Py_dup(path->fd);
Victor Stinnerf3266652013-12-19 13:24:49 +01003626 if (fd == -1)
3627 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003628
Larry Hastingsfdaea062012-06-25 04:42:23 -07003629 return_str = 1;
3630
Larry Hastings9cf065c2012-06-22 16:30:09 -07003631 Py_BEGIN_ALLOW_THREADS
3632 dirp = fdopendir(fd);
3633 Py_END_ALLOW_THREADS
3634 }
3635 else
3636#endif
3637 {
Larry Hastingsfdaea062012-06-25 04:42:23 -07003638 char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07003639 if (path->narrow) {
3640 name = path->narrow;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003641 /* only return bytes if they specified a bytes object */
Gregory P. Smith40a21602013-03-20 20:52:50 -07003642 return_str = !(PyBytes_Check(path->object));
Larry Hastingsfdaea062012-06-25 04:42:23 -07003643 }
3644 else {
3645 name = ".";
3646 return_str = 1;
3647 }
3648
Larry Hastings9cf065c2012-06-22 16:30:09 -07003649 Py_BEGIN_ALLOW_THREADS
3650 dirp = opendir(name);
3651 Py_END_ALLOW_THREADS
3652 }
3653
3654 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003655 list = path_error(path);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003656#ifdef HAVE_FDOPENDIR
3657 if (fd != -1) {
3658 Py_BEGIN_ALLOW_THREADS
3659 close(fd);
3660 Py_END_ALLOW_THREADS
3661 }
3662#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003663 goto exit;
3664 }
3665 if ((list = PyList_New(0)) == NULL) {
3666 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003667 }
3668 for (;;) {
3669 errno = 0;
3670 Py_BEGIN_ALLOW_THREADS
3671 ep = readdir(dirp);
3672 Py_END_ALLOW_THREADS
3673 if (ep == NULL) {
3674 if (errno == 0) {
3675 break;
3676 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003677 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003678 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003679 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003680 }
3681 }
3682 if (ep->d_name[0] == '.' &&
3683 (NAMLEN(ep) == 1 ||
3684 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3685 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003686 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00003687 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3688 else
3689 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003690 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003691 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003692 break;
3693 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003694 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003695 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003696 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003697 break;
3698 }
3699 Py_DECREF(v);
3700 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003701
Larry Hastings9cf065c2012-06-22 16:30:09 -07003702exit:
3703 if (dirp != NULL) {
3704 Py_BEGIN_ALLOW_THREADS
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003705#ifdef HAVE_FDOPENDIR
Larry Hastings9cf065c2012-06-22 16:30:09 -07003706 if (fd > -1)
3707 rewinddir(dirp);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003708#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003709 closedir(dirp);
3710 Py_END_ALLOW_THREADS
3711 }
3712
Larry Hastings9cf065c2012-06-22 16:30:09 -07003713 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003714} /* end of _posix_listdir */
3715#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003716
Larry Hastings2f936352014-08-05 14:04:04 +10003717
3718/*[clinic input]
3719os.listdir
3720
3721 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
3722
3723Return a list containing the names of the files in the directory.
3724
3725path can be specified as either str or bytes. If path is bytes,
3726 the filenames returned will also be bytes; in all other circumstances
3727 the filenames returned will be str.
3728If path is None, uses the path='.'.
3729On some platforms, path may also be specified as an open file descriptor;\
3730 the file descriptor must refer to a directory.
3731 If this functionality is unavailable, using it raises NotImplementedError.
3732
3733The list is in arbitrary order. It does not include the special
3734entries '.' and '..' even if they are present in the directory.
3735
3736
3737[clinic start generated code]*/
3738
Larry Hastings2f936352014-08-05 14:04:04 +10003739static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003740os_listdir_impl(PyObject *module, path_t *path)
3741/*[clinic end generated code: output=293045673fcd1a75 input=09e300416e3cd729]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003742{
3743#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3744 return _listdir_windows_no_opendir(path, NULL);
3745#else
3746 return _posix_listdir(path, NULL);
3747#endif
3748}
3749
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003750#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003751/* A helper function for abspath on win32 */
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003752/*[clinic input]
3753os._getfullpathname
Victor Stinnereb5657a2011-09-30 01:44:27 +02003754
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003755 path: path_t
3756 /
3757
3758[clinic start generated code]*/
3759
3760static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003761os__getfullpathname_impl(PyObject *module, path_t *path)
3762/*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003763{
3764 if (!path->narrow)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003765 {
Victor Stinner75875072013-11-24 19:23:25 +01003766 wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003767 wchar_t *wtemp;
Victor Stinner8c62be82010-05-06 00:08:46 +00003768 DWORD result;
3769 PyObject *v;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003770
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003771 result = GetFullPathNameW(path->wide,
Victor Stinner63941882011-09-29 00:42:28 +02003772 Py_ARRAY_LENGTH(woutbuf),
Victor Stinner8c62be82010-05-06 00:08:46 +00003773 woutbuf, &wtemp);
Victor Stinner63941882011-09-29 00:42:28 +02003774 if (result > Py_ARRAY_LENGTH(woutbuf)) {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02003775 woutbufp = PyMem_New(wchar_t, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00003776 if (!woutbufp)
3777 return PyErr_NoMemory();
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003778 result = GetFullPathNameW(path->wide, result, woutbufp, &wtemp);
Victor Stinner8c62be82010-05-06 00:08:46 +00003779 }
3780 if (result)
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003781 v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp));
Victor Stinner8c62be82010-05-06 00:08:46 +00003782 else
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003783 v = win32_error_object("GetFullPathNameW", path->object);
Victor Stinner8c62be82010-05-06 00:08:46 +00003784 if (woutbufp != woutbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003785 PyMem_Free(woutbufp);
Victor Stinner8c62be82010-05-06 00:08:46 +00003786 return v;
3787 }
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003788 else {
3789 char outbuf[MAX_PATH];
3790 char *temp;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003791
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003792 if (!GetFullPathName(path->narrow, Py_ARRAY_LENGTH(outbuf),
3793 outbuf, &temp)) {
3794 win32_error_object("GetFullPathName", path->object);
3795 return NULL;
3796 }
3797 return PyBytes_FromString(outbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003798 }
Larry Hastings2f936352014-08-05 14:04:04 +10003799}
Brian Curtind40e6f72010-07-08 21:39:08 +00003800
Brian Curtind25aef52011-06-13 15:16:04 -05003801
Larry Hastings2f936352014-08-05 14:04:04 +10003802/*[clinic input]
3803os._getfinalpathname
Brian Curtinf5e76d02010-11-24 13:14:05 +00003804
Larry Hastings2f936352014-08-05 14:04:04 +10003805 path: unicode
3806 /
3807
3808A helper function for samepath on windows.
3809[clinic start generated code]*/
3810
Larry Hastings2f936352014-08-05 14:04:04 +10003811static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003812os__getfinalpathname_impl(PyObject *module, PyObject *path)
3813/*[clinic end generated code: output=9bd78d0e52782e75 input=71d5e89334891bf4]*/
Brian Curtind40e6f72010-07-08 21:39:08 +00003814{
3815 HANDLE hFile;
3816 int buf_size;
3817 wchar_t *target_path;
3818 int result_length;
Larry Hastings2f936352014-08-05 14:04:04 +10003819 PyObject *result;
3820 wchar_t *path_wchar;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003821
Larry Hastings2f936352014-08-05 14:04:04 +10003822 path_wchar = PyUnicode_AsUnicode(path);
3823 if (path_wchar == NULL)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003824 return NULL;
Brian Curtind40e6f72010-07-08 21:39:08 +00003825
Brian Curtind40e6f72010-07-08 21:39:08 +00003826 hFile = CreateFileW(
Larry Hastings2f936352014-08-05 14:04:04 +10003827 path_wchar,
Brian Curtind40e6f72010-07-08 21:39:08 +00003828 0, /* desired access */
3829 0, /* share mode */
3830 NULL, /* security attributes */
3831 OPEN_EXISTING,
3832 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3833 FILE_FLAG_BACKUP_SEMANTICS,
3834 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003835
Victor Stinnereb5657a2011-09-30 01:44:27 +02003836 if(hFile == INVALID_HANDLE_VALUE)
Larry Hastings2f936352014-08-05 14:04:04 +10003837 return win32_error_object("CreateFileW", path);
Brian Curtind40e6f72010-07-08 21:39:08 +00003838
3839 /* We have a good handle to the target, use it to determine the
3840 target path name. */
Steve Dower2ea51c92015-03-20 21:49:12 -07003841 buf_size = GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
Brian Curtind40e6f72010-07-08 21:39:08 +00003842
3843 if(!buf_size)
Larry Hastings2f936352014-08-05 14:04:04 +10003844 return win32_error_object("GetFinalPathNameByHandle", path);
Brian Curtind40e6f72010-07-08 21:39:08 +00003845
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02003846 target_path = PyMem_New(wchar_t, buf_size+1);
Brian Curtind40e6f72010-07-08 21:39:08 +00003847 if(!target_path)
3848 return PyErr_NoMemory();
3849
Steve Dower2ea51c92015-03-20 21:49:12 -07003850 result_length = GetFinalPathNameByHandleW(hFile, target_path,
3851 buf_size, VOLUME_NAME_DOS);
Brian Curtind40e6f72010-07-08 21:39:08 +00003852 if(!result_length)
Larry Hastings2f936352014-08-05 14:04:04 +10003853 return win32_error_object("GetFinalPathNamyByHandle", path);
Brian Curtind40e6f72010-07-08 21:39:08 +00003854
3855 if(!CloseHandle(hFile))
Larry Hastings2f936352014-08-05 14:04:04 +10003856 return win32_error_object("CloseHandle", path);
Brian Curtind40e6f72010-07-08 21:39:08 +00003857
3858 target_path[result_length] = 0;
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003859 result = PyUnicode_FromWideChar(target_path, result_length);
Victor Stinnerb6404912013-07-07 16:21:41 +02003860 PyMem_Free(target_path);
Brian Curtind40e6f72010-07-08 21:39:08 +00003861 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10003862}
Brian Curtin62857742010-09-06 17:07:27 +00003863
Brian Curtin95d028f2011-06-09 09:10:38 -05003864PyDoc_STRVAR(posix__isdir__doc__,
3865"Return true if the pathname refers to an existing directory.");
3866
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003867/*[clinic input]
3868os._isdir
3869
3870 path: path_t
3871 /
3872
3873[clinic start generated code]*/
3874
Brian Curtin9c669cc2011-06-08 18:17:18 -05003875static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003876os__isdir_impl(PyObject *module, path_t *path)
3877/*[clinic end generated code: output=75f56f32720836cb input=e794f12faab62a2a]*/
Brian Curtin9c669cc2011-06-08 18:17:18 -05003878{
Brian Curtin9c669cc2011-06-08 18:17:18 -05003879 DWORD attributes;
3880
Steve Dowerb22a6772016-07-17 20:49:38 -07003881 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003882 if (!path->narrow)
3883 attributes = GetFileAttributesW(path->wide);
3884 else
3885 attributes = GetFileAttributesA(path->narrow);
Steve Dowerb22a6772016-07-17 20:49:38 -07003886 Py_END_ALLOW_THREADS
Brian Curtin9c669cc2011-06-08 18:17:18 -05003887
Brian Curtin9c669cc2011-06-08 18:17:18 -05003888 if (attributes == INVALID_FILE_ATTRIBUTES)
3889 Py_RETURN_FALSE;
3890
Brian Curtin9c669cc2011-06-08 18:17:18 -05003891 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3892 Py_RETURN_TRUE;
3893 else
3894 Py_RETURN_FALSE;
3895}
Tim Golden6b528062013-08-01 12:44:00 +01003896
Tim Golden6b528062013-08-01 12:44:00 +01003897
Larry Hastings2f936352014-08-05 14:04:04 +10003898/*[clinic input]
3899os._getvolumepathname
3900
3901 path: unicode
3902
3903A helper function for ismount on Win32.
3904[clinic start generated code]*/
3905
Larry Hastings2f936352014-08-05 14:04:04 +10003906static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003907os__getvolumepathname_impl(PyObject *module, PyObject *path)
3908/*[clinic end generated code: output=cbdcbd1059ceef4c input=7eacadc40acbda6b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003909{
3910 PyObject *result;
3911 wchar_t *path_wchar, *mountpath=NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01003912 size_t buflen;
Tim Golden6b528062013-08-01 12:44:00 +01003913 BOOL ret;
3914
Larry Hastings2f936352014-08-05 14:04:04 +10003915 path_wchar = PyUnicode_AsUnicodeAndSize(path, &buflen);
3916 if (path_wchar == NULL)
Tim Golden6b528062013-08-01 12:44:00 +01003917 return NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01003918 buflen += 1;
Tim Golden6b528062013-08-01 12:44:00 +01003919
3920 /* Volume path should be shorter than entire path */
Victor Stinner6edddfa2013-11-24 19:22:57 +01003921 buflen = Py_MAX(buflen, MAX_PATH);
3922
3923 if (buflen > DWORD_MAX) {
3924 PyErr_SetString(PyExc_OverflowError, "path too long");
3925 return NULL;
3926 }
3927
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02003928 mountpath = PyMem_New(wchar_t, buflen);
Tim Golden6b528062013-08-01 12:44:00 +01003929 if (mountpath == NULL)
3930 return PyErr_NoMemory();
3931
3932 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003933 ret = GetVolumePathNameW(path_wchar, mountpath,
Victor Stinner6edddfa2013-11-24 19:22:57 +01003934 Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Tim Golden6b528062013-08-01 12:44:00 +01003935 Py_END_ALLOW_THREADS
3936
3937 if (!ret) {
Larry Hastings2f936352014-08-05 14:04:04 +10003938 result = win32_error_object("_getvolumepathname", path);
Tim Golden6b528062013-08-01 12:44:00 +01003939 goto exit;
3940 }
3941 result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
3942
3943exit:
3944 PyMem_Free(mountpath);
3945 return result;
3946}
Tim Golden6b528062013-08-01 12:44:00 +01003947
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003948#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003949
Larry Hastings2f936352014-08-05 14:04:04 +10003950
3951/*[clinic input]
3952os.mkdir
3953
3954 path : path_t
3955
3956 mode: int = 0o777
3957
3958 *
3959
3960 dir_fd : dir_fd(requires='mkdirat') = None
3961
3962# "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
3963
3964Create a directory.
3965
3966If dir_fd is not None, it should be a file descriptor open to a directory,
3967 and path should be relative; path will then be relative to that directory.
3968dir_fd may not be implemented on your platform.
3969 If it is unavailable, using it will raise a NotImplementedError.
3970
3971The mode argument is ignored on Windows.
3972[clinic start generated code]*/
3973
Larry Hastings2f936352014-08-05 14:04:04 +10003974static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003975os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
3976/*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003977{
3978 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003979
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003980#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003981 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003982 if (path->wide)
3983 result = CreateDirectoryW(path->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003984 else
Larry Hastings2f936352014-08-05 14:04:04 +10003985 result = CreateDirectoryA(path->narrow, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00003986 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003987
Larry Hastings2f936352014-08-05 14:04:04 +10003988 if (!result)
3989 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003990#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003991 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003992#if HAVE_MKDIRAT
3993 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10003994 result = mkdirat(dir_fd, path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003995 else
3996#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00003997#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Larry Hastings2f936352014-08-05 14:04:04 +10003998 result = mkdir(path->narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003999#else
Larry Hastings2f936352014-08-05 14:04:04 +10004000 result = mkdir(path->narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004001#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004002 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004003 if (result < 0)
4004 return path_error(path);
Thomas Wouters477c8d52006-05-27 19:21:47 +00004005#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004006 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004007}
4008
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004009
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004010/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
4011#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004012#include <sys/resource.h>
4013#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004014
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004015
4016#ifdef HAVE_NICE
Larry Hastings2f936352014-08-05 14:04:04 +10004017/*[clinic input]
4018os.nice
4019
4020 increment: int
4021 /
4022
4023Add increment to the priority of process and return the new priority.
4024[clinic start generated code]*/
4025
Larry Hastings2f936352014-08-05 14:04:04 +10004026static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004027os_nice_impl(PyObject *module, int increment)
4028/*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004029{
4030 int value;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004031
Victor Stinner8c62be82010-05-06 00:08:46 +00004032 /* There are two flavours of 'nice': one that returns the new
4033 priority (as required by almost all standards out there) and the
4034 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
4035 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00004036
Victor Stinner8c62be82010-05-06 00:08:46 +00004037 If we are of the nice family that returns the new priority, we
4038 need to clear errno before the call, and check if errno is filled
4039 before calling posix_error() on a returnvalue of -1, because the
4040 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004041
Victor Stinner8c62be82010-05-06 00:08:46 +00004042 errno = 0;
4043 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004044#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004045 if (value == 0)
4046 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004047#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004048 if (value == -1 && errno != 0)
4049 /* either nice() or getpriority() returned an error */
4050 return posix_error();
4051 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00004052}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004053#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004054
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004055
4056#ifdef HAVE_GETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004057/*[clinic input]
4058os.getpriority
4059
4060 which: int
4061 who: int
4062
4063Return program scheduling priority.
4064[clinic start generated code]*/
4065
Larry Hastings2f936352014-08-05 14:04:04 +10004066static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004067os_getpriority_impl(PyObject *module, int which, int who)
4068/*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004069{
4070 int retval;
4071
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004072 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004073 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004074 if (errno != 0)
4075 return posix_error();
4076 return PyLong_FromLong((long)retval);
4077}
4078#endif /* HAVE_GETPRIORITY */
4079
4080
4081#ifdef HAVE_SETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004082/*[clinic input]
4083os.setpriority
4084
4085 which: int
4086 who: int
4087 priority: int
4088
4089Set program scheduling priority.
4090[clinic start generated code]*/
4091
Larry Hastings2f936352014-08-05 14:04:04 +10004092static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004093os_setpriority_impl(PyObject *module, int which, int who, int priority)
4094/*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004095{
4096 int retval;
4097
4098 retval = setpriority(which, who, priority);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004099 if (retval == -1)
4100 return posix_error();
4101 Py_RETURN_NONE;
4102}
4103#endif /* HAVE_SETPRIORITY */
4104
4105
Barry Warsaw53699e91996-12-10 23:23:01 +00004106static PyObject *
Larry Hastings2f936352014-08-05 14:04:04 +10004107internal_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 +00004108{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004109 char *function_name = is_replace ? "replace" : "rename";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004110 int dir_fd_specified;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004111
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004112#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004113 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004114 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004115#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004116 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004117#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004118
Larry Hastings9cf065c2012-06-22 16:30:09 -07004119 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
4120 (dst_dir_fd != DEFAULT_DIR_FD);
4121#ifndef HAVE_RENAMEAT
4122 if (dir_fd_specified) {
4123 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10004124 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004125 }
4126#endif
4127
Larry Hastings2f936352014-08-05 14:04:04 +10004128 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004129 PyErr_Format(PyExc_ValueError,
4130 "%s: src and dst must be the same type", function_name);
Larry Hastings2f936352014-08-05 14:04:04 +10004131 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004132 }
4133
4134#ifdef MS_WINDOWS
4135 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004136 if (src->wide)
4137 result = MoveFileExW(src->wide, dst->wide, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004138 else
Larry Hastings2f936352014-08-05 14:04:04 +10004139 result = MoveFileExA(src->narrow, dst->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004140 Py_END_ALLOW_THREADS
4141
Larry Hastings2f936352014-08-05 14:04:04 +10004142 if (!result)
4143 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004144
4145#else
4146 Py_BEGIN_ALLOW_THREADS
4147#ifdef HAVE_RENAMEAT
4148 if (dir_fd_specified)
Larry Hastings2f936352014-08-05 14:04:04 +10004149 result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004150 else
4151#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004152 result = rename(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004153 Py_END_ALLOW_THREADS
4154
Larry Hastings2f936352014-08-05 14:04:04 +10004155 if (result)
4156 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004157#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004158 Py_RETURN_NONE;
4159}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004160
Larry Hastings2f936352014-08-05 14:04:04 +10004161
4162/*[clinic input]
4163os.rename
4164
4165 src : path_t
4166 dst : path_t
4167 *
4168 src_dir_fd : dir_fd = None
4169 dst_dir_fd : dir_fd = None
4170
4171Rename a file or directory.
4172
4173If either src_dir_fd or dst_dir_fd is not None, it should be a file
4174 descriptor open to a directory, and the respective path string (src or dst)
4175 should be relative; the path will then be relative to that directory.
4176src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4177 If they are unavailable, using them will raise a NotImplementedError.
4178[clinic start generated code]*/
4179
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004180static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004181os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04004182 int dst_dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004183/*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004184{
Larry Hastings2f936352014-08-05 14:04:04 +10004185 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004186}
4187
Larry Hastings2f936352014-08-05 14:04:04 +10004188
4189/*[clinic input]
4190os.replace = os.rename
4191
4192Rename a file or directory, overwriting the destination.
4193
4194If either src_dir_fd or dst_dir_fd is not None, it should be a file
4195 descriptor open to a directory, and the respective path string (src or dst)
4196 should be relative; the path will then be relative to that directory.
4197src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4198 If they are unavailable, using them will raise a NotImplementedError."
4199[clinic start generated code]*/
4200
Larry Hastings2f936352014-08-05 14:04:04 +10004201static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004202os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4203 int dst_dir_fd)
4204/*[clinic end generated code: output=1968c02e7857422b input=25515dfb107c8421]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004205{
4206 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
4207}
4208
4209
4210/*[clinic input]
4211os.rmdir
4212
4213 path: path_t
4214 *
4215 dir_fd: dir_fd(requires='unlinkat') = None
4216
4217Remove a directory.
4218
4219If dir_fd is not None, it should be a file descriptor open to a directory,
4220 and path should be relative; path will then be relative to that directory.
4221dir_fd may not be implemented on your platform.
4222 If it is unavailable, using it will raise a NotImplementedError.
4223[clinic start generated code]*/
4224
Larry Hastings2f936352014-08-05 14:04:04 +10004225static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004226os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
4227/*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004228{
4229 int result;
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004230
4231 Py_BEGIN_ALLOW_THREADS
4232#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10004233 if (path->wide)
4234 result = RemoveDirectoryW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004235 else
Larry Hastings2f936352014-08-05 14:04:04 +10004236 result = RemoveDirectoryA(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004237 result = !result; /* Windows, success=1, UNIX, success=0 */
4238#else
4239#ifdef HAVE_UNLINKAT
4240 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004241 result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004242 else
4243#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004244 result = rmdir(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004245#endif
4246 Py_END_ALLOW_THREADS
4247
Larry Hastings2f936352014-08-05 14:04:04 +10004248 if (result)
4249 return path_error(path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004250
Larry Hastings2f936352014-08-05 14:04:04 +10004251 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004252}
4253
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004254
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004255#ifdef HAVE_SYSTEM
Larry Hastings2f936352014-08-05 14:04:04 +10004256#ifdef MS_WINDOWS
4257/*[clinic input]
4258os.system -> long
4259
4260 command: Py_UNICODE
4261
4262Execute the command in a subshell.
4263[clinic start generated code]*/
4264
Larry Hastings2f936352014-08-05 14:04:04 +10004265static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004266os_system_impl(PyObject *module, Py_UNICODE *command)
4267/*[clinic end generated code: output=96c4dffee36dfb48 input=303f5ce97df606b0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004268{
4269 long result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004270 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004271 result = _wsystem(command);
Victor Stinner8c62be82010-05-06 00:08:46 +00004272 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004273 return result;
4274}
4275#else /* MS_WINDOWS */
4276/*[clinic input]
4277os.system -> long
4278
4279 command: FSConverter
4280
4281Execute the command in a subshell.
4282[clinic start generated code]*/
4283
Larry Hastings2f936352014-08-05 14:04:04 +10004284static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004285os_system_impl(PyObject *module, PyObject *command)
4286/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004287{
4288 long result;
4289 char *bytes = PyBytes_AsString(command);
4290 Py_BEGIN_ALLOW_THREADS
4291 result = system(bytes);
4292 Py_END_ALLOW_THREADS
4293 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004294}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004295#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004296#endif /* HAVE_SYSTEM */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004297
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004298
Larry Hastings2f936352014-08-05 14:04:04 +10004299/*[clinic input]
4300os.umask
4301
4302 mask: int
4303 /
4304
4305Set the current numeric umask and return the previous umask.
4306[clinic start generated code]*/
4307
Larry Hastings2f936352014-08-05 14:04:04 +10004308static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004309os_umask_impl(PyObject *module, int mask)
4310/*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004311{
4312 int i = (int)umask(mask);
Victor Stinner8c62be82010-05-06 00:08:46 +00004313 if (i < 0)
4314 return posix_error();
4315 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004316}
4317
Brian Curtind40e6f72010-07-08 21:39:08 +00004318#ifdef MS_WINDOWS
4319
4320/* override the default DeleteFileW behavior so that directory
4321symlinks can be removed with this function, the same as with
4322Unix symlinks */
4323BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4324{
4325 WIN32_FILE_ATTRIBUTE_DATA info;
4326 WIN32_FIND_DATAW find_data;
4327 HANDLE find_data_handle;
4328 int is_directory = 0;
4329 int is_link = 0;
4330
4331 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4332 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004333
Brian Curtind40e6f72010-07-08 21:39:08 +00004334 /* Get WIN32_FIND_DATA structure for the path to determine if
4335 it is a symlink */
4336 if(is_directory &&
4337 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4338 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4339
4340 if(find_data_handle != INVALID_HANDLE_VALUE) {
Tim Golden0321cf22014-05-05 19:46:17 +01004341 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
4342 IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
4343 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
4344 find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
Brian Curtind40e6f72010-07-08 21:39:08 +00004345 FindClose(find_data_handle);
4346 }
4347 }
4348 }
4349
4350 if (is_directory && is_link)
4351 return RemoveDirectoryW(lpFileName);
4352
4353 return DeleteFileW(lpFileName);
4354}
4355#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004356
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004357
Larry Hastings2f936352014-08-05 14:04:04 +10004358/*[clinic input]
4359os.unlink
4360
4361 path: path_t
4362 *
4363 dir_fd: dir_fd(requires='unlinkat')=None
4364
4365Remove a file (same as remove()).
4366
4367If dir_fd is not None, it should be a file descriptor open to a directory,
4368 and path should be relative; path will then be relative to that directory.
4369dir_fd may not be implemented on your platform.
4370 If it is unavailable, using it will raise a NotImplementedError.
4371
4372[clinic start generated code]*/
4373
Larry Hastings2f936352014-08-05 14:04:04 +10004374static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004375os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
4376/*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004377{
4378 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004379
4380 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04004381 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004382#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10004383 if (path->wide)
4384 result = Py_DeleteFileW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004385 else
Larry Hastings2f936352014-08-05 14:04:04 +10004386 result = DeleteFileA(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004387 result = !result; /* Windows, success=1, UNIX, success=0 */
4388#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004389#ifdef HAVE_UNLINKAT
4390 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004391 result = unlinkat(dir_fd, path->narrow, 0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004392 else
4393#endif /* HAVE_UNLINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10004394 result = unlink(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004395#endif
Steve Dower8fc89802015-04-12 00:26:27 -04004396 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004397 Py_END_ALLOW_THREADS
4398
Larry Hastings2f936352014-08-05 14:04:04 +10004399 if (result)
4400 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004401
Larry Hastings2f936352014-08-05 14:04:04 +10004402 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004403}
4404
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004405
Larry Hastings2f936352014-08-05 14:04:04 +10004406/*[clinic input]
4407os.remove = os.unlink
4408
4409Remove a file (same as unlink()).
4410
4411If dir_fd is not None, it should be a file descriptor open to a directory,
4412 and path should be relative; path will then be relative to that directory.
4413dir_fd may not be implemented on your platform.
4414 If it is unavailable, using it will raise a NotImplementedError.
4415[clinic start generated code]*/
4416
Larry Hastings2f936352014-08-05 14:04:04 +10004417static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004418os_remove_impl(PyObject *module, path_t *path, int dir_fd)
4419/*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004420{
4421 return os_unlink_impl(module, path, dir_fd);
4422}
4423
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004424
Larry Hastings605a62d2012-06-24 04:33:36 -07004425static PyStructSequence_Field uname_result_fields[] = {
4426 {"sysname", "operating system name"},
4427 {"nodename", "name of machine on network (implementation-defined)"},
4428 {"release", "operating system release"},
4429 {"version", "operating system version"},
4430 {"machine", "hardware identifier"},
4431 {NULL}
4432};
4433
4434PyDoc_STRVAR(uname_result__doc__,
4435"uname_result: Result from os.uname().\n\n\
4436This object may be accessed either as a tuple of\n\
4437 (sysname, nodename, release, version, machine),\n\
4438or via the attributes sysname, nodename, release, version, and machine.\n\
4439\n\
4440See os.uname for more information.");
4441
4442static PyStructSequence_Desc uname_result_desc = {
4443 "uname_result", /* name */
4444 uname_result__doc__, /* doc */
4445 uname_result_fields,
4446 5
4447};
4448
4449static PyTypeObject UnameResultType;
4450
4451
4452#ifdef HAVE_UNAME
Larry Hastings2f936352014-08-05 14:04:04 +10004453/*[clinic input]
4454os.uname
4455
4456Return an object identifying the current operating system.
4457
4458The object behaves like a named tuple with the following fields:
4459 (sysname, nodename, release, version, machine)
4460
4461[clinic start generated code]*/
4462
Larry Hastings2f936352014-08-05 14:04:04 +10004463static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004464os_uname_impl(PyObject *module)
4465/*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004466{
Victor Stinner8c62be82010-05-06 00:08:46 +00004467 struct utsname u;
4468 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004469 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004470
Victor Stinner8c62be82010-05-06 00:08:46 +00004471 Py_BEGIN_ALLOW_THREADS
4472 res = uname(&u);
4473 Py_END_ALLOW_THREADS
4474 if (res < 0)
4475 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004476
4477 value = PyStructSequence_New(&UnameResultType);
4478 if (value == NULL)
4479 return NULL;
4480
4481#define SET(i, field) \
4482 { \
Victor Stinnera534fc42013-06-03 22:07:27 +02004483 PyObject *o = PyUnicode_DecodeFSDefault(field); \
Larry Hastings605a62d2012-06-24 04:33:36 -07004484 if (!o) { \
4485 Py_DECREF(value); \
4486 return NULL; \
4487 } \
4488 PyStructSequence_SET_ITEM(value, i, o); \
4489 } \
4490
4491 SET(0, u.sysname);
4492 SET(1, u.nodename);
4493 SET(2, u.release);
4494 SET(3, u.version);
4495 SET(4, u.machine);
4496
4497#undef SET
4498
4499 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004500}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004501#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004502
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004503
Larry Hastings9cf065c2012-06-22 16:30:09 -07004504
4505typedef struct {
4506 int now;
4507 time_t atime_s;
4508 long atime_ns;
4509 time_t mtime_s;
4510 long mtime_ns;
4511} utime_t;
4512
4513/*
Victor Stinner484df002014-10-09 13:52:31 +02004514 * these macros assume that "ut" is a pointer to a utime_t
Larry Hastings9cf065c2012-06-22 16:30:09 -07004515 * they also intentionally leak the declaration of a pointer named "time"
4516 */
4517#define UTIME_TO_TIMESPEC \
4518 struct timespec ts[2]; \
4519 struct timespec *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004520 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004521 time = NULL; \
4522 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004523 ts[0].tv_sec = ut->atime_s; \
4524 ts[0].tv_nsec = ut->atime_ns; \
4525 ts[1].tv_sec = ut->mtime_s; \
4526 ts[1].tv_nsec = ut->mtime_ns; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004527 time = ts; \
4528 } \
4529
4530#define UTIME_TO_TIMEVAL \
4531 struct timeval tv[2]; \
4532 struct timeval *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004533 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004534 time = NULL; \
4535 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004536 tv[0].tv_sec = ut->atime_s; \
4537 tv[0].tv_usec = ut->atime_ns / 1000; \
4538 tv[1].tv_sec = ut->mtime_s; \
4539 tv[1].tv_usec = ut->mtime_ns / 1000; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004540 time = tv; \
4541 } \
4542
4543#define UTIME_TO_UTIMBUF \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004544 struct utimbuf u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004545 struct utimbuf *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004546 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004547 time = NULL; \
4548 else { \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004549 u.actime = ut->atime_s; \
4550 u.modtime = ut->mtime_s; \
4551 time = &u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004552 }
4553
4554#define UTIME_TO_TIME_T \
4555 time_t timet[2]; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004556 time_t *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004557 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004558 time = NULL; \
4559 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004560 timet[0] = ut->atime_s; \
4561 timet[1] = ut->mtime_s; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004562 time = timet; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004563 } \
4564
4565
Victor Stinner528a9ab2015-09-03 21:30:26 +02004566#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004567
4568static int
Victor Stinner484df002014-10-09 13:52:31 +02004569utime_dir_fd(utime_t *ut, int dir_fd, char *path, int follow_symlinks)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004570{
4571#ifdef HAVE_UTIMENSAT
4572 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4573 UTIME_TO_TIMESPEC;
4574 return utimensat(dir_fd, path, time, flags);
4575#elif defined(HAVE_FUTIMESAT)
4576 UTIME_TO_TIMEVAL;
4577 /*
4578 * follow_symlinks will never be false here;
4579 * we only allow !follow_symlinks and dir_fd together
4580 * if we have utimensat()
4581 */
4582 assert(follow_symlinks);
4583 return futimesat(dir_fd, path, time);
4584#endif
4585}
4586
Larry Hastings2f936352014-08-05 14:04:04 +10004587 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
4588#else
4589 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings9cf065c2012-06-22 16:30:09 -07004590#endif
4591
Victor Stinner528a9ab2015-09-03 21:30:26 +02004592#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004593
4594static int
Victor Stinner484df002014-10-09 13:52:31 +02004595utime_fd(utime_t *ut, int fd)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004596{
4597#ifdef HAVE_FUTIMENS
4598 UTIME_TO_TIMESPEC;
4599 return futimens(fd, time);
4600#else
4601 UTIME_TO_TIMEVAL;
4602 return futimes(fd, time);
4603#endif
4604}
4605
Larry Hastings2f936352014-08-05 14:04:04 +10004606 #define PATH_UTIME_HAVE_FD 1
4607#else
4608 #define PATH_UTIME_HAVE_FD 0
Larry Hastings9cf065c2012-06-22 16:30:09 -07004609#endif
4610
4611
4612#define UTIME_HAVE_NOFOLLOW_SYMLINKS \
4613 (defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES))
4614
4615#if UTIME_HAVE_NOFOLLOW_SYMLINKS
4616
4617static int
Victor Stinner484df002014-10-09 13:52:31 +02004618utime_nofollow_symlinks(utime_t *ut, char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004619{
4620#ifdef HAVE_UTIMENSAT
4621 UTIME_TO_TIMESPEC;
4622 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4623#else
4624 UTIME_TO_TIMEVAL;
4625 return lutimes(path, time);
4626#endif
4627}
4628
4629#endif
4630
4631#ifndef MS_WINDOWS
4632
4633static int
Victor Stinner484df002014-10-09 13:52:31 +02004634utime_default(utime_t *ut, char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004635{
4636#ifdef HAVE_UTIMENSAT
4637 UTIME_TO_TIMESPEC;
4638 return utimensat(DEFAULT_DIR_FD, path, time, 0);
4639#elif defined(HAVE_UTIMES)
4640 UTIME_TO_TIMEVAL;
4641 return utimes(path, time);
4642#elif defined(HAVE_UTIME_H)
4643 UTIME_TO_UTIMBUF;
4644 return utime(path, time);
4645#else
4646 UTIME_TO_TIME_T;
4647 return utime(path, time);
4648#endif
4649}
4650
4651#endif
4652
Larry Hastings76ad59b2012-05-03 00:30:07 -07004653static int
4654split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4655{
4656 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04004657 PyObject *divmod;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004658 divmod = PyNumber_Divmod(py_long, billion);
4659 if (!divmod)
4660 goto exit;
4661 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4662 if ((*s == -1) && PyErr_Occurred())
4663 goto exit;
4664 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04004665 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07004666 goto exit;
4667
4668 result = 1;
4669exit:
4670 Py_XDECREF(divmod);
4671 return result;
4672}
4673
Larry Hastings2f936352014-08-05 14:04:04 +10004674
4675/*[clinic input]
4676os.utime
4677
4678 path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
4679 times: object = NULL
4680 *
4681 ns: object = NULL
4682 dir_fd: dir_fd(requires='futimensat') = None
4683 follow_symlinks: bool=True
4684
Martin Panter0ff89092015-09-09 01:56:53 +00004685# "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
Larry Hastings2f936352014-08-05 14:04:04 +10004686
4687Set the access and modified time of path.
4688
4689path may always be specified as a string.
4690On some platforms, path may also be specified as an open file descriptor.
4691 If this functionality is unavailable, using it raises an exception.
4692
4693If times is not None, it must be a tuple (atime, mtime);
4694 atime and mtime should be expressed as float seconds since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004695If ns is specified, it must be a tuple (atime_ns, mtime_ns);
Larry Hastings2f936352014-08-05 14:04:04 +10004696 atime_ns and mtime_ns should be expressed as integer nanoseconds
4697 since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004698If times is None and ns is unspecified, utime uses the current time.
Larry Hastings2f936352014-08-05 14:04:04 +10004699Specifying tuples for both times and ns is an error.
4700
4701If dir_fd is not None, it should be a file descriptor open to a directory,
4702 and path should be relative; path will then be relative to that directory.
4703If follow_symlinks is False, and the last element of the path is a symbolic
4704 link, utime will modify the symbolic link itself instead of the file the
4705 link points to.
4706It is an error to use dir_fd or follow_symlinks when specifying path
4707 as an open file descriptor.
4708dir_fd and follow_symlinks may not be available on your platform.
4709 If they are unavailable, using them will raise a NotImplementedError.
4710
4711[clinic start generated code]*/
4712
Larry Hastings2f936352014-08-05 14:04:04 +10004713static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004714os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
4715 int dir_fd, int follow_symlinks)
4716/*[clinic end generated code: output=cfcac69d027b82cf input=081cdc54ca685385]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004717{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004718#ifdef MS_WINDOWS
4719 HANDLE hFile;
4720 FILETIME atime, mtime;
4721#else
4722 int result;
4723#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004724
Larry Hastings9cf065c2012-06-22 16:30:09 -07004725 PyObject *return_value = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10004726 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004727
Christian Heimesb3c87242013-08-01 00:08:16 +02004728 memset(&utime, 0, sizeof(utime_t));
Larry Hastings76ad59b2012-05-03 00:30:07 -07004729
Larry Hastings9cf065c2012-06-22 16:30:09 -07004730 if (times && (times != Py_None) && ns) {
4731 PyErr_SetString(PyExc_ValueError,
4732 "utime: you may specify either 'times'"
4733 " or 'ns' but not both");
4734 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004735 }
4736
4737 if (times && (times != Py_None)) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004738 time_t a_sec, m_sec;
4739 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004740 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004741 PyErr_SetString(PyExc_TypeError,
4742 "utime: 'times' must be either"
4743 " a tuple of two ints or None");
4744 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004745 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004746 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004747 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004748 &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004749 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004750 &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004751 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004752 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004753 utime.atime_s = a_sec;
4754 utime.atime_ns = a_nsec;
4755 utime.mtime_s = m_sec;
4756 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004757 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004758 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004759 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004760 PyErr_SetString(PyExc_TypeError,
4761 "utime: 'ns' must be a tuple of two ints");
4762 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004763 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004764 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004765 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004766 &utime.atime_s, &utime.atime_ns) ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004767 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004768 &utime.mtime_s, &utime.mtime_ns)) {
4769 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004770 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004771 }
4772 else {
4773 /* times and ns are both None/unspecified. use "now". */
4774 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004775 }
4776
Larry Hastings9cf065c2012-06-22 16:30:09 -07004777#if !UTIME_HAVE_NOFOLLOW_SYMLINKS
4778 if (follow_symlinks_specified("utime", follow_symlinks))
4779 goto exit;
4780#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004781
Larry Hastings2f936352014-08-05 14:04:04 +10004782 if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
4783 dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
4784 fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -07004785 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004786
Larry Hastings9cf065c2012-06-22 16:30:09 -07004787#if !defined(HAVE_UTIMENSAT)
4788 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02004789 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004790 "utime: cannot use dir_fd and follow_symlinks "
4791 "together on this platform");
4792 goto exit;
4793 }
4794#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004795
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004796#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004797 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004798 if (path->wide)
4799 hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
Victor Stinner8c62be82010-05-06 00:08:46 +00004800 NULL, OPEN_EXISTING,
4801 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004802 else
Larry Hastings2f936352014-08-05 14:04:04 +10004803 hFile = CreateFileA(path->narrow, FILE_WRITE_ATTRIBUTES, 0,
Victor Stinner8c62be82010-05-06 00:08:46 +00004804 NULL, OPEN_EXISTING,
4805 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004806 Py_END_ALLOW_THREADS
4807 if (hFile == INVALID_HANDLE_VALUE) {
Larry Hastings2f936352014-08-05 14:04:04 +10004808 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004809 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004810 }
4811
Larry Hastings9cf065c2012-06-22 16:30:09 -07004812 if (utime.now) {
Antoine Pitrou91a7af32013-11-23 15:23:26 +01004813 GetSystemTimeAsFileTime(&mtime);
4814 atime = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00004815 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004816 else {
Steve Dowerbf1f3762015-02-21 15:26:02 -08004817 _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
4818 _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00004819 }
4820 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
4821 /* Avoid putting the file name into the error here,
4822 as that may confuse the user into believing that
4823 something is wrong with the file, when it also
4824 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01004825 PyErr_SetFromWindowsErr(0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004826 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004827 }
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004828#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004829 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004830
Larry Hastings9cf065c2012-06-22 16:30:09 -07004831#if UTIME_HAVE_NOFOLLOW_SYMLINKS
4832 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10004833 result = utime_nofollow_symlinks(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004834 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004835#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004836
Victor Stinner528a9ab2015-09-03 21:30:26 +02004837#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004838 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10004839 result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004840 else
4841#endif
4842
Victor Stinner528a9ab2015-09-03 21:30:26 +02004843#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings2f936352014-08-05 14:04:04 +10004844 if (path->fd != -1)
4845 result = utime_fd(&utime, path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004846 else
4847#endif
4848
Larry Hastings2f936352014-08-05 14:04:04 +10004849 result = utime_default(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004850
4851 Py_END_ALLOW_THREADS
4852
4853 if (result < 0) {
4854 /* see previous comment about not putting filename in error here */
4855 return_value = posix_error();
4856 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004857 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004858
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004859#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004860
4861 Py_INCREF(Py_None);
4862 return_value = Py_None;
4863
4864exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -07004865#ifdef MS_WINDOWS
4866 if (hFile != INVALID_HANDLE_VALUE)
4867 CloseHandle(hFile);
4868#endif
4869 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004870}
4871
Guido van Rossum3b066191991-06-04 19:40:25 +00004872/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004873
Larry Hastings2f936352014-08-05 14:04:04 +10004874
4875/*[clinic input]
4876os._exit
4877
4878 status: int
4879
4880Exit to the system with specified status, without normal exit processing.
4881[clinic start generated code]*/
4882
Larry Hastings2f936352014-08-05 14:04:04 +10004883static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004884os__exit_impl(PyObject *module, int status)
4885/*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004886{
4887 _exit(status);
Victor Stinner8c62be82010-05-06 00:08:46 +00004888 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004889}
4890
Martin v. Löwis114619e2002-10-07 06:44:21 +00004891#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
4892static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00004893free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004894{
Victor Stinner8c62be82010-05-06 00:08:46 +00004895 Py_ssize_t i;
4896 for (i = 0; i < count; i++)
4897 PyMem_Free(array[i]);
4898 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004899}
Martin v. Löwis011e8422009-05-05 04:43:17 +00004900
Antoine Pitrou69f71142009-05-24 21:25:49 +00004901static
Martin v. Löwis011e8422009-05-05 04:43:17 +00004902int fsconvert_strdup(PyObject *o, char**out)
4903{
Victor Stinner8c62be82010-05-06 00:08:46 +00004904 PyObject *bytes;
4905 Py_ssize_t size;
4906 if (!PyUnicode_FSConverter(o, &bytes))
4907 return 0;
4908 size = PyBytes_GET_SIZE(bytes);
4909 *out = PyMem_Malloc(size+1);
Victor Stinner50abf222013-11-07 23:56:10 +01004910 if (!*out) {
4911 PyErr_NoMemory();
Victor Stinner8c62be82010-05-06 00:08:46 +00004912 return 0;
Victor Stinner50abf222013-11-07 23:56:10 +01004913 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004914 memcpy(*out, PyBytes_AsString(bytes), size+1);
4915 Py_DECREF(bytes);
4916 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004917}
Martin v. Löwis114619e2002-10-07 06:44:21 +00004918#endif
4919
Ross Lagerwall7807c352011-03-17 20:20:30 +02004920#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00004921static char**
4922parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
4923{
Victor Stinner8c62be82010-05-06 00:08:46 +00004924 char **envlist;
4925 Py_ssize_t i, pos, envc;
4926 PyObject *keys=NULL, *vals=NULL;
4927 PyObject *key, *val, *key2, *val2;
4928 char *p, *k, *v;
4929 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004930
Victor Stinner8c62be82010-05-06 00:08:46 +00004931 i = PyMapping_Size(env);
4932 if (i < 0)
4933 return NULL;
4934 envlist = PyMem_NEW(char *, i + 1);
4935 if (envlist == NULL) {
4936 PyErr_NoMemory();
4937 return NULL;
4938 }
4939 envc = 0;
4940 keys = PyMapping_Keys(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01004941 if (!keys)
4942 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00004943 vals = PyMapping_Values(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01004944 if (!vals)
Victor Stinner8c62be82010-05-06 00:08:46 +00004945 goto error;
4946 if (!PyList_Check(keys) || !PyList_Check(vals)) {
4947 PyErr_Format(PyExc_TypeError,
4948 "env.keys() or env.values() is not a list");
4949 goto error;
4950 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004951
Victor Stinner8c62be82010-05-06 00:08:46 +00004952 for (pos = 0; pos < i; pos++) {
4953 key = PyList_GetItem(keys, pos);
4954 val = PyList_GetItem(vals, pos);
4955 if (!key || !val)
4956 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004957
Victor Stinner8c62be82010-05-06 00:08:46 +00004958 if (PyUnicode_FSConverter(key, &key2) == 0)
4959 goto error;
4960 if (PyUnicode_FSConverter(val, &val2) == 0) {
4961 Py_DECREF(key2);
4962 goto error;
4963 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004964
Victor Stinner8c62be82010-05-06 00:08:46 +00004965 k = PyBytes_AsString(key2);
4966 v = PyBytes_AsString(val2);
4967 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004968
Victor Stinner8c62be82010-05-06 00:08:46 +00004969 p = PyMem_NEW(char, len);
4970 if (p == NULL) {
4971 PyErr_NoMemory();
4972 Py_DECREF(key2);
4973 Py_DECREF(val2);
4974 goto error;
4975 }
4976 PyOS_snprintf(p, len, "%s=%s", k, v);
4977 envlist[envc++] = p;
4978 Py_DECREF(key2);
4979 Py_DECREF(val2);
Victor Stinner8c62be82010-05-06 00:08:46 +00004980 }
4981 Py_DECREF(vals);
4982 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004983
Victor Stinner8c62be82010-05-06 00:08:46 +00004984 envlist[envc] = 0;
4985 *envc_ptr = envc;
4986 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004987
4988error:
Victor Stinner8c62be82010-05-06 00:08:46 +00004989 Py_XDECREF(keys);
4990 Py_XDECREF(vals);
4991 while (--envc >= 0)
4992 PyMem_DEL(envlist[envc]);
4993 PyMem_DEL(envlist);
4994 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004995}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004996
Ross Lagerwall7807c352011-03-17 20:20:30 +02004997static char**
4998parse_arglist(PyObject* argv, Py_ssize_t *argc)
4999{
5000 int i;
5001 char **argvlist = PyMem_NEW(char *, *argc+1);
5002 if (argvlist == NULL) {
5003 PyErr_NoMemory();
5004 return NULL;
5005 }
5006 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005007 PyObject* item = PySequence_ITEM(argv, i);
5008 if (item == NULL)
5009 goto fail;
5010 if (!fsconvert_strdup(item, &argvlist[i])) {
5011 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005012 goto fail;
5013 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005014 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005015 }
5016 argvlist[*argc] = NULL;
5017 return argvlist;
5018fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005019 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005020 free_string_array(argvlist, *argc);
5021 return NULL;
5022}
5023#endif
5024
Larry Hastings2f936352014-08-05 14:04:04 +10005025
Ross Lagerwall7807c352011-03-17 20:20:30 +02005026#ifdef HAVE_EXECV
Larry Hastings2f936352014-08-05 14:04:04 +10005027/*[clinic input]
5028os.execv
5029
5030 path: FSConverter
5031 Path of executable file.
5032 argv: object
5033 Tuple or list of strings.
5034 /
5035
5036Execute an executable path with arguments, replacing current process.
5037[clinic start generated code]*/
5038
Larry Hastings2f936352014-08-05 14:04:04 +10005039static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005040os_execv_impl(PyObject *module, PyObject *path, PyObject *argv)
5041/*[clinic end generated code: output=b21dc34deeb5b004 input=96041559925e5229]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005042{
5043 char *path_char;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005044 char **argvlist;
5045 Py_ssize_t argc;
5046
5047 /* execv has two arguments: (path, argv), where
5048 argv is a list or tuple of strings. */
5049
Larry Hastings2f936352014-08-05 14:04:04 +10005050 path_char = PyBytes_AsString(path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005051 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5052 PyErr_SetString(PyExc_TypeError,
5053 "execv() arg 2 must be a tuple or list");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005054 return NULL;
5055 }
5056 argc = PySequence_Size(argv);
5057 if (argc < 1) {
5058 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005059 return NULL;
5060 }
5061
5062 argvlist = parse_arglist(argv, &argc);
5063 if (argvlist == NULL) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02005064 return NULL;
5065 }
5066
Larry Hastings2f936352014-08-05 14:04:04 +10005067 execv(path_char, argvlist);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005068
5069 /* If we get here it's definitely an error */
5070
5071 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005072 return posix_error();
5073}
5074
Larry Hastings2f936352014-08-05 14:04:04 +10005075
5076/*[clinic input]
5077os.execve
5078
5079 path: path_t(allow_fd='PATH_HAVE_FEXECVE')
5080 Path of executable file.
5081 argv: object
5082 Tuple or list of strings.
5083 env: object
5084 Dictionary of strings mapping to strings.
5085
5086Execute an executable path with arguments, replacing current process.
5087[clinic start generated code]*/
5088
Larry Hastings2f936352014-08-05 14:04:04 +10005089static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005090os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
5091/*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005092{
Larry Hastings9cf065c2012-06-22 16:30:09 -07005093 char **argvlist = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005094 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005095 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005096
Victor Stinner8c62be82010-05-06 00:08:46 +00005097 /* execve has three arguments: (path, argv, env), where
5098 argv is a list or tuple of strings and env is a dictionary
5099 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005100
Ross Lagerwall7807c352011-03-17 20:20:30 +02005101 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005102 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005103 "execve: argv must be a tuple or list");
5104 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005105 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005106 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00005107 if (!PyMapping_Check(env)) {
5108 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005109 "execve: environment must be a mapping object");
5110 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005111 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005112
Ross Lagerwall7807c352011-03-17 20:20:30 +02005113 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005114 if (argvlist == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005115 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005116 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005117
Victor Stinner8c62be82010-05-06 00:08:46 +00005118 envlist = parse_envlist(env, &envc);
5119 if (envlist == NULL)
Ross Lagerwall7807c352011-03-17 20:20:30 +02005120 goto fail;
5121
Larry Hastings9cf065c2012-06-22 16:30:09 -07005122#ifdef HAVE_FEXECVE
Larry Hastings2f936352014-08-05 14:04:04 +10005123 if (path->fd > -1)
5124 fexecve(path->fd, argvlist, envlist);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005125 else
5126#endif
Larry Hastings2f936352014-08-05 14:04:04 +10005127 execve(path->narrow, argvlist, envlist);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005128
5129 /* If we get here it's definitely an error */
5130
Larry Hastings2f936352014-08-05 14:04:04 +10005131 path_error(path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005132
5133 while (--envc >= 0)
5134 PyMem_DEL(envlist[envc]);
5135 PyMem_DEL(envlist);
5136 fail:
Larry Hastings9cf065c2012-06-22 16:30:09 -07005137 if (argvlist)
5138 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005139 return NULL;
5140}
Larry Hastings9cf065c2012-06-22 16:30:09 -07005141#endif /* HAVE_EXECV */
5142
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005143
Guido van Rossuma1065681999-01-25 23:20:23 +00005144#ifdef HAVE_SPAWNV
Larry Hastings2f936352014-08-05 14:04:04 +10005145/*[clinic input]
5146os.spawnv
5147
5148 mode: int
5149 Mode of process creation.
5150 path: FSConverter
5151 Path of executable file.
5152 argv: object
5153 Tuple or list of strings.
5154 /
5155
5156Execute the program specified by path in a new process.
5157[clinic start generated code]*/
5158
Larry Hastings2f936352014-08-05 14:04:04 +10005159static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005160os_spawnv_impl(PyObject *module, int mode, PyObject *path, PyObject *argv)
5161/*[clinic end generated code: output=c427c0ce40f10638 input=042c91dfc1e6debc]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005162{
5163 char *path_char;
Victor Stinner8c62be82010-05-06 00:08:46 +00005164 char **argvlist;
Larry Hastings2f936352014-08-05 14:04:04 +10005165 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00005166 Py_ssize_t argc;
5167 Py_intptr_t spawnval;
5168 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00005169
Victor Stinner8c62be82010-05-06 00:08:46 +00005170 /* spawnv has three arguments: (mode, path, argv), where
5171 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005172
Larry Hastings2f936352014-08-05 14:04:04 +10005173 path_char = PyBytes_AsString(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00005174 if (PyList_Check(argv)) {
5175 argc = PyList_Size(argv);
5176 getitem = PyList_GetItem;
5177 }
5178 else if (PyTuple_Check(argv)) {
5179 argc = PyTuple_Size(argv);
5180 getitem = PyTuple_GetItem;
5181 }
5182 else {
5183 PyErr_SetString(PyExc_TypeError,
5184 "spawnv() arg 2 must be a tuple or list");
Victor Stinner8c62be82010-05-06 00:08:46 +00005185 return NULL;
5186 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005187
Victor Stinner8c62be82010-05-06 00:08:46 +00005188 argvlist = PyMem_NEW(char *, argc+1);
5189 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005190 return PyErr_NoMemory();
5191 }
5192 for (i = 0; i < argc; i++) {
5193 if (!fsconvert_strdup((*getitem)(argv, i),
5194 &argvlist[i])) {
5195 free_string_array(argvlist, i);
5196 PyErr_SetString(
5197 PyExc_TypeError,
5198 "spawnv() arg 2 must contain only strings");
Victor Stinner8c62be82010-05-06 00:08:46 +00005199 return NULL;
5200 }
5201 }
5202 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005203
Victor Stinner8c62be82010-05-06 00:08:46 +00005204 if (mode == _OLD_P_OVERLAY)
5205 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00005206
Victor Stinner8c62be82010-05-06 00:08:46 +00005207 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10005208 spawnval = _spawnv(mode, path_char, argvlist);
Victor Stinner8c62be82010-05-06 00:08:46 +00005209 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00005210
Victor Stinner8c62be82010-05-06 00:08:46 +00005211 free_string_array(argvlist, argc);
Guido van Rossuma1065681999-01-25 23:20:23 +00005212
Victor Stinner8c62be82010-05-06 00:08:46 +00005213 if (spawnval == -1)
5214 return posix_error();
5215 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005216 return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005217}
5218
5219
Larry Hastings2f936352014-08-05 14:04:04 +10005220/*[clinic input]
5221os.spawnve
5222
5223 mode: int
5224 Mode of process creation.
5225 path: FSConverter
5226 Path of executable file.
5227 argv: object
5228 Tuple or list of strings.
5229 env: object
5230 Dictionary of strings mapping to strings.
5231 /
5232
5233Execute the program specified by path in a new process.
5234[clinic start generated code]*/
5235
Larry Hastings2f936352014-08-05 14:04:04 +10005236static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005237os_spawnve_impl(PyObject *module, int mode, PyObject *path, PyObject *argv,
5238 PyObject *env)
5239/*[clinic end generated code: output=ebcfa5f7ba2f4219 input=02362fd937963f8f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005240{
5241 char *path_char;
Victor Stinner8c62be82010-05-06 00:08:46 +00005242 char **argvlist;
5243 char **envlist;
5244 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00005245 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00005246 Py_intptr_t spawnval;
5247 PyObject *(*getitem)(PyObject *, Py_ssize_t);
5248 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00005249
Victor Stinner8c62be82010-05-06 00:08:46 +00005250 /* spawnve has four arguments: (mode, path, argv, env), where
5251 argv is a list or tuple of strings and env is a dictionary
5252 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005253
Larry Hastings2f936352014-08-05 14:04:04 +10005254 path_char = PyBytes_AsString(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00005255 if (PyList_Check(argv)) {
5256 argc = PyList_Size(argv);
5257 getitem = PyList_GetItem;
5258 }
5259 else if (PyTuple_Check(argv)) {
5260 argc = PyTuple_Size(argv);
5261 getitem = PyTuple_GetItem;
5262 }
5263 else {
5264 PyErr_SetString(PyExc_TypeError,
5265 "spawnve() arg 2 must be a tuple or list");
5266 goto fail_0;
5267 }
5268 if (!PyMapping_Check(env)) {
5269 PyErr_SetString(PyExc_TypeError,
5270 "spawnve() arg 3 must be a mapping object");
5271 goto fail_0;
5272 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005273
Victor Stinner8c62be82010-05-06 00:08:46 +00005274 argvlist = PyMem_NEW(char *, argc+1);
5275 if (argvlist == NULL) {
5276 PyErr_NoMemory();
5277 goto fail_0;
5278 }
5279 for (i = 0; i < argc; i++) {
5280 if (!fsconvert_strdup((*getitem)(argv, i),
5281 &argvlist[i]))
5282 {
5283 lastarg = i;
5284 goto fail_1;
5285 }
5286 }
5287 lastarg = argc;
5288 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005289
Victor Stinner8c62be82010-05-06 00:08:46 +00005290 envlist = parse_envlist(env, &envc);
5291 if (envlist == NULL)
5292 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00005293
Victor Stinner8c62be82010-05-06 00:08:46 +00005294 if (mode == _OLD_P_OVERLAY)
5295 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00005296
Victor Stinner8c62be82010-05-06 00:08:46 +00005297 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10005298 spawnval = _spawnve(mode, path_char, argvlist, envlist);
Victor Stinner8c62be82010-05-06 00:08:46 +00005299 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00005300
Victor Stinner8c62be82010-05-06 00:08:46 +00005301 if (spawnval == -1)
5302 (void) posix_error();
5303 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005304 res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005305
Victor Stinner8c62be82010-05-06 00:08:46 +00005306 while (--envc >= 0)
5307 PyMem_DEL(envlist[envc]);
5308 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00005309 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00005310 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005311 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00005312 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00005313}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005314
Guido van Rossuma1065681999-01-25 23:20:23 +00005315#endif /* HAVE_SPAWNV */
5316
5317
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005318#ifdef HAVE_FORK1
Larry Hastings2f936352014-08-05 14:04:04 +10005319/*[clinic input]
5320os.fork1
5321
5322Fork a child process with a single multiplexed (i.e., not bound) thread.
5323
5324Return 0 to child process and PID of child to parent process.
5325[clinic start generated code]*/
5326
Larry Hastings2f936352014-08-05 14:04:04 +10005327static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005328os_fork1_impl(PyObject *module)
5329/*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005330{
Victor Stinner8c62be82010-05-06 00:08:46 +00005331 pid_t pid;
5332 int result = 0;
5333 _PyImport_AcquireLock();
5334 pid = fork1();
5335 if (pid == 0) {
5336 /* child: this clobbers and resets the import lock. */
5337 PyOS_AfterFork();
5338 } else {
5339 /* parent: release the import lock. */
5340 result = _PyImport_ReleaseLock();
5341 }
5342 if (pid == -1)
5343 return posix_error();
5344 if (result < 0) {
5345 /* Don't clobber the OSError if the fork failed. */
5346 PyErr_SetString(PyExc_RuntimeError,
5347 "not holding the import lock");
5348 return NULL;
5349 }
5350 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005351}
Larry Hastings2f936352014-08-05 14:04:04 +10005352#endif /* HAVE_FORK1 */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005353
5354
Guido van Rossumad0ee831995-03-01 10:34:45 +00005355#ifdef HAVE_FORK
Larry Hastings2f936352014-08-05 14:04:04 +10005356/*[clinic input]
5357os.fork
5358
5359Fork a child process.
5360
5361Return 0 to child process and PID of child to parent process.
5362[clinic start generated code]*/
5363
Larry Hastings2f936352014-08-05 14:04:04 +10005364static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005365os_fork_impl(PyObject *module)
5366/*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00005367{
Victor Stinner8c62be82010-05-06 00:08:46 +00005368 pid_t pid;
5369 int result = 0;
5370 _PyImport_AcquireLock();
5371 pid = fork();
5372 if (pid == 0) {
5373 /* child: this clobbers and resets the import lock. */
5374 PyOS_AfterFork();
5375 } else {
5376 /* parent: release the import lock. */
5377 result = _PyImport_ReleaseLock();
5378 }
5379 if (pid == -1)
5380 return posix_error();
5381 if (result < 0) {
5382 /* Don't clobber the OSError if the fork failed. */
5383 PyErr_SetString(PyExc_RuntimeError,
5384 "not holding the import lock");
5385 return NULL;
5386 }
5387 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00005388}
Larry Hastings2f936352014-08-05 14:04:04 +10005389#endif /* HAVE_FORK */
5390
Guido van Rossum85e3b011991-06-03 12:42:10 +00005391
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005392#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005393#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Larry Hastings2f936352014-08-05 14:04:04 +10005394/*[clinic input]
5395os.sched_get_priority_max
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005396
Larry Hastings2f936352014-08-05 14:04:04 +10005397 policy: int
5398
5399Get the maximum scheduling priority for policy.
5400[clinic start generated code]*/
5401
Larry Hastings2f936352014-08-05 14:04:04 +10005402static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005403os_sched_get_priority_max_impl(PyObject *module, int policy)
5404/*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005405{
5406 int max;
5407
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005408 max = sched_get_priority_max(policy);
5409 if (max < 0)
5410 return posix_error();
5411 return PyLong_FromLong(max);
5412}
5413
Larry Hastings2f936352014-08-05 14:04:04 +10005414
5415/*[clinic input]
5416os.sched_get_priority_min
5417
5418 policy: int
5419
5420Get the minimum scheduling priority for policy.
5421[clinic start generated code]*/
5422
Larry Hastings2f936352014-08-05 14:04:04 +10005423static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005424os_sched_get_priority_min_impl(PyObject *module, int policy)
5425/*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005426{
5427 int min = sched_get_priority_min(policy);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005428 if (min < 0)
5429 return posix_error();
5430 return PyLong_FromLong(min);
5431}
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005432#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
5433
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005434
Larry Hastings2f936352014-08-05 14:04:04 +10005435#ifdef HAVE_SCHED_SETSCHEDULER
5436/*[clinic input]
5437os.sched_getscheduler
5438 pid: pid_t
5439 /
5440
5441Get the scheduling policy for the process identifiedy by pid.
5442
5443Passing 0 for pid returns the scheduling policy for the calling process.
5444[clinic start generated code]*/
5445
Larry Hastings2f936352014-08-05 14:04:04 +10005446static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005447os_sched_getscheduler_impl(PyObject *module, pid_t pid)
5448/*[clinic end generated code: output=dce4c0bd3f1b34c8 input=5f14cfd1f189e1a0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005449{
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005450 int policy;
5451
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005452 policy = sched_getscheduler(pid);
5453 if (policy < 0)
5454 return posix_error();
5455 return PyLong_FromLong(policy);
5456}
Larry Hastings2f936352014-08-05 14:04:04 +10005457#endif /* HAVE_SCHED_SETSCHEDULER */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005458
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005459
5460#if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM)
Larry Hastings2f936352014-08-05 14:04:04 +10005461/*[clinic input]
5462class os.sched_param "PyObject *" "&SchedParamType"
5463
5464@classmethod
5465os.sched_param.__new__
5466
5467 sched_priority: object
5468 A scheduling parameter.
5469
5470Current has only one field: sched_priority");
5471[clinic start generated code]*/
5472
Larry Hastings2f936352014-08-05 14:04:04 +10005473static PyObject *
5474os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005475/*[clinic end generated code: output=48f4067d60f48c13 input=73a4c22f7071fc62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005476{
5477 PyObject *res;
5478
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005479 res = PyStructSequence_New(type);
5480 if (!res)
5481 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10005482 Py_INCREF(sched_priority);
5483 PyStructSequence_SET_ITEM(res, 0, sched_priority);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005484 return res;
5485}
5486
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005487
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005488PyDoc_VAR(os_sched_param__doc__);
5489
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005490static PyStructSequence_Field sched_param_fields[] = {
5491 {"sched_priority", "the scheduling priority"},
5492 {0}
5493};
5494
5495static PyStructSequence_Desc sched_param_desc = {
5496 "sched_param", /* name */
Larry Hastings2f936352014-08-05 14:04:04 +10005497 os_sched_param__doc__, /* doc */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005498 sched_param_fields,
5499 1
5500};
5501
5502static int
5503convert_sched_param(PyObject *param, struct sched_param *res)
5504{
5505 long priority;
5506
5507 if (Py_TYPE(param) != &SchedParamType) {
5508 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
5509 return 0;
5510 }
5511 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
5512 if (priority == -1 && PyErr_Occurred())
5513 return 0;
5514 if (priority > INT_MAX || priority < INT_MIN) {
5515 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
5516 return 0;
5517 }
5518 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
5519 return 1;
5520}
Larry Hastings2f936352014-08-05 14:04:04 +10005521#endif /* defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005522
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005523
5524#ifdef HAVE_SCHED_SETSCHEDULER
Larry Hastings2f936352014-08-05 14:04:04 +10005525/*[clinic input]
5526os.sched_setscheduler
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005527
Larry Hastings2f936352014-08-05 14:04:04 +10005528 pid: pid_t
5529 policy: int
5530 param: sched_param
5531 /
5532
5533Set the scheduling policy for the process identified by pid.
5534
5535If pid is 0, the calling process is changed.
5536param is an instance of sched_param.
5537[clinic start generated code]*/
5538
Larry Hastings2f936352014-08-05 14:04:04 +10005539static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005540os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Larry Hastings89964c42015-04-14 18:07:59 -04005541 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005542/*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005543{
Jesus Cea9c822272011-09-10 01:40:52 +02005544 /*
Jesus Cea54b01492011-09-10 01:53:19 +02005545 ** sched_setscheduler() returns 0 in Linux, but the previous
5546 ** scheduling policy under Solaris/Illumos, and others.
5547 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02005548 */
Larry Hastings2f936352014-08-05 14:04:04 +10005549 if (sched_setscheduler(pid, policy, param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005550 return posix_error();
5551 Py_RETURN_NONE;
5552}
Larry Hastings2f936352014-08-05 14:04:04 +10005553#endif /* HAVE_SCHED_SETSCHEDULER*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005554
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005555
5556#ifdef HAVE_SCHED_SETPARAM
Larry Hastings2f936352014-08-05 14:04:04 +10005557/*[clinic input]
5558os.sched_getparam
5559 pid: pid_t
5560 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005561
Larry Hastings2f936352014-08-05 14:04:04 +10005562Returns scheduling parameters for the process identified by pid.
5563
5564If pid is 0, returns parameters for the calling process.
5565Return value is an instance of sched_param.
5566[clinic start generated code]*/
5567
Larry Hastings2f936352014-08-05 14:04:04 +10005568static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005569os_sched_getparam_impl(PyObject *module, pid_t pid)
5570/*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005571{
5572 struct sched_param param;
5573 PyObject *result;
5574 PyObject *priority;
5575
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005576 if (sched_getparam(pid, &param))
5577 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10005578 result = PyStructSequence_New(&SchedParamType);
5579 if (!result)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005580 return NULL;
5581 priority = PyLong_FromLong(param.sched_priority);
5582 if (!priority) {
Larry Hastings2f936352014-08-05 14:04:04 +10005583 Py_DECREF(result);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005584 return NULL;
5585 }
Larry Hastings2f936352014-08-05 14:04:04 +10005586 PyStructSequence_SET_ITEM(result, 0, priority);
5587 return result;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005588}
5589
Larry Hastings2f936352014-08-05 14:04:04 +10005590
5591/*[clinic input]
5592os.sched_setparam
5593 pid: pid_t
5594 param: sched_param
5595 /
5596
5597Set scheduling parameters for the process identified by pid.
5598
5599If pid is 0, sets parameters for the calling process.
5600param should be an instance of sched_param.
5601[clinic start generated code]*/
5602
Larry Hastings2f936352014-08-05 14:04:04 +10005603static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005604os_sched_setparam_impl(PyObject *module, pid_t pid,
Larry Hastings89964c42015-04-14 18:07:59 -04005605 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005606/*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005607{
5608 if (sched_setparam(pid, param))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005609 return posix_error();
5610 Py_RETURN_NONE;
5611}
Larry Hastings2f936352014-08-05 14:04:04 +10005612#endif /* HAVE_SCHED_SETPARAM */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005613
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005614
5615#ifdef HAVE_SCHED_RR_GET_INTERVAL
Larry Hastings2f936352014-08-05 14:04:04 +10005616/*[clinic input]
5617os.sched_rr_get_interval -> double
5618 pid: pid_t
5619 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005620
Larry Hastings2f936352014-08-05 14:04:04 +10005621Return the round-robin quantum for the process identified by pid, in seconds.
5622
5623Value returned is a float.
5624[clinic start generated code]*/
5625
Larry Hastings2f936352014-08-05 14:04:04 +10005626static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005627os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
5628/*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005629{
5630 struct timespec interval;
5631 if (sched_rr_get_interval(pid, &interval)) {
5632 posix_error();
5633 return -1.0;
5634 }
5635 return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
5636}
5637#endif /* HAVE_SCHED_RR_GET_INTERVAL */
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005638
Larry Hastings2f936352014-08-05 14:04:04 +10005639
5640/*[clinic input]
5641os.sched_yield
5642
5643Voluntarily relinquish the CPU.
5644[clinic start generated code]*/
5645
Larry Hastings2f936352014-08-05 14:04:04 +10005646static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005647os_sched_yield_impl(PyObject *module)
5648/*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005649{
5650 if (sched_yield())
5651 return posix_error();
5652 Py_RETURN_NONE;
5653}
5654
Benjamin Peterson2740af82011-08-02 17:41:34 -05005655#ifdef HAVE_SCHED_SETAFFINITY
Antoine Pitrou84869872012-08-04 16:16:35 +02005656/* The minimum number of CPUs allocated in a cpu_set_t */
5657static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005658
Larry Hastings2f936352014-08-05 14:04:04 +10005659/*[clinic input]
5660os.sched_setaffinity
5661 pid: pid_t
5662 mask : object
5663 /
5664
5665Set the CPU affinity of the process identified by pid to mask.
5666
5667mask should be an iterable of integers identifying CPUs.
5668[clinic start generated code]*/
5669
Larry Hastings2f936352014-08-05 14:04:04 +10005670static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005671os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
5672/*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005673{
Antoine Pitrou84869872012-08-04 16:16:35 +02005674 int ncpus;
5675 size_t setsize;
Larry Hastings2f936352014-08-05 14:04:04 +10005676 cpu_set_t *cpu_set = NULL;
5677 PyObject *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005678
Larry Hastings2f936352014-08-05 14:04:04 +10005679 iterator = PyObject_GetIter(mask);
Antoine Pitrou84869872012-08-04 16:16:35 +02005680 if (iterator == NULL)
5681 return NULL;
5682
5683 ncpus = NCPUS_START;
5684 setsize = CPU_ALLOC_SIZE(ncpus);
Larry Hastings2f936352014-08-05 14:04:04 +10005685 cpu_set = CPU_ALLOC(ncpus);
5686 if (cpu_set == NULL) {
Antoine Pitrou84869872012-08-04 16:16:35 +02005687 PyErr_NoMemory();
5688 goto error;
5689 }
Larry Hastings2f936352014-08-05 14:04:04 +10005690 CPU_ZERO_S(setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02005691
5692 while ((item = PyIter_Next(iterator))) {
5693 long cpu;
5694 if (!PyLong_Check(item)) {
5695 PyErr_Format(PyExc_TypeError,
5696 "expected an iterator of ints, "
5697 "but iterator yielded %R",
5698 Py_TYPE(item));
5699 Py_DECREF(item);
5700 goto error;
5701 }
5702 cpu = PyLong_AsLong(item);
5703 Py_DECREF(item);
5704 if (cpu < 0) {
5705 if (!PyErr_Occurred())
5706 PyErr_SetString(PyExc_ValueError, "negative CPU number");
5707 goto error;
5708 }
5709 if (cpu > INT_MAX - 1) {
5710 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
5711 goto error;
5712 }
5713 if (cpu >= ncpus) {
5714 /* Grow CPU mask to fit the CPU number */
5715 int newncpus = ncpus;
5716 cpu_set_t *newmask;
5717 size_t newsetsize;
5718 while (newncpus <= cpu) {
5719 if (newncpus > INT_MAX / 2)
5720 newncpus = cpu + 1;
5721 else
5722 newncpus = newncpus * 2;
5723 }
5724 newmask = CPU_ALLOC(newncpus);
5725 if (newmask == NULL) {
5726 PyErr_NoMemory();
5727 goto error;
5728 }
5729 newsetsize = CPU_ALLOC_SIZE(newncpus);
5730 CPU_ZERO_S(newsetsize, newmask);
Larry Hastings2f936352014-08-05 14:04:04 +10005731 memcpy(newmask, cpu_set, setsize);
5732 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02005733 setsize = newsetsize;
Larry Hastings2f936352014-08-05 14:04:04 +10005734 cpu_set = newmask;
Antoine Pitrou84869872012-08-04 16:16:35 +02005735 ncpus = newncpus;
5736 }
Larry Hastings2f936352014-08-05 14:04:04 +10005737 CPU_SET_S(cpu, setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02005738 }
5739 Py_CLEAR(iterator);
5740
Larry Hastings2f936352014-08-05 14:04:04 +10005741 if (sched_setaffinity(pid, setsize, cpu_set)) {
Antoine Pitrou84869872012-08-04 16:16:35 +02005742 posix_error();
5743 goto error;
5744 }
Larry Hastings2f936352014-08-05 14:04:04 +10005745 CPU_FREE(cpu_set);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005746 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02005747
5748error:
Larry Hastings2f936352014-08-05 14:04:04 +10005749 if (cpu_set)
5750 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02005751 Py_XDECREF(iterator);
5752 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005753}
5754
Larry Hastings2f936352014-08-05 14:04:04 +10005755
5756/*[clinic input]
5757os.sched_getaffinity
5758 pid: pid_t
5759 /
5760
5761Return the affinity of the process identified by pid.
5762
5763The affinity is returned as a set of CPU identifiers.
5764[clinic start generated code]*/
5765
Larry Hastings2f936352014-08-05 14:04:04 +10005766static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005767os_sched_getaffinity_impl(PyObject *module, pid_t pid)
5768/*[clinic end generated code: output=f726f2c193c17a4f input=eaf161936874b8a1]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005769{
Antoine Pitrou84869872012-08-04 16:16:35 +02005770 int cpu, ncpus, count;
5771 size_t setsize;
5772 cpu_set_t *mask = NULL;
5773 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005774
Antoine Pitrou84869872012-08-04 16:16:35 +02005775 ncpus = NCPUS_START;
5776 while (1) {
5777 setsize = CPU_ALLOC_SIZE(ncpus);
5778 mask = CPU_ALLOC(ncpus);
5779 if (mask == NULL)
5780 return PyErr_NoMemory();
5781 if (sched_getaffinity(pid, setsize, mask) == 0)
5782 break;
5783 CPU_FREE(mask);
5784 if (errno != EINVAL)
5785 return posix_error();
5786 if (ncpus > INT_MAX / 2) {
5787 PyErr_SetString(PyExc_OverflowError, "could not allocate "
5788 "a large enough CPU set");
5789 return NULL;
5790 }
5791 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005792 }
Antoine Pitrou84869872012-08-04 16:16:35 +02005793
5794 res = PySet_New(NULL);
5795 if (res == NULL)
5796 goto error;
5797 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
5798 if (CPU_ISSET_S(cpu, setsize, mask)) {
5799 PyObject *cpu_num = PyLong_FromLong(cpu);
5800 --count;
5801 if (cpu_num == NULL)
5802 goto error;
5803 if (PySet_Add(res, cpu_num)) {
5804 Py_DECREF(cpu_num);
5805 goto error;
5806 }
5807 Py_DECREF(cpu_num);
5808 }
5809 }
5810 CPU_FREE(mask);
5811 return res;
5812
5813error:
5814 if (mask)
5815 CPU_FREE(mask);
5816 Py_XDECREF(res);
5817 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005818}
5819
Benjamin Peterson2740af82011-08-02 17:41:34 -05005820#endif /* HAVE_SCHED_SETAFFINITY */
5821
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005822#endif /* HAVE_SCHED_H */
5823
Larry Hastings2f936352014-08-05 14:04:04 +10005824
Neal Norwitzb59798b2003-03-21 01:43:31 +00005825/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005826/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5827#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005828#define DEV_PTY_FILE "/dev/ptc"
5829#define HAVE_DEV_PTMX
5830#else
5831#define DEV_PTY_FILE "/dev/ptmx"
5832#endif
5833
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005834#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005835#ifdef HAVE_PTY_H
5836#include <pty.h>
5837#else
5838#ifdef HAVE_LIBUTIL_H
5839#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005840#else
5841#ifdef HAVE_UTIL_H
5842#include <util.h>
5843#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005844#endif /* HAVE_LIBUTIL_H */
5845#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005846#ifdef HAVE_STROPTS_H
5847#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005848#endif
5849#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005850
Larry Hastings2f936352014-08-05 14:04:04 +10005851
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005852#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Larry Hastings2f936352014-08-05 14:04:04 +10005853/*[clinic input]
5854os.openpty
5855
5856Open a pseudo-terminal.
5857
5858Return a tuple of (master_fd, slave_fd) containing open file descriptors
5859for both the master and slave ends.
5860[clinic start generated code]*/
5861
Larry Hastings2f936352014-08-05 14:04:04 +10005862static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005863os_openpty_impl(PyObject *module)
5864/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00005865{
Victor Stinnerdaf45552013-08-28 00:53:59 +02005866 int master_fd = -1, slave_fd = -1;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005867#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005868 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005869#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005870#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005871 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005872#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00005873 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005874#endif
5875#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005876
Thomas Wouters70c21a12000-07-14 14:28:33 +00005877#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005878 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02005879 goto posix_error;
5880
5881 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
5882 goto error;
5883 if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
5884 goto error;
5885
Neal Norwitzb59798b2003-03-21 01:43:31 +00005886#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005887 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5888 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02005889 goto posix_error;
5890 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
5891 goto error;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005892
Victor Stinnerdaf45552013-08-28 00:53:59 +02005893 slave_fd = _Py_open(slave_name, O_RDWR);
Victor Stinner8c62be82010-05-06 00:08:46 +00005894 if (slave_fd < 0)
Victor Stinnera555cfc2015-03-18 00:22:14 +01005895 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +02005896
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005897#else
Victor Stinner000de532013-11-25 23:19:58 +01005898 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Victor Stinner8c62be82010-05-06 00:08:46 +00005899 if (master_fd < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02005900 goto posix_error;
5901
Victor Stinner8c62be82010-05-06 00:08:46 +00005902 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02005903
Victor Stinner8c62be82010-05-06 00:08:46 +00005904 /* change permission of slave */
5905 if (grantpt(master_fd) < 0) {
5906 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02005907 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005908 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02005909
Victor Stinner8c62be82010-05-06 00:08:46 +00005910 /* unlock slave */
5911 if (unlockpt(master_fd) < 0) {
5912 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02005913 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005914 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02005915
Victor Stinner8c62be82010-05-06 00:08:46 +00005916 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02005917
Victor Stinner8c62be82010-05-06 00:08:46 +00005918 slave_name = ptsname(master_fd); /* get name of slave */
5919 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02005920 goto posix_error;
5921
5922 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
Victor Stinnera555cfc2015-03-18 00:22:14 +01005923 if (slave_fd == -1)
5924 goto error;
Victor Stinner000de532013-11-25 23:19:58 +01005925
5926 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
5927 goto posix_error;
5928
Neal Norwitzb59798b2003-03-21 01:43:31 +00005929#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005930 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
5931 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00005932#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00005933 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00005934#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005935#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00005936#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005937
Victor Stinner8c62be82010-05-06 00:08:46 +00005938 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00005939
Victor Stinnerdaf45552013-08-28 00:53:59 +02005940posix_error:
5941 posix_error();
5942error:
5943 if (master_fd != -1)
5944 close(master_fd);
5945 if (slave_fd != -1)
5946 close(slave_fd);
5947 return NULL;
Fred Drake8cef4cf2000-06-28 16:40:38 +00005948}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005949#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005950
Larry Hastings2f936352014-08-05 14:04:04 +10005951
Fred Drake8cef4cf2000-06-28 16:40:38 +00005952#ifdef HAVE_FORKPTY
Larry Hastings2f936352014-08-05 14:04:04 +10005953/*[clinic input]
5954os.forkpty
5955
5956Fork a new process with a new pseudo-terminal as controlling tty.
5957
5958Returns a tuple of (pid, master_fd).
5959Like fork(), return pid of 0 to the child process,
5960and pid of child to the parent process.
5961To both, return fd of newly opened pseudo-terminal.
5962[clinic start generated code]*/
5963
Larry Hastings2f936352014-08-05 14:04:04 +10005964static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005965os_forkpty_impl(PyObject *module)
5966/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00005967{
Victor Stinner8c62be82010-05-06 00:08:46 +00005968 int master_fd = -1, result = 0;
5969 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00005970
Victor Stinner8c62be82010-05-06 00:08:46 +00005971 _PyImport_AcquireLock();
5972 pid = forkpty(&master_fd, NULL, NULL, NULL);
5973 if (pid == 0) {
5974 /* child: this clobbers and resets the import lock. */
5975 PyOS_AfterFork();
5976 } else {
5977 /* parent: release the import lock. */
5978 result = _PyImport_ReleaseLock();
5979 }
5980 if (pid == -1)
5981 return posix_error();
5982 if (result < 0) {
5983 /* Don't clobber the OSError if the fork failed. */
5984 PyErr_SetString(PyExc_RuntimeError,
5985 "not holding the import lock");
5986 return NULL;
5987 }
5988 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00005989}
Larry Hastings2f936352014-08-05 14:04:04 +10005990#endif /* HAVE_FORKPTY */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005991
Ross Lagerwall7807c352011-03-17 20:20:30 +02005992
Guido van Rossumad0ee831995-03-01 10:34:45 +00005993#ifdef HAVE_GETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10005994/*[clinic input]
5995os.getegid
5996
5997Return the current process's effective group id.
5998[clinic start generated code]*/
5999
Larry Hastings2f936352014-08-05 14:04:04 +10006000static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006001os_getegid_impl(PyObject *module)
6002/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006003{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006004 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006005}
Larry Hastings2f936352014-08-05 14:04:04 +10006006#endif /* HAVE_GETEGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006007
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006008
Guido van Rossumad0ee831995-03-01 10:34:45 +00006009#ifdef HAVE_GETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006010/*[clinic input]
6011os.geteuid
6012
6013Return the current process's effective user id.
6014[clinic start generated code]*/
6015
Larry Hastings2f936352014-08-05 14:04:04 +10006016static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006017os_geteuid_impl(PyObject *module)
6018/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006019{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006020 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006021}
Larry Hastings2f936352014-08-05 14:04:04 +10006022#endif /* HAVE_GETEUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006023
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006024
Guido van Rossumad0ee831995-03-01 10:34:45 +00006025#ifdef HAVE_GETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006026/*[clinic input]
6027os.getgid
6028
6029Return the current process's group id.
6030[clinic start generated code]*/
6031
Larry Hastings2f936352014-08-05 14:04:04 +10006032static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006033os_getgid_impl(PyObject *module)
6034/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006035{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006036 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006037}
Larry Hastings2f936352014-08-05 14:04:04 +10006038#endif /* HAVE_GETGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006039
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006040
Larry Hastings2f936352014-08-05 14:04:04 +10006041/*[clinic input]
6042os.getpid
6043
6044Return the current process id.
6045[clinic start generated code]*/
6046
Larry Hastings2f936352014-08-05 14:04:04 +10006047static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006048os_getpid_impl(PyObject *module)
6049/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006050{
Victor Stinner8c62be82010-05-06 00:08:46 +00006051 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00006052}
6053
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006054#ifdef HAVE_GETGROUPLIST
Larry Hastings2f936352014-08-05 14:04:04 +10006055
6056/* AC 3.5: funny apple logic below */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006057PyDoc_STRVAR(posix_getgrouplist__doc__,
6058"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
6059Returns a list of groups to which a user belongs.\n\n\
6060 user: username to lookup\n\
6061 group: base group id of the user");
6062
6063static PyObject *
6064posix_getgrouplist(PyObject *self, PyObject *args)
6065{
6066#ifdef NGROUPS_MAX
6067#define MAX_GROUPS NGROUPS_MAX
6068#else
6069 /* defined to be 16 on Solaris7, so this should be a small number */
6070#define MAX_GROUPS 64
6071#endif
6072
6073 const char *user;
6074 int i, ngroups;
6075 PyObject *list;
6076#ifdef __APPLE__
6077 int *groups, basegid;
6078#else
6079 gid_t *groups, basegid;
6080#endif
6081 ngroups = MAX_GROUPS;
6082
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006083#ifdef __APPLE__
6084 if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006085 return NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006086#else
6087 if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user,
6088 _Py_Gid_Converter, &basegid))
6089 return NULL;
6090#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006091
6092#ifdef __APPLE__
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006093 groups = PyMem_New(int, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006094#else
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006095 groups = PyMem_New(gid_t, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006096#endif
6097 if (groups == NULL)
6098 return PyErr_NoMemory();
6099
6100 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
6101 PyMem_Del(groups);
6102 return posix_error();
6103 }
6104
6105 list = PyList_New(ngroups);
6106 if (list == NULL) {
6107 PyMem_Del(groups);
6108 return NULL;
6109 }
6110
6111 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006112#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006113 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006114#else
6115 PyObject *o = _PyLong_FromGid(groups[i]);
6116#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006117 if (o == NULL) {
6118 Py_DECREF(list);
6119 PyMem_Del(groups);
6120 return NULL;
6121 }
6122 PyList_SET_ITEM(list, i, o);
6123 }
6124
6125 PyMem_Del(groups);
6126
6127 return list;
6128}
Larry Hastings2f936352014-08-05 14:04:04 +10006129#endif /* HAVE_GETGROUPLIST */
6130
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006131
Fred Drakec9680921999-12-13 16:37:25 +00006132#ifdef HAVE_GETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006133/*[clinic input]
6134os.getgroups
6135
6136Return list of supplemental group IDs for the process.
6137[clinic start generated code]*/
6138
Larry Hastings2f936352014-08-05 14:04:04 +10006139static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006140os_getgroups_impl(PyObject *module)
6141/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
Fred Drakec9680921999-12-13 16:37:25 +00006142{
6143 PyObject *result = NULL;
6144
Fred Drakec9680921999-12-13 16:37:25 +00006145#ifdef NGROUPS_MAX
6146#define MAX_GROUPS NGROUPS_MAX
6147#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006148 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00006149#define MAX_GROUPS 64
6150#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006151 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006152
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006153 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006154 * This is a helper variable to store the intermediate result when
6155 * that happens.
6156 *
6157 * To keep the code readable the OSX behaviour is unconditional,
6158 * according to the POSIX spec this should be safe on all unix-y
6159 * systems.
6160 */
6161 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006162 int n;
Fred Drakec9680921999-12-13 16:37:25 +00006163
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006164#ifdef __APPLE__
6165 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
6166 * there are more groups than can fit in grouplist. Therefore, on OS X
6167 * always first call getgroups with length 0 to get the actual number
6168 * of groups.
6169 */
6170 n = getgroups(0, NULL);
6171 if (n < 0) {
6172 return posix_error();
6173 } else if (n <= MAX_GROUPS) {
6174 /* groups will fit in existing array */
6175 alt_grouplist = grouplist;
6176 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006177 alt_grouplist = PyMem_New(gid_t, n);
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006178 if (alt_grouplist == NULL) {
6179 errno = EINVAL;
6180 return posix_error();
6181 }
6182 }
6183
6184 n = getgroups(n, alt_grouplist);
6185 if (n == -1) {
6186 if (alt_grouplist != grouplist) {
6187 PyMem_Free(alt_grouplist);
6188 }
6189 return posix_error();
6190 }
6191#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006192 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006193 if (n < 0) {
6194 if (errno == EINVAL) {
6195 n = getgroups(0, NULL);
6196 if (n == -1) {
6197 return posix_error();
6198 }
6199 if (n == 0) {
6200 /* Avoid malloc(0) */
6201 alt_grouplist = grouplist;
6202 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006203 alt_grouplist = PyMem_New(gid_t, n);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006204 if (alt_grouplist == NULL) {
6205 errno = EINVAL;
6206 return posix_error();
6207 }
6208 n = getgroups(n, alt_grouplist);
6209 if (n == -1) {
6210 PyMem_Free(alt_grouplist);
6211 return posix_error();
6212 }
6213 }
6214 } else {
6215 return posix_error();
6216 }
6217 }
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006218#endif
6219
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006220 result = PyList_New(n);
6221 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006222 int i;
6223 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006224 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00006225 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006226 Py_DECREF(result);
6227 result = NULL;
6228 break;
Fred Drakec9680921999-12-13 16:37:25 +00006229 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006230 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00006231 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006232 }
6233
6234 if (alt_grouplist != grouplist) {
6235 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00006236 }
Neal Norwitze241ce82003-02-17 18:17:05 +00006237
Fred Drakec9680921999-12-13 16:37:25 +00006238 return result;
6239}
Larry Hastings2f936352014-08-05 14:04:04 +10006240#endif /* HAVE_GETGROUPS */
Fred Drakec9680921999-12-13 16:37:25 +00006241
Antoine Pitroub7572f02009-12-02 20:46:48 +00006242#ifdef HAVE_INITGROUPS
6243PyDoc_STRVAR(posix_initgroups__doc__,
6244"initgroups(username, gid) -> None\n\n\
6245Call the system initgroups() to initialize the group access list with all of\n\
6246the groups of which the specified username is a member, plus the specified\n\
6247group id.");
6248
Larry Hastings2f936352014-08-05 14:04:04 +10006249/* AC 3.5: funny apple logic */
Antoine Pitroub7572f02009-12-02 20:46:48 +00006250static PyObject *
6251posix_initgroups(PyObject *self, PyObject *args)
6252{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006253 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00006254 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006255 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006256#ifdef __APPLE__
6257 int gid;
6258#else
6259 gid_t gid;
6260#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00006261
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006262#ifdef __APPLE__
6263 if (!PyArg_ParseTuple(args, "O&i:initgroups",
6264 PyUnicode_FSConverter, &oname,
6265 &gid))
6266#else
6267 if (!PyArg_ParseTuple(args, "O&O&:initgroups",
6268 PyUnicode_FSConverter, &oname,
6269 _Py_Gid_Converter, &gid))
6270#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006271 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006272 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006273
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006274 res = initgroups(username, gid);
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006275 Py_DECREF(oname);
6276 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00006277 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006278
Victor Stinner8c62be82010-05-06 00:08:46 +00006279 Py_INCREF(Py_None);
6280 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00006281}
Larry Hastings2f936352014-08-05 14:04:04 +10006282#endif /* HAVE_INITGROUPS */
6283
Antoine Pitroub7572f02009-12-02 20:46:48 +00006284
Martin v. Löwis606edc12002-06-13 21:09:11 +00006285#ifdef HAVE_GETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10006286/*[clinic input]
6287os.getpgid
6288
6289 pid: pid_t
6290
6291Call the system call getpgid(), and return the result.
6292[clinic start generated code]*/
6293
Larry Hastings2f936352014-08-05 14:04:04 +10006294static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006295os_getpgid_impl(PyObject *module, pid_t pid)
6296/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006297{
6298 pid_t pgid = getpgid(pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00006299 if (pgid < 0)
6300 return posix_error();
6301 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00006302}
6303#endif /* HAVE_GETPGID */
6304
6305
Guido van Rossumb6775db1994-08-01 11:34:53 +00006306#ifdef HAVE_GETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10006307/*[clinic input]
6308os.getpgrp
6309
6310Return the current process group id.
6311[clinic start generated code]*/
6312
Larry Hastings2f936352014-08-05 14:04:04 +10006313static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006314os_getpgrp_impl(PyObject *module)
6315/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
Guido van Rossum04814471991-06-04 20:23:49 +00006316{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006317#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006318 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006319#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006320 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006321#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00006322}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006323#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00006324
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006325
Guido van Rossumb6775db1994-08-01 11:34:53 +00006326#ifdef HAVE_SETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10006327/*[clinic input]
6328os.setpgrp
6329
6330Make the current process the leader of its process group.
6331[clinic start generated code]*/
6332
Larry Hastings2f936352014-08-05 14:04:04 +10006333static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006334os_setpgrp_impl(PyObject *module)
6335/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00006336{
Guido van Rossum64933891994-10-20 21:56:42 +00006337#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006338 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006339#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006340 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006341#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006342 return posix_error();
6343 Py_INCREF(Py_None);
6344 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006345}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006346#endif /* HAVE_SETPGRP */
6347
Guido van Rossumad0ee831995-03-01 10:34:45 +00006348#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006349
6350#ifdef MS_WINDOWS
6351#include <tlhelp32.h>
6352
6353static PyObject*
6354win32_getppid()
6355{
6356 HANDLE snapshot;
6357 pid_t mypid;
6358 PyObject* result = NULL;
6359 BOOL have_record;
6360 PROCESSENTRY32 pe;
6361
6362 mypid = getpid(); /* This function never fails */
6363
6364 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
6365 if (snapshot == INVALID_HANDLE_VALUE)
6366 return PyErr_SetFromWindowsErr(GetLastError());
6367
6368 pe.dwSize = sizeof(pe);
6369 have_record = Process32First(snapshot, &pe);
6370 while (have_record) {
6371 if (mypid == (pid_t)pe.th32ProcessID) {
6372 /* We could cache the ulong value in a static variable. */
6373 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
6374 break;
6375 }
6376
6377 have_record = Process32Next(snapshot, &pe);
6378 }
6379
6380 /* If our loop exits and our pid was not found (result will be NULL)
6381 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
6382 * error anyway, so let's raise it. */
6383 if (!result)
6384 result = PyErr_SetFromWindowsErr(GetLastError());
6385
6386 CloseHandle(snapshot);
6387
6388 return result;
6389}
6390#endif /*MS_WINDOWS*/
6391
Larry Hastings2f936352014-08-05 14:04:04 +10006392
6393/*[clinic input]
6394os.getppid
6395
6396Return the parent's process id.
6397
6398If the parent process has already exited, Windows machines will still
6399return its id; others systems will return the id of the 'init' process (1).
6400[clinic start generated code]*/
6401
Larry Hastings2f936352014-08-05 14:04:04 +10006402static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006403os_getppid_impl(PyObject *module)
6404/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006405{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006406#ifdef MS_WINDOWS
6407 return win32_getppid();
6408#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006409 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00006410#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006411}
6412#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006413
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006414
Fred Drake12c6e2d1999-12-14 21:25:03 +00006415#ifdef HAVE_GETLOGIN
Larry Hastings2f936352014-08-05 14:04:04 +10006416/*[clinic input]
6417os.getlogin
6418
6419Return the actual login name.
6420[clinic start generated code]*/
6421
Larry Hastings2f936352014-08-05 14:04:04 +10006422static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006423os_getlogin_impl(PyObject *module)
6424/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
Fred Drake12c6e2d1999-12-14 21:25:03 +00006425{
Victor Stinner8c62be82010-05-06 00:08:46 +00006426 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006427#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006428 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02006429 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006430
6431 if (GetUserNameW(user_name, &num_chars)) {
6432 /* num_chars is the number of unicode chars plus null terminator */
6433 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006434 }
6435 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006436 result = PyErr_SetFromWindowsErr(GetLastError());
6437#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006438 char *name;
6439 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006440
Victor Stinner8c62be82010-05-06 00:08:46 +00006441 errno = 0;
6442 name = getlogin();
6443 if (name == NULL) {
6444 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00006445 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00006446 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00006447 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00006448 }
6449 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00006450 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00006451 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006452#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00006453 return result;
6454}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006455#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006456
Larry Hastings2f936352014-08-05 14:04:04 +10006457
Guido van Rossumad0ee831995-03-01 10:34:45 +00006458#ifdef HAVE_GETUID
Larry Hastings2f936352014-08-05 14:04:04 +10006459/*[clinic input]
6460os.getuid
6461
6462Return the current process's user id.
6463[clinic start generated code]*/
6464
Larry Hastings2f936352014-08-05 14:04:04 +10006465static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006466os_getuid_impl(PyObject *module)
6467/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006468{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006469 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006470}
Larry Hastings2f936352014-08-05 14:04:04 +10006471#endif /* HAVE_GETUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006472
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006473
Brian Curtineb24d742010-04-12 17:16:38 +00006474#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10006475#define HAVE_KILL
6476#endif /* MS_WINDOWS */
6477
6478#ifdef HAVE_KILL
6479/*[clinic input]
6480os.kill
6481
6482 pid: pid_t
6483 signal: Py_ssize_t
6484 /
6485
6486Kill a process with a signal.
6487[clinic start generated code]*/
6488
Larry Hastings2f936352014-08-05 14:04:04 +10006489static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006490os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
6491/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006492#ifndef MS_WINDOWS
6493{
6494 if (kill(pid, (int)signal) == -1)
6495 return posix_error();
6496 Py_RETURN_NONE;
6497}
6498#else /* !MS_WINDOWS */
Brian Curtineb24d742010-04-12 17:16:38 +00006499{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00006500 PyObject *result;
Larry Hastings2f936352014-08-05 14:04:04 +10006501 DWORD sig = (DWORD)signal;
6502 DWORD err;
Victor Stinner8c62be82010-05-06 00:08:46 +00006503 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00006504
Victor Stinner8c62be82010-05-06 00:08:46 +00006505 /* Console processes which share a common console can be sent CTRL+C or
6506 CTRL+BREAK events, provided they handle said events. */
6507 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006508 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006509 err = GetLastError();
6510 PyErr_SetFromWindowsErr(err);
6511 }
6512 else
6513 Py_RETURN_NONE;
6514 }
Brian Curtineb24d742010-04-12 17:16:38 +00006515
Victor Stinner8c62be82010-05-06 00:08:46 +00006516 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
6517 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006518 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00006519 if (handle == NULL) {
6520 err = GetLastError();
6521 return PyErr_SetFromWindowsErr(err);
6522 }
Brian Curtineb24d742010-04-12 17:16:38 +00006523
Victor Stinner8c62be82010-05-06 00:08:46 +00006524 if (TerminateProcess(handle, sig) == 0) {
6525 err = GetLastError();
6526 result = PyErr_SetFromWindowsErr(err);
6527 } else {
6528 Py_INCREF(Py_None);
6529 result = Py_None;
6530 }
Brian Curtineb24d742010-04-12 17:16:38 +00006531
Victor Stinner8c62be82010-05-06 00:08:46 +00006532 CloseHandle(handle);
6533 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00006534}
Larry Hastings2f936352014-08-05 14:04:04 +10006535#endif /* !MS_WINDOWS */
6536#endif /* HAVE_KILL */
6537
6538
6539#ifdef HAVE_KILLPG
6540/*[clinic input]
6541os.killpg
6542
6543 pgid: pid_t
6544 signal: int
6545 /
6546
6547Kill a process group with a signal.
6548[clinic start generated code]*/
6549
Larry Hastings2f936352014-08-05 14:04:04 +10006550static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006551os_killpg_impl(PyObject *module, pid_t pgid, int signal)
6552/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006553{
6554 /* XXX some man pages make the `pgid` parameter an int, others
6555 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
6556 take the same type. Moreover, pid_t is always at least as wide as
6557 int (else compilation of this module fails), which is safe. */
6558 if (killpg(pgid, signal) == -1)
6559 return posix_error();
6560 Py_RETURN_NONE;
6561}
6562#endif /* HAVE_KILLPG */
6563
Brian Curtineb24d742010-04-12 17:16:38 +00006564
Guido van Rossumc0125471996-06-28 18:55:32 +00006565#ifdef HAVE_PLOCK
Guido van Rossumc0125471996-06-28 18:55:32 +00006566#ifdef HAVE_SYS_LOCK_H
6567#include <sys/lock.h>
6568#endif
6569
Larry Hastings2f936352014-08-05 14:04:04 +10006570/*[clinic input]
6571os.plock
6572 op: int
6573 /
6574
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006575Lock program segments into memory.");
Larry Hastings2f936352014-08-05 14:04:04 +10006576[clinic start generated code]*/
6577
Larry Hastings2f936352014-08-05 14:04:04 +10006578static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006579os_plock_impl(PyObject *module, int op)
6580/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006581{
Victor Stinner8c62be82010-05-06 00:08:46 +00006582 if (plock(op) == -1)
6583 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006584 Py_RETURN_NONE;
Guido van Rossumc0125471996-06-28 18:55:32 +00006585}
Larry Hastings2f936352014-08-05 14:04:04 +10006586#endif /* HAVE_PLOCK */
6587
Guido van Rossumc0125471996-06-28 18:55:32 +00006588
Guido van Rossumb6775db1994-08-01 11:34:53 +00006589#ifdef HAVE_SETUID
Larry Hastings2f936352014-08-05 14:04:04 +10006590/*[clinic input]
6591os.setuid
6592
6593 uid: uid_t
6594 /
6595
6596Set the current process's user id.
6597[clinic start generated code]*/
6598
Larry Hastings2f936352014-08-05 14:04:04 +10006599static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006600os_setuid_impl(PyObject *module, uid_t uid)
6601/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006602{
Victor Stinner8c62be82010-05-06 00:08:46 +00006603 if (setuid(uid) < 0)
6604 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006605 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006606}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006607#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006608
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006609
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006610#ifdef HAVE_SETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006611/*[clinic input]
6612os.seteuid
6613
6614 euid: uid_t
6615 /
6616
6617Set the current process's effective user id.
6618[clinic start generated code]*/
6619
Larry Hastings2f936352014-08-05 14:04:04 +10006620static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006621os_seteuid_impl(PyObject *module, uid_t euid)
6622/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006623{
6624 if (seteuid(euid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00006625 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006626 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006627}
6628#endif /* HAVE_SETEUID */
6629
Larry Hastings2f936352014-08-05 14:04:04 +10006630
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006631#ifdef HAVE_SETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006632/*[clinic input]
6633os.setegid
6634
6635 egid: gid_t
6636 /
6637
6638Set the current process's effective group id.
6639[clinic start generated code]*/
6640
Larry Hastings2f936352014-08-05 14:04:04 +10006641static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006642os_setegid_impl(PyObject *module, gid_t egid)
6643/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006644{
6645 if (setegid(egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00006646 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006647 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006648}
6649#endif /* HAVE_SETEGID */
6650
Larry Hastings2f936352014-08-05 14:04:04 +10006651
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006652#ifdef HAVE_SETREUID
Larry Hastings2f936352014-08-05 14:04:04 +10006653/*[clinic input]
6654os.setreuid
6655
6656 ruid: uid_t
6657 euid: uid_t
6658 /
6659
6660Set the current process's real and effective user ids.
6661[clinic start generated code]*/
6662
Larry Hastings2f936352014-08-05 14:04:04 +10006663static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006664os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
6665/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006666{
Victor Stinner8c62be82010-05-06 00:08:46 +00006667 if (setreuid(ruid, euid) < 0) {
6668 return posix_error();
6669 } else {
6670 Py_INCREF(Py_None);
6671 return Py_None;
6672 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006673}
6674#endif /* HAVE_SETREUID */
6675
Larry Hastings2f936352014-08-05 14:04:04 +10006676
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006677#ifdef HAVE_SETREGID
Larry Hastings2f936352014-08-05 14:04:04 +10006678/*[clinic input]
6679os.setregid
6680
6681 rgid: gid_t
6682 egid: gid_t
6683 /
6684
6685Set the current process's real and effective group ids.
6686[clinic start generated code]*/
6687
Larry Hastings2f936352014-08-05 14:04:04 +10006688static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006689os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
6690/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006691{
6692 if (setregid(rgid, egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00006693 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006694 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006695}
6696#endif /* HAVE_SETREGID */
6697
Larry Hastings2f936352014-08-05 14:04:04 +10006698
Guido van Rossumb6775db1994-08-01 11:34:53 +00006699#ifdef HAVE_SETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006700/*[clinic input]
6701os.setgid
6702 gid: gid_t
6703 /
6704
6705Set the current process's group id.
6706[clinic start generated code]*/
6707
Larry Hastings2f936352014-08-05 14:04:04 +10006708static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006709os_setgid_impl(PyObject *module, gid_t gid)
6710/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006711{
Victor Stinner8c62be82010-05-06 00:08:46 +00006712 if (setgid(gid) < 0)
6713 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006714 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006715}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006716#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006717
Larry Hastings2f936352014-08-05 14:04:04 +10006718
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006719#ifdef HAVE_SETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006720/*[clinic input]
6721os.setgroups
6722
6723 groups: object
6724 /
6725
6726Set the groups of the current process to list.
6727[clinic start generated code]*/
6728
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006729static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006730os_setgroups(PyObject *module, PyObject *groups)
6731/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006732{
Victor Stinner8c62be82010-05-06 00:08:46 +00006733 int i, len;
6734 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00006735
Victor Stinner8c62be82010-05-06 00:08:46 +00006736 if (!PySequence_Check(groups)) {
6737 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
6738 return NULL;
6739 }
6740 len = PySequence_Size(groups);
6741 if (len > MAX_GROUPS) {
6742 PyErr_SetString(PyExc_ValueError, "too many groups");
6743 return NULL;
6744 }
6745 for(i = 0; i < len; i++) {
6746 PyObject *elem;
6747 elem = PySequence_GetItem(groups, i);
6748 if (!elem)
6749 return NULL;
6750 if (!PyLong_Check(elem)) {
6751 PyErr_SetString(PyExc_TypeError,
6752 "groups must be integers");
6753 Py_DECREF(elem);
6754 return NULL;
6755 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006756 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006757 Py_DECREF(elem);
6758 return NULL;
6759 }
6760 }
6761 Py_DECREF(elem);
6762 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006763
Victor Stinner8c62be82010-05-06 00:08:46 +00006764 if (setgroups(len, grouplist) < 0)
6765 return posix_error();
6766 Py_INCREF(Py_None);
6767 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006768}
6769#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006770
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006771#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
6772static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006773wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006774{
Victor Stinner8c62be82010-05-06 00:08:46 +00006775 PyObject *result;
6776 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006777 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006778
Victor Stinner8c62be82010-05-06 00:08:46 +00006779 if (pid == -1)
6780 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006781
Victor Stinner8c62be82010-05-06 00:08:46 +00006782 if (struct_rusage == NULL) {
6783 PyObject *m = PyImport_ImportModuleNoBlock("resource");
6784 if (m == NULL)
6785 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006786 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00006787 Py_DECREF(m);
6788 if (struct_rusage == NULL)
6789 return NULL;
6790 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006791
Victor Stinner8c62be82010-05-06 00:08:46 +00006792 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6793 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6794 if (!result)
6795 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006796
6797#ifndef doubletime
6798#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6799#endif
6800
Victor Stinner8c62be82010-05-06 00:08:46 +00006801 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006802 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00006803 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006804 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006805#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00006806 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
6807 SET_INT(result, 2, ru->ru_maxrss);
6808 SET_INT(result, 3, ru->ru_ixrss);
6809 SET_INT(result, 4, ru->ru_idrss);
6810 SET_INT(result, 5, ru->ru_isrss);
6811 SET_INT(result, 6, ru->ru_minflt);
6812 SET_INT(result, 7, ru->ru_majflt);
6813 SET_INT(result, 8, ru->ru_nswap);
6814 SET_INT(result, 9, ru->ru_inblock);
6815 SET_INT(result, 10, ru->ru_oublock);
6816 SET_INT(result, 11, ru->ru_msgsnd);
6817 SET_INT(result, 12, ru->ru_msgrcv);
6818 SET_INT(result, 13, ru->ru_nsignals);
6819 SET_INT(result, 14, ru->ru_nvcsw);
6820 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006821#undef SET_INT
6822
Victor Stinner8c62be82010-05-06 00:08:46 +00006823 if (PyErr_Occurred()) {
6824 Py_DECREF(result);
6825 return NULL;
6826 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006827
Victor Stinner8c62be82010-05-06 00:08:46 +00006828 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006829}
6830#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6831
Larry Hastings2f936352014-08-05 14:04:04 +10006832
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006833#ifdef HAVE_WAIT3
Larry Hastings2f936352014-08-05 14:04:04 +10006834/*[clinic input]
6835os.wait3
6836
6837 options: int
6838Wait for completion of a child process.
6839
6840Returns a tuple of information about the child process:
6841 (pid, status, rusage)
6842[clinic start generated code]*/
6843
Larry Hastings2f936352014-08-05 14:04:04 +10006844static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006845os_wait3_impl(PyObject *module, int options)
6846/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006847{
Victor Stinner8c62be82010-05-06 00:08:46 +00006848 pid_t pid;
Victor Stinner8c62be82010-05-06 00:08:46 +00006849 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006850 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006851 WAIT_TYPE status;
6852 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006853
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006854 do {
6855 Py_BEGIN_ALLOW_THREADS
6856 pid = wait3(&status, options, &ru);
6857 Py_END_ALLOW_THREADS
6858 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
6859 if (pid < 0)
6860 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006861
Victor Stinner4195b5c2012-02-08 23:03:19 +01006862 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006863}
6864#endif /* HAVE_WAIT3 */
6865
Larry Hastings2f936352014-08-05 14:04:04 +10006866
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006867#ifdef HAVE_WAIT4
Larry Hastings2f936352014-08-05 14:04:04 +10006868/*[clinic input]
6869
6870os.wait4
6871
6872 pid: pid_t
6873 options: int
6874
6875Wait for completion of a specific child process.
6876
6877Returns a tuple of information about the child process:
6878 (pid, status, rusage)
6879[clinic start generated code]*/
6880
Larry Hastings2f936352014-08-05 14:04:04 +10006881static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006882os_wait4_impl(PyObject *module, pid_t pid, int options)
6883/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006884{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006885 pid_t res;
Victor Stinner8c62be82010-05-06 00:08:46 +00006886 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006887 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006888 WAIT_TYPE status;
6889 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006890
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006891 do {
6892 Py_BEGIN_ALLOW_THREADS
6893 res = wait4(pid, &status, options, &ru);
6894 Py_END_ALLOW_THREADS
6895 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
6896 if (res < 0)
6897 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006898
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006899 return wait_helper(res, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006900}
6901#endif /* HAVE_WAIT4 */
6902
Larry Hastings2f936352014-08-05 14:04:04 +10006903
Ross Lagerwall7807c352011-03-17 20:20:30 +02006904#if defined(HAVE_WAITID) && !defined(__APPLE__)
Larry Hastings2f936352014-08-05 14:04:04 +10006905/*[clinic input]
6906os.waitid
6907
6908 idtype: idtype_t
6909 Must be one of be P_PID, P_PGID or P_ALL.
6910 id: id_t
6911 The id to wait on.
6912 options: int
6913 Constructed from the ORing of one or more of WEXITED, WSTOPPED
6914 or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
6915 /
6916
6917Returns the result of waiting for a process or processes.
6918
6919Returns either waitid_result or None if WNOHANG is specified and there are
6920no children in a waitable state.
6921[clinic start generated code]*/
6922
Larry Hastings2f936352014-08-05 14:04:04 +10006923static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006924os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
6925/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006926{
6927 PyObject *result;
6928 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006929 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02006930 siginfo_t si;
6931 si.si_pid = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10006932
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006933 do {
6934 Py_BEGIN_ALLOW_THREADS
6935 res = waitid(idtype, id, &si, options);
6936 Py_END_ALLOW_THREADS
6937 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
6938 if (res < 0)
6939 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02006940
6941 if (si.si_pid == 0)
6942 Py_RETURN_NONE;
6943
6944 result = PyStructSequence_New(&WaitidResultType);
6945 if (!result)
6946 return NULL;
6947
6948 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006949 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02006950 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
6951 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
6952 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
6953 if (PyErr_Occurred()) {
6954 Py_DECREF(result);
6955 return NULL;
6956 }
6957
6958 return result;
6959}
Larry Hastings2f936352014-08-05 14:04:04 +10006960#endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
Ross Lagerwall7807c352011-03-17 20:20:30 +02006961
Larry Hastings2f936352014-08-05 14:04:04 +10006962
6963#if defined(HAVE_WAITPID)
6964/*[clinic input]
6965os.waitpid
6966 pid: pid_t
6967 options: int
6968 /
6969
6970Wait for completion of a given child process.
6971
6972Returns a tuple of information regarding the child process:
6973 (pid, status)
6974
6975The options argument is ignored on Windows.
6976[clinic start generated code]*/
6977
Larry Hastings2f936352014-08-05 14:04:04 +10006978static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006979os_waitpid_impl(PyObject *module, pid_t pid, int options)
6980/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006981{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006982 pid_t res;
6983 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006984 WAIT_TYPE status;
6985 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006986
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006987 do {
6988 Py_BEGIN_ALLOW_THREADS
6989 res = waitpid(pid, &status, options);
6990 Py_END_ALLOW_THREADS
6991 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
6992 if (res < 0)
6993 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006994
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006995 return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006996}
Tim Petersab034fa2002-02-01 11:27:43 +00006997#elif defined(HAVE_CWAIT)
Tim Petersab034fa2002-02-01 11:27:43 +00006998/* MS C has a variant of waitpid() that's usable for most purposes. */
Larry Hastings2f936352014-08-05 14:04:04 +10006999/*[clinic input]
7000os.waitpid
7001 pid: Py_intptr_t
7002 options: int
7003 /
7004
7005Wait for completion of a given process.
7006
7007Returns a tuple of information regarding the process:
7008 (pid, status << 8)
7009
7010The options argument is ignored on Windows.
7011[clinic start generated code]*/
7012
Larry Hastings2f936352014-08-05 14:04:04 +10007013static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007014os_waitpid_impl(PyObject *module, Py_intptr_t pid, int options)
7015/*[clinic end generated code: output=15f1ce005a346b09 input=444c8f51cca5b862]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007016{
7017 int status;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007018 Py_intptr_t res;
7019 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007020
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007021 do {
7022 Py_BEGIN_ALLOW_THREADS
7023 res = _cwait(&status, pid, options);
7024 Py_END_ALLOW_THREADS
7025 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007026 if (res < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007027 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007028
Victor Stinner8c62be82010-05-06 00:08:46 +00007029 /* shift the status left a byte so this is more like the POSIX waitpid */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007030 return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00007031}
Larry Hastings2f936352014-08-05 14:04:04 +10007032#endif
7033
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007034
Guido van Rossumad0ee831995-03-01 10:34:45 +00007035#ifdef HAVE_WAIT
Larry Hastings2f936352014-08-05 14:04:04 +10007036/*[clinic input]
7037os.wait
7038
7039Wait for completion of a child process.
7040
7041Returns a tuple of information about the child process:
7042 (pid, status)
7043[clinic start generated code]*/
7044
Larry Hastings2f936352014-08-05 14:04:04 +10007045static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007046os_wait_impl(PyObject *module)
7047/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
Guido van Rossum21803b81992-08-09 12:55:27 +00007048{
Victor Stinner8c62be82010-05-06 00:08:46 +00007049 pid_t pid;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007050 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007051 WAIT_TYPE status;
7052 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00007053
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007054 do {
7055 Py_BEGIN_ALLOW_THREADS
7056 pid = wait(&status);
7057 Py_END_ALLOW_THREADS
7058 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7059 if (pid < 0)
7060 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007061
Victor Stinner8c62be82010-05-06 00:08:46 +00007062 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00007063}
Larry Hastings2f936352014-08-05 14:04:04 +10007064#endif /* HAVE_WAIT */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007065
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007066
Larry Hastings9cf065c2012-06-22 16:30:09 -07007067#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
7068PyDoc_STRVAR(readlink__doc__,
7069"readlink(path, *, dir_fd=None) -> path\n\n\
7070Return a string representing the path to which the symbolic link points.\n\
7071\n\
7072If dir_fd is not None, it should be a file descriptor open to a directory,\n\
7073 and path should be relative; path will then be relative to that directory.\n\
7074dir_fd may not be implemented on your platform.\n\
7075 If it is unavailable, using it will raise a NotImplementedError.");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007076#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007077
Guido van Rossumb6775db1994-08-01 11:34:53 +00007078#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007079
Larry Hastings2f936352014-08-05 14:04:04 +10007080/* AC 3.5: merge win32 and not together */
Barry Warsaw53699e91996-12-10 23:23:01 +00007081static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07007082posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007083{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007084 path_t path;
7085 int dir_fd = DEFAULT_DIR_FD;
7086 char buffer[MAXPATHLEN];
7087 ssize_t length;
7088 PyObject *return_value = NULL;
7089 static char *keywords[] = {"path", "dir_fd", NULL};
Thomas Wouters89f507f2006-12-13 04:49:30 +00007090
Larry Hastings9cf065c2012-06-22 16:30:09 -07007091 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01007092 path.function_name = "readlink";
Larry Hastings9cf065c2012-06-22 16:30:09 -07007093 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords,
7094 path_converter, &path,
Larry Hastings2f936352014-08-05 14:04:04 +10007095 READLINKAT_DIR_FD_CONVERTER, &dir_fd))
Victor Stinner8c62be82010-05-06 00:08:46 +00007096 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00007097
Victor Stinner8c62be82010-05-06 00:08:46 +00007098 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007099#ifdef HAVE_READLINKAT
7100 if (dir_fd != DEFAULT_DIR_FD)
7101 length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer));
Victor Stinnera45598a2010-05-14 16:35:39 +00007102 else
Larry Hastings9cf065c2012-06-22 16:30:09 -07007103#endif
7104 length = readlink(path.narrow, buffer, sizeof(buffer));
7105 Py_END_ALLOW_THREADS
7106
7107 if (length < 0) {
Victor Stinner292c8352012-10-30 02:17:38 +01007108 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007109 goto exit;
7110 }
7111
7112 if (PyUnicode_Check(path.object))
7113 return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length);
7114 else
7115 return_value = PyBytes_FromStringAndSize(buffer, length);
7116exit:
7117 path_cleanup(&path);
7118 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007119}
Larry Hastings9cf065c2012-06-22 16:30:09 -07007120
Guido van Rossumb6775db1994-08-01 11:34:53 +00007121#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007122
Larry Hastings2f936352014-08-05 14:04:04 +10007123#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
7124
7125static PyObject *
7126win_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
7127{
7128 wchar_t *path;
7129 DWORD n_bytes_returned;
7130 DWORD io_result;
7131 PyObject *po, *result;
7132 int dir_fd;
7133 HANDLE reparse_point_handle;
7134
7135 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
7136 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
7137 wchar_t *print_name;
7138
7139 static char *keywords[] = {"path", "dir_fd", NULL};
7140
7141 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords,
7142 &po,
7143 dir_fd_unavailable, &dir_fd
7144 ))
7145 return NULL;
7146
7147 path = PyUnicode_AsUnicode(po);
7148 if (path == NULL)
7149 return NULL;
7150
7151 /* First get a handle to the reparse point */
7152 Py_BEGIN_ALLOW_THREADS
7153 reparse_point_handle = CreateFileW(
7154 path,
7155 0,
7156 0,
7157 0,
7158 OPEN_EXISTING,
7159 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
7160 0);
7161 Py_END_ALLOW_THREADS
7162
7163 if (reparse_point_handle==INVALID_HANDLE_VALUE)
7164 return win32_error_object("readlink", po);
7165
7166 Py_BEGIN_ALLOW_THREADS
7167 /* New call DeviceIoControl to read the reparse point */
7168 io_result = DeviceIoControl(
7169 reparse_point_handle,
7170 FSCTL_GET_REPARSE_POINT,
7171 0, 0, /* in buffer */
7172 target_buffer, sizeof(target_buffer),
7173 &n_bytes_returned,
7174 0 /* we're not using OVERLAPPED_IO */
7175 );
7176 CloseHandle(reparse_point_handle);
7177 Py_END_ALLOW_THREADS
7178
7179 if (io_result==0)
7180 return win32_error_object("readlink", po);
7181
7182 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
7183 {
7184 PyErr_SetString(PyExc_ValueError,
7185 "not a symbolic link");
7186 return NULL;
7187 }
7188 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
7189 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
7190
7191 result = PyUnicode_FromWideChar(print_name,
7192 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
7193 return result;
7194}
7195
7196#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
7197
7198
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007199
Larry Hastings9cf065c2012-06-22 16:30:09 -07007200#ifdef HAVE_SYMLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -07007201
7202#if defined(MS_WINDOWS)
7203
7204/* Grab CreateSymbolicLinkW dynamically from kernel32 */
7205static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD) = NULL;
7206static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPSTR, LPSTR, DWORD) = NULL;
Victor Stinner31b3b922013-06-05 01:49:17 +02007207
Larry Hastings9cf065c2012-06-22 16:30:09 -07007208static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007209check_CreateSymbolicLink(void)
Larry Hastings9cf065c2012-06-22 16:30:09 -07007210{
7211 HINSTANCE hKernel32;
7212 /* only recheck */
7213 if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA)
7214 return 1;
7215 hKernel32 = GetModuleHandleW(L"KERNEL32");
7216 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
7217 "CreateSymbolicLinkW");
7218 *(FARPROC*)&Py_CreateSymbolicLinkA = GetProcAddress(hKernel32,
7219 "CreateSymbolicLinkA");
7220 return (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA);
7221}
7222
Victor Stinner31b3b922013-06-05 01:49:17 +02007223/* Remove the last portion of the path */
7224static void
7225_dirnameW(WCHAR *path)
7226{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007227 WCHAR *ptr;
7228
7229 /* walk the path from the end until a backslash is encountered */
Victor Stinner31b3b922013-06-05 01:49:17 +02007230 for(ptr = path + wcslen(path); ptr != path; ptr--) {
Victor Stinner072318b2013-06-05 02:07:46 +02007231 if (*ptr == L'\\' || *ptr == L'/')
Jason R. Coombs3a092862013-05-27 23:21:28 -04007232 break;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007233 }
7234 *ptr = 0;
7235}
7236
Victor Stinner31b3b922013-06-05 01:49:17 +02007237/* Remove the last portion of the path */
7238static void
7239_dirnameA(char *path)
7240{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007241 char *ptr;
7242
7243 /* walk the path from the end until a backslash is encountered */
Victor Stinner31b3b922013-06-05 01:49:17 +02007244 for(ptr = path + strlen(path); ptr != path; ptr--) {
7245 if (*ptr == '\\' || *ptr == '/')
Jason R. Coombs3a092862013-05-27 23:21:28 -04007246 break;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007247 }
7248 *ptr = 0;
7249}
7250
Victor Stinner31b3b922013-06-05 01:49:17 +02007251/* Is this path absolute? */
7252static int
7253_is_absW(const WCHAR *path)
7254{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007255 return path[0] == L'\\' || path[0] == L'/' || path[1] == L':';
7256
7257}
7258
Victor Stinner31b3b922013-06-05 01:49:17 +02007259/* Is this path absolute? */
7260static int
7261_is_absA(const char *path)
7262{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007263 return path[0] == '\\' || path[0] == '/' || path[1] == ':';
7264
7265}
7266
Victor Stinner31b3b922013-06-05 01:49:17 +02007267/* join root and rest with a backslash */
7268static void
7269_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
7270{
Victor Stinnere7e7eba2013-06-05 00:35:54 +02007271 size_t root_len;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007272
Victor Stinner31b3b922013-06-05 01:49:17 +02007273 if (_is_absW(rest)) {
Jason R. Coombs3a092862013-05-27 23:21:28 -04007274 wcscpy(dest_path, rest);
7275 return;
7276 }
7277
7278 root_len = wcslen(root);
7279
7280 wcscpy(dest_path, root);
7281 if(root_len) {
Victor Stinner31b3b922013-06-05 01:49:17 +02007282 dest_path[root_len] = L'\\';
7283 root_len++;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007284 }
7285 wcscpy(dest_path+root_len, rest);
7286}
7287
Victor Stinner31b3b922013-06-05 01:49:17 +02007288/* join root and rest with a backslash */
7289static void
7290_joinA(char *dest_path, const char *root, const char *rest)
7291{
Victor Stinnere7e7eba2013-06-05 00:35:54 +02007292 size_t root_len;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007293
Victor Stinner31b3b922013-06-05 01:49:17 +02007294 if (_is_absA(rest)) {
Jason R. Coombs3a092862013-05-27 23:21:28 -04007295 strcpy(dest_path, rest);
7296 return;
7297 }
7298
7299 root_len = strlen(root);
7300
7301 strcpy(dest_path, root);
7302 if(root_len) {
7303 dest_path[root_len] = '\\';
Victor Stinner31b3b922013-06-05 01:49:17 +02007304 root_len++;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007305 }
7306 strcpy(dest_path+root_len, rest);
7307}
7308
Victor Stinner31b3b922013-06-05 01:49:17 +02007309/* Return True if the path at src relative to dest is a directory */
7310static int
7311_check_dirW(WCHAR *src, WCHAR *dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04007312{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007313 WIN32_FILE_ATTRIBUTE_DATA src_info;
7314 WCHAR dest_parent[MAX_PATH];
7315 WCHAR src_resolved[MAX_PATH] = L"";
7316
7317 /* dest_parent = os.path.dirname(dest) */
7318 wcscpy(dest_parent, dest);
7319 _dirnameW(dest_parent);
7320 /* src_resolved = os.path.join(dest_parent, src) */
7321 _joinW(src_resolved, dest_parent, src);
7322 return (
7323 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
7324 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
7325 );
7326}
7327
Victor Stinner31b3b922013-06-05 01:49:17 +02007328/* Return True if the path at src relative to dest is a directory */
7329static int
7330_check_dirA(char *src, char *dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04007331{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007332 WIN32_FILE_ATTRIBUTE_DATA src_info;
7333 char dest_parent[MAX_PATH];
7334 char src_resolved[MAX_PATH] = "";
7335
7336 /* dest_parent = os.path.dirname(dest) */
7337 strcpy(dest_parent, dest);
Victor Stinner5a436762013-06-05 00:37:12 +02007338 _dirnameA(dest_parent);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007339 /* src_resolved = os.path.join(dest_parent, src) */
Victor Stinner5a436762013-06-05 00:37:12 +02007340 _joinA(src_resolved, dest_parent, src);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007341 return (
7342 GetFileAttributesExA(src_resolved, GetFileExInfoStandard, &src_info)
7343 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
7344 );
7345}
Larry Hastings9cf065c2012-06-22 16:30:09 -07007346#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007347
Larry Hastings2f936352014-08-05 14:04:04 +10007348
7349/*[clinic input]
7350os.symlink
7351 src: path_t
7352 dst: path_t
7353 target_is_directory: bool = False
7354 *
7355 dir_fd: dir_fd(requires='symlinkat')=None
7356
7357# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
7358
7359Create a symbolic link pointing to src named dst.
7360
7361target_is_directory is required on Windows if the target is to be
7362 interpreted as a directory. (On Windows, symlink requires
7363 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
7364 target_is_directory is ignored on non-Windows platforms.
7365
7366If dir_fd is not None, it should be a file descriptor open to a directory,
7367 and path should be relative; path will then be relative to that directory.
7368dir_fd may not be implemented on your platform.
7369 If it is unavailable, using it will raise a NotImplementedError.
7370
7371[clinic start generated code]*/
7372
Larry Hastings2f936352014-08-05 14:04:04 +10007373static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007374os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04007375 int target_is_directory, int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007376/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007377{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007378#ifdef MS_WINDOWS
7379 DWORD result;
7380#else
7381 int result;
7382#endif
7383
Larry Hastings9cf065c2012-06-22 16:30:09 -07007384#ifdef MS_WINDOWS
7385 if (!check_CreateSymbolicLink()) {
7386 PyErr_SetString(PyExc_NotImplementedError,
7387 "CreateSymbolicLink functions not found");
Larry Hastings2f936352014-08-05 14:04:04 +10007388 return NULL;
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03007389 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07007390 if (!win32_can_symlink) {
7391 PyErr_SetString(PyExc_OSError, "symbolic link privilege not held");
Larry Hastings2f936352014-08-05 14:04:04 +10007392 return NULL;
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03007393 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07007394#endif
7395
Larry Hastings2f936352014-08-05 14:04:04 +10007396 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07007397 PyErr_SetString(PyExc_ValueError,
7398 "symlink: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10007399 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007400 }
7401
7402#ifdef MS_WINDOWS
Jason R. Coombs3a092862013-05-27 23:21:28 -04007403
Larry Hastings9cf065c2012-06-22 16:30:09 -07007404 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10007405 if (dst->wide) {
Jason R. Coombs3a092862013-05-27 23:21:28 -04007406 /* if src is a directory, ensure target_is_directory==1 */
Larry Hastings2f936352014-08-05 14:04:04 +10007407 target_is_directory |= _check_dirW(src->wide, dst->wide);
7408 result = Py_CreateSymbolicLinkW(dst->wide, src->wide,
Larry Hastings9cf065c2012-06-22 16:30:09 -07007409 target_is_directory);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007410 }
7411 else {
7412 /* if src is a directory, ensure target_is_directory==1 */
Larry Hastings2f936352014-08-05 14:04:04 +10007413 target_is_directory |= _check_dirA(src->narrow, dst->narrow);
7414 result = Py_CreateSymbolicLinkA(dst->narrow, src->narrow,
Larry Hastings9cf065c2012-06-22 16:30:09 -07007415 target_is_directory);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007416 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07007417 Py_END_ALLOW_THREADS
7418
Larry Hastings2f936352014-08-05 14:04:04 +10007419 if (!result)
7420 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007421
7422#else
7423
7424 Py_BEGIN_ALLOW_THREADS
7425#if HAVE_SYMLINKAT
7426 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10007427 result = symlinkat(src->narrow, dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007428 else
7429#endif
Larry Hastings2f936352014-08-05 14:04:04 +10007430 result = symlink(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007431 Py_END_ALLOW_THREADS
7432
Larry Hastings2f936352014-08-05 14:04:04 +10007433 if (result)
7434 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007435#endif
7436
Larry Hastings2f936352014-08-05 14:04:04 +10007437 Py_RETURN_NONE;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007438}
7439#endif /* HAVE_SYMLINK */
7440
Larry Hastings9cf065c2012-06-22 16:30:09 -07007441
Brian Curtind40e6f72010-07-08 21:39:08 +00007442
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007443
Larry Hastings605a62d2012-06-24 04:33:36 -07007444static PyStructSequence_Field times_result_fields[] = {
7445 {"user", "user time"},
7446 {"system", "system time"},
7447 {"children_user", "user time of children"},
7448 {"children_system", "system time of children"},
7449 {"elapsed", "elapsed time since an arbitrary point in the past"},
7450 {NULL}
7451};
7452
7453PyDoc_STRVAR(times_result__doc__,
7454"times_result: Result from os.times().\n\n\
7455This object may be accessed either as a tuple of\n\
7456 (user, system, children_user, children_system, elapsed),\n\
7457or via the attributes user, system, children_user, children_system,\n\
7458and elapsed.\n\
7459\n\
7460See os.times for more information.");
7461
7462static PyStructSequence_Desc times_result_desc = {
7463 "times_result", /* name */
7464 times_result__doc__, /* doc */
7465 times_result_fields,
7466 5
7467};
7468
7469static PyTypeObject TimesResultType;
7470
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007471#ifdef MS_WINDOWS
7472#define HAVE_TIMES /* mandatory, for the method table */
7473#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07007474
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007475#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07007476
7477static PyObject *
7478build_times_result(double user, double system,
7479 double children_user, double children_system,
7480 double elapsed)
7481{
7482 PyObject *value = PyStructSequence_New(&TimesResultType);
7483 if (value == NULL)
7484 return NULL;
7485
7486#define SET(i, field) \
7487 { \
7488 PyObject *o = PyFloat_FromDouble(field); \
7489 if (!o) { \
7490 Py_DECREF(value); \
7491 return NULL; \
7492 } \
7493 PyStructSequence_SET_ITEM(value, i, o); \
7494 } \
7495
7496 SET(0, user);
7497 SET(1, system);
7498 SET(2, children_user);
7499 SET(3, children_system);
7500 SET(4, elapsed);
7501
7502#undef SET
7503
7504 return value;
7505}
7506
Larry Hastings605a62d2012-06-24 04:33:36 -07007507
Larry Hastings2f936352014-08-05 14:04:04 +10007508#ifndef MS_WINDOWS
7509#define NEED_TICKS_PER_SECOND
7510static long ticks_per_second = -1;
7511#endif /* MS_WINDOWS */
7512
7513/*[clinic input]
7514os.times
7515
7516Return a collection containing process timing information.
7517
7518The object returned behaves like a named tuple with these fields:
7519 (utime, stime, cutime, cstime, elapsed_time)
7520All fields are floating point numbers.
7521[clinic start generated code]*/
7522
Larry Hastings2f936352014-08-05 14:04:04 +10007523static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007524os_times_impl(PyObject *module)
7525/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007526#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00007527{
Victor Stinner8c62be82010-05-06 00:08:46 +00007528 FILETIME create, exit, kernel, user;
7529 HANDLE hProc;
7530 hProc = GetCurrentProcess();
7531 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
7532 /* The fields of a FILETIME structure are the hi and lo part
7533 of a 64-bit value expressed in 100 nanosecond units.
7534 1e7 is one second in such units; 1e-7 the inverse.
7535 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
7536 */
Larry Hastings605a62d2012-06-24 04:33:36 -07007537 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00007538 (double)(user.dwHighDateTime*429.4967296 +
7539 user.dwLowDateTime*1e-7),
7540 (double)(kernel.dwHighDateTime*429.4967296 +
7541 kernel.dwLowDateTime*1e-7),
7542 (double)0,
7543 (double)0,
7544 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00007545}
Larry Hastings2f936352014-08-05 14:04:04 +10007546#else /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007547{
Larry Hastings2f936352014-08-05 14:04:04 +10007548
7549
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007550 struct tms t;
7551 clock_t c;
7552 errno = 0;
7553 c = times(&t);
7554 if (c == (clock_t) -1)
7555 return posix_error();
7556 return build_times_result(
7557 (double)t.tms_utime / ticks_per_second,
7558 (double)t.tms_stime / ticks_per_second,
7559 (double)t.tms_cutime / ticks_per_second,
7560 (double)t.tms_cstime / ticks_per_second,
7561 (double)c / ticks_per_second);
7562}
Larry Hastings2f936352014-08-05 14:04:04 +10007563#endif /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007564#endif /* HAVE_TIMES */
7565
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007566
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007567#ifdef HAVE_GETSID
Larry Hastings2f936352014-08-05 14:04:04 +10007568/*[clinic input]
7569os.getsid
7570
7571 pid: pid_t
7572 /
7573
7574Call the system call getsid(pid) and return the result.
7575[clinic start generated code]*/
7576
Larry Hastings2f936352014-08-05 14:04:04 +10007577static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007578os_getsid_impl(PyObject *module, pid_t pid)
7579/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007580{
Victor Stinner8c62be82010-05-06 00:08:46 +00007581 int sid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007582 sid = getsid(pid);
7583 if (sid < 0)
7584 return posix_error();
7585 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007586}
7587#endif /* HAVE_GETSID */
7588
7589
Guido van Rossumb6775db1994-08-01 11:34:53 +00007590#ifdef HAVE_SETSID
Larry Hastings2f936352014-08-05 14:04:04 +10007591/*[clinic input]
7592os.setsid
7593
7594Call the system call setsid().
7595[clinic start generated code]*/
7596
Larry Hastings2f936352014-08-05 14:04:04 +10007597static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007598os_setsid_impl(PyObject *module)
7599/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00007600{
Victor Stinner8c62be82010-05-06 00:08:46 +00007601 if (setsid() < 0)
7602 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007603 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007604}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007605#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007606
Larry Hastings2f936352014-08-05 14:04:04 +10007607
Guido van Rossumb6775db1994-08-01 11:34:53 +00007608#ifdef HAVE_SETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10007609/*[clinic input]
7610os.setpgid
7611
7612 pid: pid_t
7613 pgrp: pid_t
7614 /
7615
7616Call the system call setpgid(pid, pgrp).
7617[clinic start generated code]*/
7618
Larry Hastings2f936352014-08-05 14:04:04 +10007619static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007620os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
7621/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007622{
Victor Stinner8c62be82010-05-06 00:08:46 +00007623 if (setpgid(pid, pgrp) < 0)
7624 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007625 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007626}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007627#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007628
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007629
Guido van Rossumb6775db1994-08-01 11:34:53 +00007630#ifdef HAVE_TCGETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007631/*[clinic input]
7632os.tcgetpgrp
7633
7634 fd: int
7635 /
7636
7637Return the process group associated with the terminal specified by fd.
7638[clinic start generated code]*/
7639
Larry Hastings2f936352014-08-05 14:04:04 +10007640static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007641os_tcgetpgrp_impl(PyObject *module, int fd)
7642/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007643{
7644 pid_t pgid = tcgetpgrp(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00007645 if (pgid < 0)
7646 return posix_error();
7647 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00007648}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007649#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00007650
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007651
Guido van Rossumb6775db1994-08-01 11:34:53 +00007652#ifdef HAVE_TCSETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007653/*[clinic input]
7654os.tcsetpgrp
7655
7656 fd: int
7657 pgid: pid_t
7658 /
7659
7660Set the process group associated with the terminal specified by fd.
7661[clinic start generated code]*/
7662
Larry Hastings2f936352014-08-05 14:04:04 +10007663static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007664os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
7665/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007666{
Victor Stinner8c62be82010-05-06 00:08:46 +00007667 if (tcsetpgrp(fd, pgid) < 0)
7668 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007669 Py_RETURN_NONE;
Guido van Rossum7066dd71992-09-17 17:54:56 +00007670}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007671#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00007672
Guido van Rossum687dd131993-05-17 08:34:16 +00007673/* Functions acting on file descriptors */
7674
Victor Stinnerdaf45552013-08-28 00:53:59 +02007675#ifdef O_CLOEXEC
7676extern int _Py_open_cloexec_works;
7677#endif
7678
Larry Hastings2f936352014-08-05 14:04:04 +10007679
7680/*[clinic input]
7681os.open -> int
7682 path: path_t
7683 flags: int
7684 mode: int = 0o777
7685 *
7686 dir_fd: dir_fd(requires='openat') = None
7687
7688# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
7689
7690Open a file for low level IO. Returns a file descriptor (integer).
7691
7692If dir_fd is not None, it should be a file descriptor open to a directory,
7693 and path should be relative; path will then be relative to that directory.
7694dir_fd may not be implemented on your platform.
7695 If it is unavailable, using it will raise a NotImplementedError.
7696[clinic start generated code]*/
7697
Larry Hastings2f936352014-08-05 14:04:04 +10007698static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007699os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
7700/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007701{
7702 int fd;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007703 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007704
Victor Stinnerdaf45552013-08-28 00:53:59 +02007705#ifdef O_CLOEXEC
7706 int *atomic_flag_works = &_Py_open_cloexec_works;
7707#elif !defined(MS_WINDOWS)
7708 int *atomic_flag_works = NULL;
7709#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007710
Victor Stinnerdaf45552013-08-28 00:53:59 +02007711#ifdef MS_WINDOWS
7712 flags |= O_NOINHERIT;
7713#elif defined(O_CLOEXEC)
7714 flags |= O_CLOEXEC;
7715#endif
7716
Steve Dower8fc89802015-04-12 00:26:27 -04007717 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007718 do {
7719 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007720#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007721 if (path->wide)
7722 fd = _wopen(path->wide, flags, mode);
7723 else
Larry Hastings9cf065c2012-06-22 16:30:09 -07007724#endif
7725#ifdef HAVE_OPENAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007726 if (dir_fd != DEFAULT_DIR_FD)
7727 fd = openat(dir_fd, path->narrow, flags, mode);
7728 else
Larry Hastings9cf065c2012-06-22 16:30:09 -07007729#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007730 fd = open(path->narrow, flags, mode);
7731 Py_END_ALLOW_THREADS
7732 } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04007733 _Py_END_SUPPRESS_IPH
Guido van Rossum687dd131993-05-17 08:34:16 +00007734
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007735 if (fd < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007736 if (!async_err)
7737 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings2f936352014-08-05 14:04:04 +10007738 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007739 }
7740
Victor Stinnerdaf45552013-08-28 00:53:59 +02007741#ifndef MS_WINDOWS
7742 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
7743 close(fd);
Larry Hastings2f936352014-08-05 14:04:04 +10007744 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02007745 }
7746#endif
7747
Larry Hastings2f936352014-08-05 14:04:04 +10007748 return fd;
7749}
7750
7751
7752/*[clinic input]
7753os.close
7754
7755 fd: int
7756
7757Close a file descriptor.
7758[clinic start generated code]*/
7759
Barry Warsaw53699e91996-12-10 23:23:01 +00007760static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007761os_close_impl(PyObject *module, int fd)
7762/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00007763{
Larry Hastings2f936352014-08-05 14:04:04 +10007764 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007765 if (!_PyVerify_fd(fd))
7766 return posix_error();
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007767 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
7768 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
7769 * for more details.
7770 */
Victor Stinner8c62be82010-05-06 00:08:46 +00007771 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04007772 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007773 res = close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04007774 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007775 Py_END_ALLOW_THREADS
7776 if (res < 0)
7777 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007778 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00007779}
7780
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007781
Larry Hastings2f936352014-08-05 14:04:04 +10007782/*[clinic input]
7783os.closerange
7784
7785 fd_low: int
7786 fd_high: int
7787 /
7788
7789Closes all file descriptors in [fd_low, fd_high), ignoring errors.
7790[clinic start generated code]*/
7791
Larry Hastings2f936352014-08-05 14:04:04 +10007792static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007793os_closerange_impl(PyObject *module, int fd_low, int fd_high)
7794/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007795{
7796 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00007797 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04007798 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10007799 for (i = fd_low; i < fd_high; i++)
Victor Stinner8c62be82010-05-06 00:08:46 +00007800 if (_PyVerify_fd(i))
7801 close(i);
Steve Dower8fc89802015-04-12 00:26:27 -04007802 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007803 Py_END_ALLOW_THREADS
7804 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00007805}
7806
7807
Larry Hastings2f936352014-08-05 14:04:04 +10007808/*[clinic input]
7809os.dup -> int
7810
7811 fd: int
7812 /
7813
7814Return a duplicate of a file descriptor.
7815[clinic start generated code]*/
7816
Larry Hastings2f936352014-08-05 14:04:04 +10007817static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007818os_dup_impl(PyObject *module, int fd)
7819/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007820{
7821 return _Py_dup(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00007822}
7823
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007824
Larry Hastings2f936352014-08-05 14:04:04 +10007825/*[clinic input]
7826os.dup2
7827 fd: int
7828 fd2: int
7829 inheritable: bool=True
7830
7831Duplicate file descriptor.
7832[clinic start generated code]*/
7833
Larry Hastings2f936352014-08-05 14:04:04 +10007834static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007835os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
7836/*[clinic end generated code: output=db832a2d872ccc5f input=76e96f511be0352f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007837{
Victor Stinnerdaf45552013-08-28 00:53:59 +02007838 int res;
7839#if defined(HAVE_DUP3) && \
7840 !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
7841 /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
7842 int dup3_works = -1;
7843#endif
7844
Victor Stinner8c62be82010-05-06 00:08:46 +00007845 if (!_PyVerify_fd_dup2(fd, fd2))
7846 return posix_error();
Victor Stinnerdaf45552013-08-28 00:53:59 +02007847
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007848 /* dup2() can fail with EINTR if the target FD is already open, because it
7849 * then has to be closed. See os_close_impl() for why we don't handle EINTR
7850 * upon close(), and therefore below.
7851 */
Victor Stinnerdaf45552013-08-28 00:53:59 +02007852#ifdef MS_WINDOWS
7853 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04007854 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007855 res = dup2(fd, fd2);
Steve Dower8fc89802015-04-12 00:26:27 -04007856 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02007857 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007858 if (res < 0)
7859 return posix_error();
Victor Stinnerdaf45552013-08-28 00:53:59 +02007860
7861 /* Character files like console cannot be make non-inheritable */
7862 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
7863 close(fd2);
7864 return NULL;
7865 }
7866
7867#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
7868 Py_BEGIN_ALLOW_THREADS
7869 if (!inheritable)
7870 res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
7871 else
7872 res = dup2(fd, fd2);
7873 Py_END_ALLOW_THREADS
7874 if (res < 0)
7875 return posix_error();
7876
7877#else
7878
7879#ifdef HAVE_DUP3
7880 if (!inheritable && dup3_works != 0) {
7881 Py_BEGIN_ALLOW_THREADS
7882 res = dup3(fd, fd2, O_CLOEXEC);
7883 Py_END_ALLOW_THREADS
7884 if (res < 0) {
7885 if (dup3_works == -1)
7886 dup3_works = (errno != ENOSYS);
7887 if (dup3_works)
7888 return posix_error();
7889 }
7890 }
7891
7892 if (inheritable || dup3_works == 0)
7893 {
7894#endif
7895 Py_BEGIN_ALLOW_THREADS
7896 res = dup2(fd, fd2);
7897 Py_END_ALLOW_THREADS
7898 if (res < 0)
7899 return posix_error();
7900
7901 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
7902 close(fd2);
7903 return NULL;
7904 }
7905#ifdef HAVE_DUP3
7906 }
7907#endif
7908
7909#endif
7910
Larry Hastings2f936352014-08-05 14:04:04 +10007911 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00007912}
7913
Larry Hastings2f936352014-08-05 14:04:04 +10007914
Ross Lagerwall7807c352011-03-17 20:20:30 +02007915#ifdef HAVE_LOCKF
Larry Hastings2f936352014-08-05 14:04:04 +10007916/*[clinic input]
7917os.lockf
7918
7919 fd: int
7920 An open file descriptor.
7921 command: int
7922 One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
7923 length: Py_off_t
7924 The number of bytes to lock, starting at the current position.
7925 /
7926
7927Apply, test or remove a POSIX lock on an open file descriptor.
7928
7929[clinic start generated code]*/
7930
Larry Hastings2f936352014-08-05 14:04:04 +10007931static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007932os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
7933/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007934{
7935 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007936
7937 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10007938 res = lockf(fd, command, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02007939 Py_END_ALLOW_THREADS
7940
7941 if (res < 0)
7942 return posix_error();
7943
7944 Py_RETURN_NONE;
7945}
Larry Hastings2f936352014-08-05 14:04:04 +10007946#endif /* HAVE_LOCKF */
Ross Lagerwall7807c352011-03-17 20:20:30 +02007947
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007948
Larry Hastings2f936352014-08-05 14:04:04 +10007949/*[clinic input]
7950os.lseek -> Py_off_t
7951
7952 fd: int
7953 position: Py_off_t
7954 how: int
7955 /
7956
7957Set the position of a file descriptor. Return the new position.
7958
7959Return the new cursor position in number of bytes
7960relative to the beginning of the file.
7961[clinic start generated code]*/
7962
Larry Hastings2f936352014-08-05 14:04:04 +10007963static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007964os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
7965/*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007966{
7967 Py_off_t result;
7968
7969 if (!_PyVerify_fd(fd)) {
7970 posix_error();
7971 return -1;
7972 }
Guido van Rossum687dd131993-05-17 08:34:16 +00007973#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00007974 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
7975 switch (how) {
Larry Hastings2f936352014-08-05 14:04:04 +10007976 case 0: how = SEEK_SET; break;
7977 case 1: how = SEEK_CUR; break;
7978 case 2: how = SEEK_END; break;
Victor Stinner8c62be82010-05-06 00:08:46 +00007979 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007980#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007981
Victor Stinner8c62be82010-05-06 00:08:46 +00007982 if (PyErr_Occurred())
Larry Hastings2f936352014-08-05 14:04:04 +10007983 return -1;
Guido van Rossum94f6f721999-01-06 18:42:14 +00007984
Larry Hastings2f936352014-08-05 14:04:04 +10007985 if (!_PyVerify_fd(fd)) {
7986 posix_error();
7987 return -1;
7988 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007989 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04007990 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +02007991#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007992 result = _lseeki64(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00007993#else
Larry Hastings2f936352014-08-05 14:04:04 +10007994 result = lseek(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00007995#endif
Steve Dower8fc89802015-04-12 00:26:27 -04007996 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007997 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10007998 if (result < 0)
7999 posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00008000
Larry Hastings2f936352014-08-05 14:04:04 +10008001 return result;
Guido van Rossum687dd131993-05-17 08:34:16 +00008002}
8003
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008004
Larry Hastings2f936352014-08-05 14:04:04 +10008005/*[clinic input]
8006os.read
8007 fd: int
8008 length: Py_ssize_t
8009 /
8010
8011Read from a file descriptor. Returns a bytes object.
8012[clinic start generated code]*/
8013
Larry Hastings2f936352014-08-05 14:04:04 +10008014static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008015os_read_impl(PyObject *module, int fd, Py_ssize_t length)
8016/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008017{
Victor Stinner8c62be82010-05-06 00:08:46 +00008018 Py_ssize_t n;
8019 PyObject *buffer;
Larry Hastings2f936352014-08-05 14:04:04 +10008020
8021 if (length < 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008022 errno = EINVAL;
8023 return posix_error();
8024 }
Larry Hastings2f936352014-08-05 14:04:04 +10008025
8026#ifdef MS_WINDOWS
Victor Stinner66aab0c2015-03-19 22:53:20 +01008027 /* On Windows, the count parameter of read() is an int */
Larry Hastings2f936352014-08-05 14:04:04 +10008028 if (length > INT_MAX)
8029 length = INT_MAX;
Larry Hastings2f936352014-08-05 14:04:04 +10008030#endif
8031
8032 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Victor Stinner8c62be82010-05-06 00:08:46 +00008033 if (buffer == NULL)
8034 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008035
Victor Stinner66aab0c2015-03-19 22:53:20 +01008036 n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
8037 if (n == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008038 Py_DECREF(buffer);
Victor Stinner66aab0c2015-03-19 22:53:20 +01008039 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008040 }
Larry Hastings2f936352014-08-05 14:04:04 +10008041
8042 if (n != length)
Victor Stinner8c62be82010-05-06 00:08:46 +00008043 _PyBytes_Resize(&buffer, n);
Larry Hastings2f936352014-08-05 14:04:04 +10008044
Victor Stinner8c62be82010-05-06 00:08:46 +00008045 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00008046}
8047
Ross Lagerwall7807c352011-03-17 20:20:30 +02008048#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
8049 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008050static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008051iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
8052{
8053 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008054 Py_ssize_t blen, total = 0;
8055
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008056 *iov = PyMem_New(struct iovec, cnt);
8057 if (*iov == NULL) {
8058 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008059 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008060 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008061
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008062 *buf = PyMem_New(Py_buffer, cnt);
8063 if (*buf == NULL) {
8064 PyMem_Del(*iov);
8065 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008066 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008067 }
8068
8069 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008070 PyObject *item = PySequence_GetItem(seq, i);
8071 if (item == NULL)
8072 goto fail;
8073 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
8074 Py_DECREF(item);
8075 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008076 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008077 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008078 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008079 blen = (*buf)[i].len;
8080 (*iov)[i].iov_len = blen;
8081 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008082 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008083 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008084
8085fail:
8086 PyMem_Del(*iov);
8087 for (j = 0; j < i; j++) {
8088 PyBuffer_Release(&(*buf)[j]);
8089 }
8090 PyMem_Del(*buf);
Victor Stinner57ddf782014-01-08 15:21:28 +01008091 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008092}
8093
8094static void
8095iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
8096{
8097 int i;
8098 PyMem_Del(iov);
8099 for (i = 0; i < cnt; i++) {
8100 PyBuffer_Release(&buf[i]);
8101 }
8102 PyMem_Del(buf);
8103}
8104#endif
8105
Larry Hastings2f936352014-08-05 14:04:04 +10008106
Ross Lagerwall7807c352011-03-17 20:20:30 +02008107#ifdef HAVE_READV
Larry Hastings2f936352014-08-05 14:04:04 +10008108/*[clinic input]
8109os.readv -> Py_ssize_t
8110
8111 fd: int
8112 buffers: object
8113 /
8114
8115Read from a file descriptor fd into an iterable of buffers.
8116
8117The buffers should be mutable buffers accepting bytes.
8118readv will transfer data into each buffer until it is full
8119and then move on to the next buffer in the sequence to hold
8120the rest of the data.
8121
8122readv returns the total number of bytes read,
8123which may be less than the total capacity of all the buffers.
8124[clinic start generated code]*/
8125
Larry Hastings2f936352014-08-05 14:04:04 +10008126static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008127os_readv_impl(PyObject *module, int fd, PyObject *buffers)
8128/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008129{
8130 int cnt;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008131 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008132 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008133 struct iovec *iov;
8134 Py_buffer *buf;
8135
Larry Hastings2f936352014-08-05 14:04:04 +10008136 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008137 PyErr_SetString(PyExc_TypeError,
8138 "readv() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10008139 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008140 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02008141
Larry Hastings2f936352014-08-05 14:04:04 +10008142 cnt = PySequence_Size(buffers);
8143
8144 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
8145 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008146
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008147 do {
8148 Py_BEGIN_ALLOW_THREADS
8149 n = readv(fd, iov, cnt);
8150 Py_END_ALLOW_THREADS
8151 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008152
8153 iov_cleanup(iov, buf, cnt);
Larry Hastings2f936352014-08-05 14:04:04 +10008154 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008155 if (!async_err)
8156 posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008157 return -1;
8158 }
Victor Stinner57ddf782014-01-08 15:21:28 +01008159
Larry Hastings2f936352014-08-05 14:04:04 +10008160 return n;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008161}
Larry Hastings2f936352014-08-05 14:04:04 +10008162#endif /* HAVE_READV */
8163
Ross Lagerwall7807c352011-03-17 20:20:30 +02008164
8165#ifdef HAVE_PREAD
Larry Hastings2f936352014-08-05 14:04:04 +10008166/*[clinic input]
8167# TODO length should be size_t! but Python doesn't support parsing size_t yet.
8168os.pread
8169
8170 fd: int
8171 length: int
8172 offset: Py_off_t
8173 /
8174
8175Read a number of bytes from a file descriptor starting at a particular offset.
8176
8177Read length bytes from file descriptor fd, starting at offset bytes from
8178the beginning of the file. The file offset remains unchanged.
8179[clinic start generated code]*/
8180
Larry Hastings2f936352014-08-05 14:04:04 +10008181static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008182os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset)
8183/*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008184{
Ross Lagerwall7807c352011-03-17 20:20:30 +02008185 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008186 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008187 PyObject *buffer;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008188
Larry Hastings2f936352014-08-05 14:04:04 +10008189 if (length < 0) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008190 errno = EINVAL;
8191 return posix_error();
8192 }
Larry Hastings2f936352014-08-05 14:04:04 +10008193 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008194 if (buffer == NULL)
8195 return NULL;
8196 if (!_PyVerify_fd(fd)) {
8197 Py_DECREF(buffer);
8198 return posix_error();
8199 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008200
8201 do {
8202 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008203 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008204 n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04008205 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008206 Py_END_ALLOW_THREADS
8207 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8208
Ross Lagerwall7807c352011-03-17 20:20:30 +02008209 if (n < 0) {
8210 Py_DECREF(buffer);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008211 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008212 }
Larry Hastings2f936352014-08-05 14:04:04 +10008213 if (n != length)
Ross Lagerwall7807c352011-03-17 20:20:30 +02008214 _PyBytes_Resize(&buffer, n);
8215 return buffer;
8216}
Larry Hastings2f936352014-08-05 14:04:04 +10008217#endif /* HAVE_PREAD */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008218
Larry Hastings2f936352014-08-05 14:04:04 +10008219
8220/*[clinic input]
8221os.write -> Py_ssize_t
8222
8223 fd: int
8224 data: Py_buffer
8225 /
8226
8227Write a bytes object to a file descriptor.
8228[clinic start generated code]*/
8229
Larry Hastings2f936352014-08-05 14:04:04 +10008230static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008231os_write_impl(PyObject *module, int fd, Py_buffer *data)
8232/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008233{
Victor Stinner66aab0c2015-03-19 22:53:20 +01008234 return _Py_write(fd, data->buf, data->len);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008235}
8236
8237#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008238PyDoc_STRVAR(posix_sendfile__doc__,
Martin Panterbf19d162015-09-09 01:01:13 +00008239"sendfile(out, in, offset, count) -> byteswritten\n\
Martin Panter94994132015-09-09 05:29:24 +00008240sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008241 -> byteswritten\n\
Martin Panterbf19d162015-09-09 01:01:13 +00008242Copy count bytes from file descriptor in to file descriptor out.");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008243
Larry Hastings2f936352014-08-05 14:04:04 +10008244/* AC 3.5: don't bother converting, has optional group*/
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008245static PyObject *
8246posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
8247{
8248 int in, out;
8249 Py_ssize_t ret;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008250 int async_err = 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008251 off_t offset;
8252
8253#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
8254#ifndef __APPLE__
8255 Py_ssize_t len;
8256#endif
8257 PyObject *headers = NULL, *trailers = NULL;
8258 Py_buffer *hbuf, *tbuf;
8259 off_t sbytes;
8260 struct sf_hdtr sf;
8261 int flags = 0;
Martin Panterbf19d162015-09-09 01:01:13 +00008262 /* Beware that "in" clashes with Python's own "in" operator keyword */
Benjamin Petersond8a43b42011-02-26 21:35:16 +00008263 static char *keywords[] = {"out", "in",
8264 "offset", "count",
8265 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008266
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02008267 sf.headers = NULL;
8268 sf.trailers = NULL;
8269
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008270#ifdef __APPLE__
8271 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008272 keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008273#else
8274 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008275 keywords, &out, &in, Py_off_t_converter, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008276#endif
8277 &headers, &trailers, &flags))
8278 return NULL;
8279 if (headers != NULL) {
8280 if (!PySequence_Check(headers)) {
8281 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00008282 "sendfile() headers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008283 return NULL;
8284 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008285 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008286 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008287 if (sf.hdr_cnt > 0 &&
Victor Stinner57ddf782014-01-08 15:21:28 +01008288 (i = iov_setup(&(sf.headers), &hbuf,
8289 headers, sf.hdr_cnt, PyBUF_SIMPLE)) < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008290 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008291#ifdef __APPLE__
8292 sbytes += i;
8293#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008294 }
8295 }
8296 if (trailers != NULL) {
8297 if (!PySequence_Check(trailers)) {
8298 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00008299 "sendfile() trailers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008300 return NULL;
8301 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008302 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008303 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008304 if (sf.trl_cnt > 0 &&
Victor Stinner57ddf782014-01-08 15:21:28 +01008305 (i = iov_setup(&(sf.trailers), &tbuf,
8306 trailers, sf.trl_cnt, PyBUF_SIMPLE)) < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008307 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008308#ifdef __APPLE__
8309 sbytes += i;
8310#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008311 }
8312 }
8313
Steve Dower8fc89802015-04-12 00:26:27 -04008314 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008315 do {
8316 Py_BEGIN_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008317#ifdef __APPLE__
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008318 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008319#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008320 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008321#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008322 Py_END_ALLOW_THREADS
8323 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04008324 _Py_END_SUPPRESS_IPH
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008325
8326 if (sf.headers != NULL)
8327 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
8328 if (sf.trailers != NULL)
8329 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
8330
8331 if (ret < 0) {
8332 if ((errno == EAGAIN) || (errno == EBUSY)) {
8333 if (sbytes != 0) {
8334 // some data has been sent
8335 goto done;
8336 }
8337 else {
8338 // no data has been sent; upper application is supposed
8339 // to retry on EAGAIN or EBUSY
8340 return posix_error();
8341 }
8342 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008343 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008344 }
8345 goto done;
8346
8347done:
8348 #if !defined(HAVE_LARGEFILE_SUPPORT)
8349 return Py_BuildValue("l", sbytes);
8350 #else
8351 return Py_BuildValue("L", sbytes);
8352 #endif
8353
8354#else
8355 Py_ssize_t count;
8356 PyObject *offobj;
8357 static char *keywords[] = {"out", "in",
8358 "offset", "count", NULL};
8359 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
8360 keywords, &out, &in, &offobj, &count))
8361 return NULL;
8362#ifdef linux
8363 if (offobj == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008364 do {
8365 Py_BEGIN_ALLOW_THREADS
8366 ret = sendfile(out, in, NULL, count);
8367 Py_END_ALLOW_THREADS
8368 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008369 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008370 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02008371 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008372 }
8373#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008374 if (!Py_off_t_converter(offobj, &offset))
Antoine Pitroudcc20b82011-02-26 13:38:35 +00008375 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008376
8377 do {
8378 Py_BEGIN_ALLOW_THREADS
8379 ret = sendfile(out, in, &offset, count);
8380 Py_END_ALLOW_THREADS
8381 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008382 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008383 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008384 return Py_BuildValue("n", ret);
8385#endif
8386}
Larry Hastings2f936352014-08-05 14:04:04 +10008387#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008388
Larry Hastings2f936352014-08-05 14:04:04 +10008389
8390/*[clinic input]
8391os.fstat
8392
8393 fd : int
8394
8395Perform a stat system call on the given file descriptor.
8396
8397Like stat(), but for an open file descriptor.
8398Equivalent to os.stat(fd).
8399[clinic start generated code]*/
8400
Larry Hastings2f936352014-08-05 14:04:04 +10008401static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008402os_fstat_impl(PyObject *module, int fd)
8403/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008404{
Victor Stinner8c62be82010-05-06 00:08:46 +00008405 STRUCT_STAT st;
8406 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008407 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008408
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008409 do {
8410 Py_BEGIN_ALLOW_THREADS
8411 res = FSTAT(fd, &st);
8412 Py_END_ALLOW_THREADS
8413 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +00008414 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00008415#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01008416 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00008417#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008418 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00008419#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008420 }
Tim Peters5aa91602002-01-30 05:46:57 +00008421
Victor Stinner4195b5c2012-02-08 23:03:19 +01008422 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00008423}
8424
Larry Hastings2f936352014-08-05 14:04:04 +10008425
8426/*[clinic input]
8427os.isatty -> bool
8428 fd: int
8429 /
8430
8431Return True if the fd is connected to a terminal.
8432
8433Return True if the file descriptor is an open file descriptor
8434connected to the slave end of a terminal.
8435[clinic start generated code]*/
8436
Larry Hastings2f936352014-08-05 14:04:04 +10008437static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008438os_isatty_impl(PyObject *module, int fd)
8439/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008440{
Steve Dower8fc89802015-04-12 00:26:27 -04008441 int return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10008442 if (!_PyVerify_fd(fd))
8443 return 0;
Steve Dower8fc89802015-04-12 00:26:27 -04008444 _Py_BEGIN_SUPPRESS_IPH
8445 return_value = isatty(fd);
8446 _Py_END_SUPPRESS_IPH
8447 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10008448}
8449
8450
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008451#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +10008452/*[clinic input]
8453os.pipe
8454
8455Create a pipe.
8456
8457Returns a tuple of two file descriptors:
8458 (read_fd, write_fd)
8459[clinic start generated code]*/
8460
Larry Hastings2f936352014-08-05 14:04:04 +10008461static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008462os_pipe_impl(PyObject *module)
8463/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00008464{
Victor Stinner8c62be82010-05-06 00:08:46 +00008465 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +02008466#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008467 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008468 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +00008469 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008470#else
8471 int res;
8472#endif
8473
8474#ifdef MS_WINDOWS
8475 attr.nLength = sizeof(attr);
8476 attr.lpSecurityDescriptor = NULL;
8477 attr.bInheritHandle = FALSE;
8478
8479 Py_BEGIN_ALLOW_THREADS
8480 ok = CreatePipe(&read, &write, &attr, 0);
8481 if (ok) {
8482 fds[0] = _open_osfhandle((Py_intptr_t)read, _O_RDONLY);
8483 fds[1] = _open_osfhandle((Py_intptr_t)write, _O_WRONLY);
8484 if (fds[0] == -1 || fds[1] == -1) {
8485 CloseHandle(read);
8486 CloseHandle(write);
8487 ok = 0;
8488 }
8489 }
8490 Py_END_ALLOW_THREADS
8491
Victor Stinner8c62be82010-05-06 00:08:46 +00008492 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01008493 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +02008494#else
8495
8496#ifdef HAVE_PIPE2
8497 Py_BEGIN_ALLOW_THREADS
8498 res = pipe2(fds, O_CLOEXEC);
8499 Py_END_ALLOW_THREADS
8500
8501 if (res != 0 && errno == ENOSYS)
8502 {
8503#endif
8504 Py_BEGIN_ALLOW_THREADS
8505 res = pipe(fds);
8506 Py_END_ALLOW_THREADS
8507
8508 if (res == 0) {
8509 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
8510 close(fds[0]);
8511 close(fds[1]);
8512 return NULL;
8513 }
8514 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
8515 close(fds[0]);
8516 close(fds[1]);
8517 return NULL;
8518 }
8519 }
8520#ifdef HAVE_PIPE2
8521 }
8522#endif
8523
8524 if (res != 0)
8525 return PyErr_SetFromErrno(PyExc_OSError);
8526#endif /* !MS_WINDOWS */
8527 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +00008528}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008529#endif /* HAVE_PIPE */
8530
Larry Hastings2f936352014-08-05 14:04:04 +10008531
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008532#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +10008533/*[clinic input]
8534os.pipe2
8535
8536 flags: int
8537 /
8538
8539Create a pipe with flags set atomically.
8540
8541Returns a tuple of two file descriptors:
8542 (read_fd, write_fd)
8543
8544flags can be constructed by ORing together one or more of these values:
8545O_NONBLOCK, O_CLOEXEC.
8546[clinic start generated code]*/
8547
Larry Hastings2f936352014-08-05 14:04:04 +10008548static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008549os_pipe2_impl(PyObject *module, int flags)
8550/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008551{
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008552 int fds[2];
8553 int res;
8554
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008555 res = pipe2(fds, flags);
8556 if (res != 0)
8557 return posix_error();
8558 return Py_BuildValue("(ii)", fds[0], fds[1]);
8559}
8560#endif /* HAVE_PIPE2 */
8561
Larry Hastings2f936352014-08-05 14:04:04 +10008562
Ross Lagerwall7807c352011-03-17 20:20:30 +02008563#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +10008564/*[clinic input]
8565os.writev -> Py_ssize_t
8566 fd: int
8567 buffers: object
8568 /
8569
8570Iterate over buffers, and write the contents of each to a file descriptor.
8571
8572Returns the total number of bytes written.
8573buffers must be a sequence of bytes-like objects.
8574[clinic start generated code]*/
8575
Larry Hastings2f936352014-08-05 14:04:04 +10008576static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008577os_writev_impl(PyObject *module, int fd, PyObject *buffers)
8578/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008579{
8580 int cnt;
8581 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008582 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008583 struct iovec *iov;
8584 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +10008585
8586 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008587 PyErr_SetString(PyExc_TypeError,
8588 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10008589 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008590 }
Larry Hastings2f936352014-08-05 14:04:04 +10008591 cnt = PySequence_Size(buffers);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008592
Larry Hastings2f936352014-08-05 14:04:04 +10008593 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
8594 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008595 }
8596
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008597 do {
8598 Py_BEGIN_ALLOW_THREADS
8599 result = writev(fd, iov, cnt);
8600 Py_END_ALLOW_THREADS
8601 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008602
8603 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008604 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10008605 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +01008606
Georg Brandl306336b2012-06-24 12:55:33 +02008607 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008608}
Larry Hastings2f936352014-08-05 14:04:04 +10008609#endif /* HAVE_WRITEV */
8610
8611
8612#ifdef HAVE_PWRITE
8613/*[clinic input]
8614os.pwrite -> Py_ssize_t
8615
8616 fd: int
8617 buffer: Py_buffer
8618 offset: Py_off_t
8619 /
8620
8621Write bytes to a file descriptor starting at a particular offset.
8622
8623Write buffer to fd, starting at offset bytes from the beginning of
8624the file. Returns the number of bytes writte. Does not change the
8625current file offset.
8626[clinic start generated code]*/
8627
Larry Hastings2f936352014-08-05 14:04:04 +10008628static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008629os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
8630/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008631{
8632 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008633 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008634
8635 if (!_PyVerify_fd(fd)) {
8636 posix_error();
8637 return -1;
8638 }
8639
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008640 do {
8641 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008642 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008643 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04008644 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008645 Py_END_ALLOW_THREADS
8646 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10008647
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008648 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10008649 posix_error();
8650 return size;
8651}
8652#endif /* HAVE_PWRITE */
8653
8654
8655#ifdef HAVE_MKFIFO
8656/*[clinic input]
8657os.mkfifo
8658
8659 path: path_t
8660 mode: int=0o666
8661 *
8662 dir_fd: dir_fd(requires='mkfifoat')=None
8663
8664Create a "fifo" (a POSIX named pipe).
8665
8666If dir_fd is not None, it should be a file descriptor open to a directory,
8667 and path should be relative; path will then be relative to that directory.
8668dir_fd may not be implemented on your platform.
8669 If it is unavailable, using it will raise a NotImplementedError.
8670[clinic start generated code]*/
8671
Larry Hastings2f936352014-08-05 14:04:04 +10008672static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008673os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
8674/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008675{
8676 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008677 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008678
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008679 do {
8680 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008681#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008682 if (dir_fd != DEFAULT_DIR_FD)
8683 result = mkfifoat(dir_fd, path->narrow, mode);
8684 else
Ross Lagerwall7807c352011-03-17 20:20:30 +02008685#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008686 result = mkfifo(path->narrow, mode);
8687 Py_END_ALLOW_THREADS
8688 } while (result != 0 && errno == EINTR &&
8689 !(async_err = PyErr_CheckSignals()));
8690 if (result != 0)
8691 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10008692
8693 Py_RETURN_NONE;
8694}
8695#endif /* HAVE_MKFIFO */
8696
8697
8698#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
8699/*[clinic input]
8700os.mknod
8701
8702 path: path_t
8703 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008704 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +10008705 *
8706 dir_fd: dir_fd(requires='mknodat')=None
8707
8708Create a node in the file system.
8709
8710Create a node in the file system (file, device special file or named pipe)
8711at path. mode specifies both the permissions to use and the
8712type of node to be created, being combined (bitwise OR) with one of
8713S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
8714device defines the newly created device special file (probably using
8715os.makedev()). Otherwise device is ignored.
8716
8717If dir_fd is not None, it should be a file descriptor open to a directory,
8718 and path should be relative; path will then be relative to that directory.
8719dir_fd may not be implemented on your platform.
8720 If it is unavailable, using it will raise a NotImplementedError.
8721[clinic start generated code]*/
8722
Larry Hastings2f936352014-08-05 14:04:04 +10008723static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008724os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04008725 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008726/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008727{
8728 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008729 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008730
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008731 do {
8732 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008733#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008734 if (dir_fd != DEFAULT_DIR_FD)
8735 result = mknodat(dir_fd, path->narrow, mode, device);
8736 else
Larry Hastings2f936352014-08-05 14:04:04 +10008737#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008738 result = mknod(path->narrow, mode, device);
8739 Py_END_ALLOW_THREADS
8740 } while (result != 0 && errno == EINTR &&
8741 !(async_err = PyErr_CheckSignals()));
8742 if (result != 0)
8743 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10008744
8745 Py_RETURN_NONE;
8746}
8747#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
8748
8749
8750#ifdef HAVE_DEVICE_MACROS
8751/*[clinic input]
8752os.major -> unsigned_int
8753
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008754 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10008755 /
8756
8757Extracts a device major number from a raw device number.
8758[clinic start generated code]*/
8759
Larry Hastings2f936352014-08-05 14:04:04 +10008760static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008761os_major_impl(PyObject *module, dev_t device)
8762/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008763{
8764 return major(device);
8765}
8766
8767
8768/*[clinic input]
8769os.minor -> unsigned_int
8770
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008771 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10008772 /
8773
8774Extracts a device minor number from a raw device number.
8775[clinic start generated code]*/
8776
Larry Hastings2f936352014-08-05 14:04:04 +10008777static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008778os_minor_impl(PyObject *module, dev_t device)
8779/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008780{
8781 return minor(device);
8782}
8783
8784
8785/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008786os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10008787
8788 major: int
8789 minor: int
8790 /
8791
8792Composes a raw device number from the major and minor device numbers.
8793[clinic start generated code]*/
8794
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008795static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008796os_makedev_impl(PyObject *module, int major, int minor)
8797/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008798{
8799 return makedev(major, minor);
8800}
8801#endif /* HAVE_DEVICE_MACROS */
8802
8803
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008804#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008805/*[clinic input]
8806os.ftruncate
8807
8808 fd: int
8809 length: Py_off_t
8810 /
8811
8812Truncate a file, specified by file descriptor, to a specific length.
8813[clinic start generated code]*/
8814
Larry Hastings2f936352014-08-05 14:04:04 +10008815static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008816os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
8817/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008818{
8819 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008820 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008821
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008822 if (!_PyVerify_fd(fd))
8823 return posix_error();
8824
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008825 do {
8826 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04008827 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008828#ifdef MS_WINDOWS
8829 result = _chsize_s(fd, length);
8830#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008831 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008832#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04008833 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008834 Py_END_ALLOW_THREADS
8835 } while (result != 0 && errno == EINTR &&
8836 !(async_err = PyErr_CheckSignals()));
8837 if (result != 0)
8838 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10008839 Py_RETURN_NONE;
8840}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008841#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10008842
8843
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008844#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008845/*[clinic input]
8846os.truncate
8847 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
8848 length: Py_off_t
8849
8850Truncate a file, specified by path, to a specific length.
8851
8852On some platforms, path may also be specified as an open file descriptor.
8853 If this functionality is unavailable, using it raises an exception.
8854[clinic start generated code]*/
8855
Larry Hastings2f936352014-08-05 14:04:04 +10008856static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008857os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
8858/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008859{
8860 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008861#ifdef MS_WINDOWS
8862 int fd;
8863#endif
8864
8865 if (path->fd != -1)
8866 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +10008867
8868 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04008869 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008870#ifdef MS_WINDOWS
8871 if (path->wide)
8872 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Larry Hastings2f936352014-08-05 14:04:04 +10008873 else
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008874 fd = _open(path->narrow, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +02008875 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008876 result = -1;
8877 else {
8878 result = _chsize_s(fd, length);
8879 close(fd);
8880 if (result < 0)
8881 errno = result;
8882 }
8883#else
8884 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +10008885#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04008886 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10008887 Py_END_ALLOW_THREADS
8888 if (result < 0)
8889 return path_error(path);
8890
8891 Py_RETURN_NONE;
8892}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008893#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10008894
Ross Lagerwall7807c352011-03-17 20:20:30 +02008895
Victor Stinnerd6b17692014-09-30 12:20:05 +02008896/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
8897 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
8898 defined, which is the case in Python on AIX. AIX bug report:
8899 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
8900#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
8901# define POSIX_FADVISE_AIX_BUG
8902#endif
8903
Victor Stinnerec39e262014-09-30 12:35:58 +02008904
Victor Stinnerd6b17692014-09-30 12:20:05 +02008905#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10008906/*[clinic input]
8907os.posix_fallocate
8908
8909 fd: int
8910 offset: Py_off_t
8911 length: Py_off_t
8912 /
8913
8914Ensure a file has allocated at least a particular number of bytes on disk.
8915
8916Ensure that the file specified by fd encompasses a range of bytes
8917starting at offset bytes from the beginning and continuing for length bytes.
8918[clinic start generated code]*/
8919
Larry Hastings2f936352014-08-05 14:04:04 +10008920static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008921os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04008922 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008923/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008924{
8925 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008926 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008927
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008928 do {
8929 Py_BEGIN_ALLOW_THREADS
8930 result = posix_fallocate(fd, offset, length);
8931 Py_END_ALLOW_THREADS
8932 } while (result != 0 && errno == EINTR &&
8933 !(async_err = PyErr_CheckSignals()));
8934 if (result != 0)
8935 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008936 Py_RETURN_NONE;
8937}
Victor Stinnerec39e262014-09-30 12:35:58 +02008938#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +10008939
Ross Lagerwall7807c352011-03-17 20:20:30 +02008940
Victor Stinnerd6b17692014-09-30 12:20:05 +02008941#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10008942/*[clinic input]
8943os.posix_fadvise
8944
8945 fd: int
8946 offset: Py_off_t
8947 length: Py_off_t
8948 advice: int
8949 /
8950
8951Announce an intention to access data in a specific pattern.
8952
8953Announce an intention to access data in a specific pattern, thus allowing
8954the kernel to make optimizations.
8955The advice applies to the region of the file specified by fd starting at
8956offset and continuing for length bytes.
8957advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
8958POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
8959POSIX_FADV_DONTNEED.
8960[clinic start generated code]*/
8961
Larry Hastings2f936352014-08-05 14:04:04 +10008962static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008963os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04008964 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008965/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008966{
8967 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008968 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008969
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008970 do {
8971 Py_BEGIN_ALLOW_THREADS
8972 result = posix_fadvise(fd, offset, length, advice);
8973 Py_END_ALLOW_THREADS
8974 } while (result != 0 && errno == EINTR &&
8975 !(async_err = PyErr_CheckSignals()));
8976 if (result != 0)
8977 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008978 Py_RETURN_NONE;
8979}
Victor Stinnerec39e262014-09-30 12:35:58 +02008980#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008981
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008982#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008983
Fred Drake762e2061999-08-26 17:23:54 +00008984/* Save putenv() parameters as values here, so we can collect them when they
8985 * get re-set with another call for the same key. */
8986static PyObject *posix_putenv_garbage;
8987
Larry Hastings2f936352014-08-05 14:04:04 +10008988static void
8989posix_putenv_garbage_setitem(PyObject *name, PyObject *value)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008990{
Larry Hastings2f936352014-08-05 14:04:04 +10008991 /* Install the first arg and newstr in posix_putenv_garbage;
8992 * this will cause previous value to be collected. This has to
8993 * happen after the real putenv() call because the old value
8994 * was still accessible until then. */
8995 if (PyDict_SetItem(posix_putenv_garbage, name, value))
8996 /* really not much we can do; just leak */
8997 PyErr_Clear();
8998 else
8999 Py_DECREF(value);
9000}
9001
9002
Thomas Hellerf78f12a2007-11-08 19:33:05 +00009003#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009004/*[clinic input]
9005os.putenv
9006
9007 name: unicode
9008 value: unicode
9009 /
9010
9011Change or add an environment variable.
9012[clinic start generated code]*/
9013
Larry Hastings2f936352014-08-05 14:04:04 +10009014static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009015os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9016/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009017{
9018 wchar_t *env;
9019
9020 PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value);
9021 if (unicode == NULL) {
Victor Stinner84ae1182010-05-06 22:05:07 +00009022 PyErr_NoMemory();
Larry Hastings2f936352014-08-05 14:04:04 +10009023 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00009024 }
Larry Hastings2f936352014-08-05 14:04:04 +10009025 if (_MAX_ENV < PyUnicode_GET_LENGTH(unicode)) {
Victor Stinner65170952011-11-22 22:16:17 +01009026 PyErr_Format(PyExc_ValueError,
9027 "the environment variable is longer than %u characters",
9028 _MAX_ENV);
9029 goto error;
9030 }
9031
Larry Hastings2f936352014-08-05 14:04:04 +10009032 env = PyUnicode_AsUnicode(unicode);
9033 if (env == NULL)
Victor Stinnereb5657a2011-09-30 01:44:27 +02009034 goto error;
Larry Hastings2f936352014-08-05 14:04:04 +10009035 if (_wputenv(env)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009036 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00009037 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00009038 }
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009039
Larry Hastings2f936352014-08-05 14:04:04 +10009040 posix_putenv_garbage_setitem(name, unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00009041 Py_RETURN_NONE;
9042
9043error:
Larry Hastings2f936352014-08-05 14:04:04 +10009044 Py_DECREF(unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00009045 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009046}
Larry Hastings2f936352014-08-05 14:04:04 +10009047#else /* MS_WINDOWS */
9048/*[clinic input]
9049os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +00009050
Larry Hastings2f936352014-08-05 14:04:04 +10009051 name: FSConverter
9052 value: FSConverter
9053 /
9054
9055Change or add an environment variable.
9056[clinic start generated code]*/
9057
Larry Hastings2f936352014-08-05 14:04:04 +10009058static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009059os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9060/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009061{
9062 PyObject *bytes = NULL;
9063 char *env;
9064 char *name_string = PyBytes_AsString(name);
9065 char *value_string = PyBytes_AsString(value);
9066
9067 bytes = PyBytes_FromFormat("%s=%s", name_string, value_string);
9068 if (bytes == NULL) {
9069 PyErr_NoMemory();
9070 return NULL;
9071 }
9072
9073 env = PyBytes_AS_STRING(bytes);
9074 if (putenv(env)) {
9075 Py_DECREF(bytes);
9076 return posix_error();
9077 }
9078
9079 posix_putenv_garbage_setitem(name, bytes);
9080 Py_RETURN_NONE;
9081}
9082#endif /* MS_WINDOWS */
9083#endif /* HAVE_PUTENV */
9084
9085
9086#ifdef HAVE_UNSETENV
9087/*[clinic input]
9088os.unsetenv
9089 name: FSConverter
9090 /
9091
9092Delete an environment variable.
9093[clinic start generated code]*/
9094
Larry Hastings2f936352014-08-05 14:04:04 +10009095static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009096os_unsetenv_impl(PyObject *module, PyObject *name)
9097/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009098{
Victor Stinner984890f2011-11-24 13:53:38 +01009099#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +01009100 int err;
Victor Stinner984890f2011-11-24 13:53:38 +01009101#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00009102
Victor Stinner984890f2011-11-24 13:53:38 +01009103#ifdef HAVE_BROKEN_UNSETENV
9104 unsetenv(PyBytes_AS_STRING(name));
9105#else
Victor Stinner65170952011-11-22 22:16:17 +01009106 err = unsetenv(PyBytes_AS_STRING(name));
Larry Hastings2f936352014-08-05 14:04:04 +10009107 if (err)
Victor Stinner60b385e2011-11-22 22:01:28 +01009108 return posix_error();
Victor Stinner984890f2011-11-24 13:53:38 +01009109#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00009110
Victor Stinner8c62be82010-05-06 00:08:46 +00009111 /* Remove the key from posix_putenv_garbage;
9112 * this will cause it to be collected. This has to
9113 * happen after the real unsetenv() call because the
9114 * old value was still accessible until then.
9115 */
Victor Stinner65170952011-11-22 22:16:17 +01009116 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009117 /* really not much we can do; just leak */
9118 PyErr_Clear();
9119 }
Victor Stinner84ae1182010-05-06 22:05:07 +00009120 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00009121}
Larry Hastings2f936352014-08-05 14:04:04 +10009122#endif /* HAVE_UNSETENV */
Guido van Rossumc524d952001-10-19 01:31:59 +00009123
Larry Hastings2f936352014-08-05 14:04:04 +10009124
9125/*[clinic input]
9126os.strerror
9127
9128 code: int
9129 /
9130
9131Translate an error code to a message string.
9132[clinic start generated code]*/
9133
Larry Hastings2f936352014-08-05 14:04:04 +10009134static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009135os_strerror_impl(PyObject *module, int code)
9136/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009137{
9138 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +00009139 if (message == NULL) {
9140 PyErr_SetString(PyExc_ValueError,
9141 "strerror() argument out of range");
9142 return NULL;
9143 }
Victor Stinner1b579672011-12-17 05:47:23 +01009144 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +00009145}
Guido van Rossumb6a47161997-09-15 22:54:34 +00009146
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009147
Guido van Rossumc9641791998-08-04 15:26:23 +00009148#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00009149#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +10009150/*[clinic input]
9151os.WCOREDUMP -> bool
9152
9153 status: int
9154 /
9155
9156Return True if the process returning status was dumped to a core file.
9157[clinic start generated code]*/
9158
Larry Hastings2f936352014-08-05 14:04:04 +10009159static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009160os_WCOREDUMP_impl(PyObject *module, int status)
9161/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009162{
9163 WAIT_TYPE wait_status;
9164 WAIT_STATUS_INT(wait_status) = status;
9165 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +00009166}
9167#endif /* WCOREDUMP */
9168
Larry Hastings2f936352014-08-05 14:04:04 +10009169
Fred Drake106c1a02002-04-23 15:58:02 +00009170#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +10009171/*[clinic input]
9172os.WIFCONTINUED -> bool
9173
9174 status: int
9175
9176Return True if a particular process was continued from a job control stop.
9177
9178Return True if the process returning status was continued from a
9179job control stop.
9180[clinic start generated code]*/
9181
Larry Hastings2f936352014-08-05 14:04:04 +10009182static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009183os_WIFCONTINUED_impl(PyObject *module, int status)
9184/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009185{
9186 WAIT_TYPE wait_status;
9187 WAIT_STATUS_INT(wait_status) = status;
9188 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +00009189}
9190#endif /* WIFCONTINUED */
9191
Larry Hastings2f936352014-08-05 14:04:04 +10009192
Guido van Rossumc9641791998-08-04 15:26:23 +00009193#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +10009194/*[clinic input]
9195os.WIFSTOPPED -> bool
9196
9197 status: int
9198
9199Return True if the process returning status was stopped.
9200[clinic start generated code]*/
9201
Larry Hastings2f936352014-08-05 14:04:04 +10009202static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009203os_WIFSTOPPED_impl(PyObject *module, int status)
9204/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009205{
9206 WAIT_TYPE wait_status;
9207 WAIT_STATUS_INT(wait_status) = status;
9208 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009209}
9210#endif /* WIFSTOPPED */
9211
Larry Hastings2f936352014-08-05 14:04:04 +10009212
Guido van Rossumc9641791998-08-04 15:26:23 +00009213#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +10009214/*[clinic input]
9215os.WIFSIGNALED -> bool
9216
9217 status: int
9218
9219Return True if the process returning status was terminated by a signal.
9220[clinic start generated code]*/
9221
Larry Hastings2f936352014-08-05 14:04:04 +10009222static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009223os_WIFSIGNALED_impl(PyObject *module, int status)
9224/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009225{
9226 WAIT_TYPE wait_status;
9227 WAIT_STATUS_INT(wait_status) = status;
9228 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009229}
9230#endif /* WIFSIGNALED */
9231
Larry Hastings2f936352014-08-05 14:04:04 +10009232
Guido van Rossumc9641791998-08-04 15:26:23 +00009233#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +10009234/*[clinic input]
9235os.WIFEXITED -> bool
9236
9237 status: int
9238
9239Return True if the process returning status exited via the exit() system call.
9240[clinic start generated code]*/
9241
Larry Hastings2f936352014-08-05 14:04:04 +10009242static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009243os_WIFEXITED_impl(PyObject *module, int status)
9244/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009245{
9246 WAIT_TYPE wait_status;
9247 WAIT_STATUS_INT(wait_status) = status;
9248 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009249}
9250#endif /* WIFEXITED */
9251
Larry Hastings2f936352014-08-05 14:04:04 +10009252
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00009253#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +10009254/*[clinic input]
9255os.WEXITSTATUS -> int
9256
9257 status: int
9258
9259Return the process return code from status.
9260[clinic start generated code]*/
9261
Larry Hastings2f936352014-08-05 14:04:04 +10009262static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009263os_WEXITSTATUS_impl(PyObject *module, int status)
9264/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009265{
9266 WAIT_TYPE wait_status;
9267 WAIT_STATUS_INT(wait_status) = status;
9268 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009269}
9270#endif /* WEXITSTATUS */
9271
Larry Hastings2f936352014-08-05 14:04:04 +10009272
Guido van Rossumc9641791998-08-04 15:26:23 +00009273#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +10009274/*[clinic input]
9275os.WTERMSIG -> int
9276
9277 status: int
9278
9279Return the signal that terminated the process that provided the status value.
9280[clinic start generated code]*/
9281
Larry Hastings2f936352014-08-05 14:04:04 +10009282static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009283os_WTERMSIG_impl(PyObject *module, int status)
9284/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009285{
9286 WAIT_TYPE wait_status;
9287 WAIT_STATUS_INT(wait_status) = status;
9288 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009289}
9290#endif /* WTERMSIG */
9291
Larry Hastings2f936352014-08-05 14:04:04 +10009292
Guido van Rossumc9641791998-08-04 15:26:23 +00009293#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +10009294/*[clinic input]
9295os.WSTOPSIG -> int
9296
9297 status: int
9298
9299Return the signal that stopped the process that provided the status value.
9300[clinic start generated code]*/
9301
Larry Hastings2f936352014-08-05 14:04:04 +10009302static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009303os_WSTOPSIG_impl(PyObject *module, int status)
9304/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009305{
9306 WAIT_TYPE wait_status;
9307 WAIT_STATUS_INT(wait_status) = status;
9308 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009309}
9310#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +00009311#endif /* HAVE_SYS_WAIT_H */
9312
9313
Thomas Wouters477c8d52006-05-27 19:21:47 +00009314#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00009315#ifdef _SCO_DS
9316/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
9317 needed definitions in sys/statvfs.h */
9318#define _SVID3
9319#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009320#include <sys/statvfs.h>
9321
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009322static PyObject*
9323_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009324 PyObject *v = PyStructSequence_New(&StatVFSResultType);
9325 if (v == NULL)
9326 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009327
9328#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00009329 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
9330 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
9331 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
9332 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
9333 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
9334 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
9335 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
9336 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
9337 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
9338 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009339#else
Victor Stinner8c62be82010-05-06 00:08:46 +00009340 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
9341 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
9342 PyStructSequence_SET_ITEM(v, 2,
9343 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
9344 PyStructSequence_SET_ITEM(v, 3,
9345 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
9346 PyStructSequence_SET_ITEM(v, 4,
9347 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
9348 PyStructSequence_SET_ITEM(v, 5,
9349 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
9350 PyStructSequence_SET_ITEM(v, 6,
9351 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
9352 PyStructSequence_SET_ITEM(v, 7,
9353 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
9354 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
9355 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009356#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +01009357 if (PyErr_Occurred()) {
9358 Py_DECREF(v);
9359 return NULL;
9360 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009361
Victor Stinner8c62be82010-05-06 00:08:46 +00009362 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009363}
9364
Larry Hastings2f936352014-08-05 14:04:04 +10009365
9366/*[clinic input]
9367os.fstatvfs
9368 fd: int
9369 /
9370
9371Perform an fstatvfs system call on the given fd.
9372
9373Equivalent to statvfs(fd).
9374[clinic start generated code]*/
9375
Larry Hastings2f936352014-08-05 14:04:04 +10009376static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009377os_fstatvfs_impl(PyObject *module, int fd)
9378/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009379{
9380 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009381 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00009382 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009383
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009384 do {
9385 Py_BEGIN_ALLOW_THREADS
9386 result = fstatvfs(fd, &st);
9387 Py_END_ALLOW_THREADS
9388 } while (result != 0 && errno == EINTR &&
9389 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10009390 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009391 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009392
Victor Stinner8c62be82010-05-06 00:08:46 +00009393 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00009394}
Larry Hastings2f936352014-08-05 14:04:04 +10009395#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +00009396
9397
Thomas Wouters477c8d52006-05-27 19:21:47 +00009398#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00009399#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +10009400/*[clinic input]
9401os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +00009402
Larry Hastings2f936352014-08-05 14:04:04 +10009403 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
9404
9405Perform a statvfs system call on the given path.
9406
9407path may always be specified as a string.
9408On some platforms, path may also be specified as an open file descriptor.
9409 If this functionality is unavailable, using it raises an exception.
9410[clinic start generated code]*/
9411
Larry Hastings2f936352014-08-05 14:04:04 +10009412static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009413os_statvfs_impl(PyObject *module, path_t *path)
9414/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009415{
9416 int result;
9417 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -07009418
9419 Py_BEGIN_ALLOW_THREADS
9420#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +10009421 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07009422#ifdef __APPLE__
9423 /* handle weak-linking on Mac OS X 10.3 */
9424 if (fstatvfs == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009425 fd_specified("statvfs", path->fd);
9426 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07009427 }
9428#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009429 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07009430 }
9431 else
9432#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009433 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07009434 Py_END_ALLOW_THREADS
9435
9436 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10009437 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07009438 }
9439
Larry Hastings2f936352014-08-05 14:04:04 +10009440 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00009441}
Larry Hastings2f936352014-08-05 14:04:04 +10009442#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
9443
Guido van Rossum94f6f721999-01-06 18:42:14 +00009444
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009445#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009446/*[clinic input]
9447os._getdiskusage
9448
9449 path: Py_UNICODE
9450
9451Return disk usage statistics about the given path as a (total, free) tuple.
9452[clinic start generated code]*/
9453
Larry Hastings2f936352014-08-05 14:04:04 +10009454static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009455os__getdiskusage_impl(PyObject *module, Py_UNICODE *path)
9456/*[clinic end generated code: output=76d6adcd86b1db0b input=6458133aed893c78]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009457{
9458 BOOL retval;
9459 ULARGE_INTEGER _, total, free;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009460
9461 Py_BEGIN_ALLOW_THREADS
Victor Stinner6139c1b2011-11-09 22:14:14 +01009462 retval = GetDiskFreeSpaceExW(path, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009463 Py_END_ALLOW_THREADS
9464 if (retval == 0)
9465 return PyErr_SetFromWindowsErr(0);
9466
9467 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
9468}
Larry Hastings2f936352014-08-05 14:04:04 +10009469#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009470
9471
Fred Drakec9680921999-12-13 16:37:25 +00009472/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
9473 * It maps strings representing configuration variable names to
9474 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00009475 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00009476 * rarely-used constants. There are three separate tables that use
9477 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00009478 *
9479 * This code is always included, even if none of the interfaces that
9480 * need it are included. The #if hackery needed to avoid it would be
9481 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00009482 */
9483struct constdef {
9484 char *name;
9485 long value;
9486};
9487
Fred Drake12c6e2d1999-12-14 21:25:03 +00009488static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009489conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00009490 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00009491{
Christian Heimes217cfd12007-12-02 14:31:20 +00009492 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00009493 *valuep = PyLong_AS_LONG(arg);
9494 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00009495 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00009496 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00009497 /* look up the value in the table using a binary search */
9498 size_t lo = 0;
9499 size_t mid;
9500 size_t hi = tablesize;
9501 int cmp;
9502 const char *confname;
9503 if (!PyUnicode_Check(arg)) {
9504 PyErr_SetString(PyExc_TypeError,
9505 "configuration names must be strings or integers");
9506 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00009507 }
Stefan Krah0e803b32010-11-26 16:16:47 +00009508 confname = _PyUnicode_AsString(arg);
9509 if (confname == NULL)
9510 return 0;
9511 while (lo < hi) {
9512 mid = (lo + hi) / 2;
9513 cmp = strcmp(confname, table[mid].name);
9514 if (cmp < 0)
9515 hi = mid;
9516 else if (cmp > 0)
9517 lo = mid + 1;
9518 else {
9519 *valuep = table[mid].value;
9520 return 1;
9521 }
9522 }
9523 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
9524 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00009525 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00009526}
9527
9528
9529#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
9530static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00009531#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009532 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00009533#endif
9534#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009535 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00009536#endif
Fred Drakec9680921999-12-13 16:37:25 +00009537#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009538 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009539#endif
9540#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00009541 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00009542#endif
9543#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00009544 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00009545#endif
9546#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00009547 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00009548#endif
9549#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009550 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009551#endif
9552#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00009553 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00009554#endif
9555#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00009556 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00009557#endif
9558#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009559 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009560#endif
9561#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009562 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00009563#endif
9564#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009565 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009566#endif
9567#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00009568 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00009569#endif
9570#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009571 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009572#endif
9573#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00009574 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00009575#endif
9576#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009577 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009578#endif
9579#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00009580 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00009581#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00009582#ifdef _PC_ACL_ENABLED
9583 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
9584#endif
9585#ifdef _PC_MIN_HOLE_SIZE
9586 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
9587#endif
9588#ifdef _PC_ALLOC_SIZE_MIN
9589 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
9590#endif
9591#ifdef _PC_REC_INCR_XFER_SIZE
9592 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
9593#endif
9594#ifdef _PC_REC_MAX_XFER_SIZE
9595 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
9596#endif
9597#ifdef _PC_REC_MIN_XFER_SIZE
9598 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
9599#endif
9600#ifdef _PC_REC_XFER_ALIGN
9601 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
9602#endif
9603#ifdef _PC_SYMLINK_MAX
9604 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
9605#endif
9606#ifdef _PC_XATTR_ENABLED
9607 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
9608#endif
9609#ifdef _PC_XATTR_EXISTS
9610 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
9611#endif
9612#ifdef _PC_TIMESTAMP_RESOLUTION
9613 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
9614#endif
Fred Drakec9680921999-12-13 16:37:25 +00009615};
9616
Fred Drakec9680921999-12-13 16:37:25 +00009617static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009618conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009619{
9620 return conv_confname(arg, valuep, posix_constants_pathconf,
9621 sizeof(posix_constants_pathconf)
9622 / sizeof(struct constdef));
9623}
9624#endif
9625
Larry Hastings2f936352014-08-05 14:04:04 +10009626
Fred Drakec9680921999-12-13 16:37:25 +00009627#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +10009628/*[clinic input]
9629os.fpathconf -> long
9630
9631 fd: int
9632 name: path_confname
9633 /
9634
9635Return the configuration limit name for the file descriptor fd.
9636
9637If there is no limit, return -1.
9638[clinic start generated code]*/
9639
Larry Hastings2f936352014-08-05 14:04:04 +10009640static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009641os_fpathconf_impl(PyObject *module, int fd, int name)
9642/*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009643{
9644 long limit;
9645
9646 errno = 0;
9647 limit = fpathconf(fd, name);
9648 if (limit == -1 && errno != 0)
9649 posix_error();
9650
9651 return limit;
9652}
9653#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +00009654
9655
9656#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +10009657/*[clinic input]
9658os.pathconf -> long
9659 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
9660 name: path_confname
9661
9662Return the configuration limit name for the file or directory path.
9663
9664If there is no limit, return -1.
9665On some platforms, path may also be specified as an open file descriptor.
9666 If this functionality is unavailable, using it raises an exception.
9667[clinic start generated code]*/
9668
Larry Hastings2f936352014-08-05 14:04:04 +10009669static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009670os_pathconf_impl(PyObject *module, path_t *path, int name)
9671/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009672{
Victor Stinner8c62be82010-05-06 00:08:46 +00009673 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00009674
Victor Stinner8c62be82010-05-06 00:08:46 +00009675 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +02009676#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +10009677 if (path->fd != -1)
9678 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +02009679 else
9680#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009681 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +00009682 if (limit == -1 && errno != 0) {
9683 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00009684 /* could be a path or name problem */
9685 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00009686 else
Larry Hastings2f936352014-08-05 14:04:04 +10009687 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00009688 }
Larry Hastings2f936352014-08-05 14:04:04 +10009689
9690 return limit;
Fred Drakec9680921999-12-13 16:37:25 +00009691}
Larry Hastings2f936352014-08-05 14:04:04 +10009692#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +00009693
9694#ifdef HAVE_CONFSTR
9695static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00009696#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00009697 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00009698#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00009699#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009700 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00009701#endif
9702#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009703 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00009704#endif
Fred Draked86ed291999-12-15 15:34:33 +00009705#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009706 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00009707#endif
9708#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00009709 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00009710#endif
9711#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00009712 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00009713#endif
9714#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009715 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00009716#endif
Fred Drakec9680921999-12-13 16:37:25 +00009717#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009718 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009719#endif
9720#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009721 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009722#endif
9723#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009724 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009725#endif
9726#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009727 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009728#endif
9729#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009730 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009731#endif
9732#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009733 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009734#endif
9735#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009736 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009737#endif
9738#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009739 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009740#endif
Fred Draked86ed291999-12-15 15:34:33 +00009741#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00009742 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00009743#endif
Fred Drakec9680921999-12-13 16:37:25 +00009744#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00009745 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00009746#endif
Fred Draked86ed291999-12-15 15:34:33 +00009747#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00009748 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00009749#endif
9750#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009751 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00009752#endif
9753#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009754 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00009755#endif
9756#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009757 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00009758#endif
Fred Drakec9680921999-12-13 16:37:25 +00009759#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009760 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009761#endif
9762#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009763 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009764#endif
9765#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009766 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009767#endif
9768#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009769 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009770#endif
9771#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009772 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009773#endif
9774#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009775 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009776#endif
9777#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009778 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009779#endif
9780#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009781 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009782#endif
9783#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009784 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009785#endif
9786#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009787 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009788#endif
9789#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009790 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009791#endif
9792#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009793 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009794#endif
9795#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009796 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009797#endif
9798#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009799 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009800#endif
9801#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009802 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009803#endif
9804#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009805 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009806#endif
Fred Draked86ed291999-12-15 15:34:33 +00009807#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00009808 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00009809#endif
9810#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00009811 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00009812#endif
9813#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00009814 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00009815#endif
9816#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009817 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00009818#endif
9819#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00009820 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00009821#endif
9822#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00009823 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00009824#endif
9825#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009826 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00009827#endif
9828#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00009829 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00009830#endif
9831#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009832 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00009833#endif
9834#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00009835 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00009836#endif
9837#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00009838 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00009839#endif
9840#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00009841 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00009842#endif
9843#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00009844 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00009845#endif
Fred Drakec9680921999-12-13 16:37:25 +00009846};
9847
9848static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009849conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009850{
9851 return conv_confname(arg, valuep, posix_constants_confstr,
9852 sizeof(posix_constants_confstr)
9853 / sizeof(struct constdef));
9854}
9855
Larry Hastings2f936352014-08-05 14:04:04 +10009856
9857/*[clinic input]
9858os.confstr
9859
9860 name: confstr_confname
9861 /
9862
9863Return a string-valued system configuration variable.
9864[clinic start generated code]*/
9865
Larry Hastings2f936352014-08-05 14:04:04 +10009866static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009867os_confstr_impl(PyObject *module, int name)
9868/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +00009869{
9870 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +00009871 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +02009872 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +00009873
Victor Stinnercb043522010-09-10 23:49:04 +00009874 errno = 0;
9875 len = confstr(name, buffer, sizeof(buffer));
9876 if (len == 0) {
9877 if (errno) {
9878 posix_error();
9879 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00009880 }
9881 else {
Victor Stinnercb043522010-09-10 23:49:04 +00009882 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00009883 }
9884 }
Victor Stinnercb043522010-09-10 23:49:04 +00009885
Victor Stinnerdd3a6a52013-06-25 23:13:47 +02009886 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +01009887 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +00009888 char *buf = PyMem_Malloc(len);
9889 if (buf == NULL)
9890 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +01009891 len2 = confstr(name, buf, len);
9892 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +02009893 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +00009894 PyMem_Free(buf);
9895 }
9896 else
9897 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00009898 return result;
9899}
Larry Hastings2f936352014-08-05 14:04:04 +10009900#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +00009901
9902
9903#ifdef HAVE_SYSCONF
9904static struct constdef posix_constants_sysconf[] = {
9905#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00009906 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00009907#endif
9908#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00009909 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00009910#endif
9911#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009912 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009913#endif
9914#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009915 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009916#endif
9917#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009918 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009919#endif
9920#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00009921 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00009922#endif
9923#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00009924 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00009925#endif
9926#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009927 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009928#endif
9929#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00009930 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00009931#endif
9932#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009933 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009934#endif
Fred Draked86ed291999-12-15 15:34:33 +00009935#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009936 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00009937#endif
9938#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00009939 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00009940#endif
Fred Drakec9680921999-12-13 16:37:25 +00009941#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009942 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009943#endif
Fred Drakec9680921999-12-13 16:37:25 +00009944#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009945 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009946#endif
9947#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009948 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009949#endif
9950#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009951 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009952#endif
9953#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009954 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009955#endif
9956#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009957 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009958#endif
Fred Draked86ed291999-12-15 15:34:33 +00009959#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009960 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00009961#endif
Fred Drakec9680921999-12-13 16:37:25 +00009962#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00009963 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00009964#endif
9965#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009966 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009967#endif
9968#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009969 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009970#endif
9971#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009972 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009973#endif
9974#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009975 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009976#endif
Fred Draked86ed291999-12-15 15:34:33 +00009977#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00009978 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00009979#endif
Fred Drakec9680921999-12-13 16:37:25 +00009980#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009981 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009982#endif
9983#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009984 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009985#endif
9986#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009987 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009988#endif
9989#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009990 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009991#endif
9992#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009993 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009994#endif
9995#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009996 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00009997#endif
9998#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009999 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010000#endif
10001#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010002 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010003#endif
10004#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000010005 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000010006#endif
10007#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010008 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010009#endif
10010#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010011 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000010012#endif
10013#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010014 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000010015#endif
10016#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010017 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010018#endif
10019#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010020 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010021#endif
10022#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010023 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010024#endif
10025#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010026 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010027#endif
10028#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010029 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +000010030#endif
10031#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010032 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010033#endif
10034#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010035 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010036#endif
10037#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000010038 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000010039#endif
10040#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010041 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010042#endif
10043#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010044 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000010045#endif
10046#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010047 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000010048#endif
Fred Draked86ed291999-12-15 15:34:33 +000010049#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +000010050 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +000010051#endif
Fred Drakec9680921999-12-13 16:37:25 +000010052#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010053 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010054#endif
10055#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010056 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010057#endif
10058#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010059 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010060#endif
Fred Draked86ed291999-12-15 15:34:33 +000010061#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010062 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +000010063#endif
Fred Drakec9680921999-12-13 16:37:25 +000010064#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +000010065 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +000010066#endif
Fred Draked86ed291999-12-15 15:34:33 +000010067#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +000010068 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +000010069#endif
10070#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +000010071 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +000010072#endif
Fred Drakec9680921999-12-13 16:37:25 +000010073#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010074 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010075#endif
10076#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010077 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010078#endif
10079#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010080 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010081#endif
10082#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010083 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000010084#endif
Fred Draked86ed291999-12-15 15:34:33 +000010085#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +000010086 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +000010087#endif
Fred Drakec9680921999-12-13 16:37:25 +000010088#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +000010089 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +000010090#endif
10091#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010092 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +000010093#endif
10094#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010095 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010096#endif
10097#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010098 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +000010099#endif
10100#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +000010101 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +000010102#endif
10103#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +000010104 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +000010105#endif
10106#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +000010107 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +000010108#endif
Fred Draked86ed291999-12-15 15:34:33 +000010109#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +000010110 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +000010111#endif
Fred Drakec9680921999-12-13 16:37:25 +000010112#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010113 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010114#endif
10115#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010116 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010117#endif
Fred Draked86ed291999-12-15 15:34:33 +000010118#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010119 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000010120#endif
Fred Drakec9680921999-12-13 16:37:25 +000010121#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010122 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010123#endif
10124#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010125 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010126#endif
10127#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010128 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010129#endif
10130#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010131 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010132#endif
10133#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010134 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010135#endif
10136#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010137 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010138#endif
10139#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010140 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010141#endif
10142#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010143 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +000010144#endif
10145#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000010146 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +000010147#endif
Fred Draked86ed291999-12-15 15:34:33 +000010148#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010149 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +000010150#endif
10151#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000010152 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +000010153#endif
Fred Drakec9680921999-12-13 16:37:25 +000010154#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +000010155 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +000010156#endif
10157#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010158 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010159#endif
10160#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000010161 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +000010162#endif
10163#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000010164 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +000010165#endif
10166#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010167 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010168#endif
10169#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000010170 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000010171#endif
10172#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +000010173 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +000010174#endif
10175#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +000010176 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +000010177#endif
10178#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +000010179 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +000010180#endif
10181#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +000010182 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +000010183#endif
10184#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +000010185 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +000010186#endif
10187#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010188 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +000010189#endif
10190#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010191 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +000010192#endif
10193#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +000010194 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +000010195#endif
10196#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +000010197 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +000010198#endif
10199#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +000010200 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +000010201#endif
10202#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +000010203 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +000010204#endif
10205#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010206 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010207#endif
10208#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000010209 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000010210#endif
10211#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +000010212 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +000010213#endif
10214#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010215 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010216#endif
10217#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010218 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010219#endif
10220#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +000010221 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +000010222#endif
10223#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010224 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010225#endif
10226#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010227 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010228#endif
10229#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000010230 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000010231#endif
10232#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000010233 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000010234#endif
10235#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010236 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010237#endif
10238#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010239 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010240#endif
10241#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010242 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000010243#endif
10244#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010245 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010246#endif
10247#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010248 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010249#endif
10250#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010251 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010252#endif
10253#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010254 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010255#endif
10256#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010257 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010258#endif
Fred Draked86ed291999-12-15 15:34:33 +000010259#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000010260 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000010261#endif
Fred Drakec9680921999-12-13 16:37:25 +000010262#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000010263 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000010264#endif
10265#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010266 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010267#endif
10268#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000010269 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000010270#endif
10271#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010272 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010273#endif
10274#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010275 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010276#endif
10277#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000010278 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000010279#endif
10280#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000010281 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000010282#endif
10283#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000010284 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000010285#endif
10286#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000010287 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000010288#endif
10289#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010290 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010291#endif
10292#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000010293 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000010294#endif
10295#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010296 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000010297#endif
10298#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000010299 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000010300#endif
10301#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000010302 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000010303#endif
10304#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000010305 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000010306#endif
10307#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010308 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010309#endif
10310#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010311 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010312#endif
10313#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000010314 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000010315#endif
10316#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010317 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010318#endif
10319#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010320 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010321#endif
10322#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010323 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010324#endif
10325#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010326 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010327#endif
10328#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010329 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010330#endif
10331#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010332 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010333#endif
10334#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000010335 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000010336#endif
10337#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010338 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010339#endif
10340#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010341 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010342#endif
10343#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010344 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010345#endif
10346#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010347 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000010348#endif
10349#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000010350 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000010351#endif
10352#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010353 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000010354#endif
10355#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000010356 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000010357#endif
10358#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010359 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000010360#endif
10361#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000010362 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000010363#endif
10364#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000010365 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000010366#endif
10367#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000010368 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000010369#endif
10370#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000010371 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000010372#endif
10373#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000010374 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000010375#endif
10376#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000010377 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000010378#endif
10379#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000010380 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000010381#endif
10382#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010383 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010384#endif
10385#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010386 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010387#endif
10388#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000010389 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000010390#endif
10391#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000010392 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000010393#endif
10394#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000010395 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000010396#endif
10397};
10398
10399static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010400conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010401{
10402 return conv_confname(arg, valuep, posix_constants_sysconf,
10403 sizeof(posix_constants_sysconf)
10404 / sizeof(struct constdef));
10405}
10406
Larry Hastings2f936352014-08-05 14:04:04 +100010407
10408/*[clinic input]
10409os.sysconf -> long
10410 name: sysconf_confname
10411 /
10412
10413Return an integer-valued system configuration variable.
10414[clinic start generated code]*/
10415
Larry Hastings2f936352014-08-05 14:04:04 +100010416static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010417os_sysconf_impl(PyObject *module, int name)
10418/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010419{
10420 long value;
10421
10422 errno = 0;
10423 value = sysconf(name);
10424 if (value == -1 && errno != 0)
10425 posix_error();
10426 return value;
10427}
10428#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010429
10430
Fred Drakebec628d1999-12-15 18:31:10 +000010431/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020010432 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000010433 * the exported dictionaries that are used to publish information about the
10434 * names available on the host platform.
10435 *
10436 * Sorting the table at runtime ensures that the table is properly ordered
10437 * when used, even for platforms we're not able to test on. It also makes
10438 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000010439 */
Fred Drakebec628d1999-12-15 18:31:10 +000010440
10441static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010442cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000010443{
10444 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000010445 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000010446 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000010447 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000010448
10449 return strcmp(c1->name, c2->name);
10450}
10451
10452static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010453setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +000010454 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000010455{
Fred Drakebec628d1999-12-15 18:31:10 +000010456 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000010457 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000010458
10459 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
10460 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000010461 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000010462 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010463
Barry Warsaw3155db32000-04-13 15:20:40 +000010464 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010465 PyObject *o = PyLong_FromLong(table[i].value);
10466 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
10467 Py_XDECREF(o);
10468 Py_DECREF(d);
10469 return -1;
10470 }
10471 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000010472 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000010473 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000010474}
10475
Fred Drakebec628d1999-12-15 18:31:10 +000010476/* Return -1 on failure, 0 on success. */
10477static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000010478setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000010479{
10480#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000010481 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000010482 sizeof(posix_constants_pathconf)
10483 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000010484 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000010485 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010486#endif
10487#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000010488 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000010489 sizeof(posix_constants_confstr)
10490 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000010491 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000010492 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010493#endif
10494#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000010495 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000010496 sizeof(posix_constants_sysconf)
10497 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000010498 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000010499 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010500#endif
Fred Drakebec628d1999-12-15 18:31:10 +000010501 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000010502}
Fred Draked86ed291999-12-15 15:34:33 +000010503
10504
Larry Hastings2f936352014-08-05 14:04:04 +100010505/*[clinic input]
10506os.abort
10507
10508Abort the interpreter immediately.
10509
10510This function 'dumps core' or otherwise fails in the hardest way possible
10511on the hosting operating system. This function never returns.
10512[clinic start generated code]*/
10513
Larry Hastings2f936352014-08-05 14:04:04 +100010514static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010515os_abort_impl(PyObject *module)
10516/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010517{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010518 abort();
10519 /*NOTREACHED*/
10520 Py_FatalError("abort() called from Python code didn't abort!");
10521 return NULL;
10522}
Fred Drakebec628d1999-12-15 18:31:10 +000010523
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010524#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010525/* AC 3.5: change to path_t? but that might change exceptions */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000010526PyDoc_STRVAR(win32_startfile__doc__,
Larry Hastings2f936352014-08-05 14:04:04 +100010527"startfile(filepath [, operation])\n\
10528\n\
10529Start a file with its associated application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +000010530\n\
Georg Brandlf4f44152006-02-18 22:29:33 +000010531When \"operation\" is not specified or \"open\", this acts like\n\
10532double-clicking the file in Explorer, or giving the file name as an\n\
10533argument to the DOS \"start\" command: the file is opened with whatever\n\
10534application (if any) its extension is associated.\n\
10535When another \"operation\" is given, it specifies what should be done with\n\
10536the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +000010537\n\
10538startfile returns as soon as the associated application is launched.\n\
10539There is no option to wait for the application to close, and no way\n\
10540to retrieve the application's exit status.\n\
10541\n\
10542The filepath is relative to the current directory. If you want to use\n\
10543an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000010544the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +000010545
Steve Dower7d0e0c92015-01-24 08:18:24 -080010546/* Grab ShellExecute dynamically from shell32 */
10547static int has_ShellExecute = -1;
10548static HINSTANCE (CALLBACK *Py_ShellExecuteA)(HWND, LPCSTR, LPCSTR, LPCSTR,
10549 LPCSTR, INT);
10550static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
10551 LPCWSTR, INT);
10552static int
10553check_ShellExecute()
10554{
10555 HINSTANCE hShell32;
10556
10557 /* only recheck */
10558 if (-1 == has_ShellExecute) {
10559 Py_BEGIN_ALLOW_THREADS
10560 hShell32 = LoadLibraryW(L"SHELL32");
10561 Py_END_ALLOW_THREADS
10562 if (hShell32) {
10563 *(FARPROC*)&Py_ShellExecuteA = GetProcAddress(hShell32,
10564 "ShellExecuteA");
10565 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
10566 "ShellExecuteW");
10567 has_ShellExecute = Py_ShellExecuteA &&
10568 Py_ShellExecuteW;
10569 } else {
10570 has_ShellExecute = 0;
10571 }
10572 }
10573 return has_ShellExecute;
10574}
10575
10576
Tim Petersf58a7aa2000-09-22 10:05:54 +000010577static PyObject *
10578win32_startfile(PyObject *self, PyObject *args)
10579{
Victor Stinner8c62be82010-05-06 00:08:46 +000010580 PyObject *ofilepath;
10581 char *filepath;
10582 char *operation = NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +020010583 wchar_t *wpath, *woperation;
Victor Stinner8c62be82010-05-06 00:08:46 +000010584 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000010585
Victor Stinnereb5657a2011-09-30 01:44:27 +020010586 PyObject *unipath, *uoperation = NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080010587
10588 if(!check_ShellExecute()) {
10589 /* If the OS doesn't have ShellExecute, return a
10590 NotImplementedError. */
10591 return PyErr_Format(PyExc_NotImplementedError,
10592 "startfile not available on this platform");
10593 }
10594
Victor Stinner8c62be82010-05-06 00:08:46 +000010595 if (!PyArg_ParseTuple(args, "U|s:startfile",
10596 &unipath, &operation)) {
10597 PyErr_Clear();
10598 goto normal;
10599 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +000010600
Victor Stinner8c62be82010-05-06 00:08:46 +000010601 if (operation) {
Victor Stinnereb5657a2011-09-30 01:44:27 +020010602 uoperation = PyUnicode_DecodeASCII(operation,
Victor Stinner8c62be82010-05-06 00:08:46 +000010603 strlen(operation), NULL);
Victor Stinnereb5657a2011-09-30 01:44:27 +020010604 if (!uoperation) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010605 PyErr_Clear();
10606 operation = NULL;
10607 goto normal;
10608 }
10609 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +000010610
Victor Stinnereb5657a2011-09-30 01:44:27 +020010611 wpath = PyUnicode_AsUnicode(unipath);
10612 if (wpath == NULL)
10613 goto normal;
10614 if (uoperation) {
10615 woperation = PyUnicode_AsUnicode(uoperation);
10616 if (woperation == NULL)
10617 goto normal;
10618 }
10619 else
10620 woperation = NULL;
10621
Victor Stinner8c62be82010-05-06 00:08:46 +000010622 Py_BEGIN_ALLOW_THREADS
Steve Dower7d0e0c92015-01-24 08:18:24 -080010623 rc = Py_ShellExecuteW((HWND)0, woperation, wpath,
10624 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000010625 Py_END_ALLOW_THREADS
10626
Victor Stinnereb5657a2011-09-30 01:44:27 +020010627 Py_XDECREF(uoperation);
Victor Stinner8c62be82010-05-06 00:08:46 +000010628 if (rc <= (HINSTANCE)32) {
Victor Stinnereb5657a2011-09-30 01:44:27 +020010629 win32_error_object("startfile", unipath);
10630 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000010631 }
10632 Py_INCREF(Py_None);
10633 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010634
10635normal:
Victor Stinner8c62be82010-05-06 00:08:46 +000010636 if (!PyArg_ParseTuple(args, "O&|s:startfile",
10637 PyUnicode_FSConverter, &ofilepath,
10638 &operation))
10639 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +010010640 if (win32_warn_bytes_api()) {
10641 Py_DECREF(ofilepath);
10642 return NULL;
10643 }
Victor Stinner8c62be82010-05-06 00:08:46 +000010644 filepath = PyBytes_AsString(ofilepath);
10645 Py_BEGIN_ALLOW_THREADS
Steve Dower7d0e0c92015-01-24 08:18:24 -080010646 rc = Py_ShellExecuteA((HWND)0, operation, filepath,
10647 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000010648 Py_END_ALLOW_THREADS
10649 if (rc <= (HINSTANCE)32) {
10650 PyObject *errval = win32_error("startfile", filepath);
10651 Py_DECREF(ofilepath);
10652 return errval;
10653 }
10654 Py_DECREF(ofilepath);
10655 Py_INCREF(Py_None);
10656 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +000010657}
Larry Hastings2f936352014-08-05 14:04:04 +100010658#endif /* MS_WINDOWS */
10659
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010660
Martin v. Löwis438b5342002-12-27 10:16:42 +000010661#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100010662/*[clinic input]
10663os.getloadavg
10664
10665Return average recent system load information.
10666
10667Return the number of processes in the system run queue averaged over
10668the last 1, 5, and 15 minutes as a tuple of three floats.
10669Raises OSError if the load average was unobtainable.
10670[clinic start generated code]*/
10671
Larry Hastings2f936352014-08-05 14:04:04 +100010672static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010673os_getloadavg_impl(PyObject *module)
10674/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000010675{
10676 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000010677 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000010678 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
10679 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000010680 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000010681 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000010682}
Larry Hastings2f936352014-08-05 14:04:04 +100010683#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000010684
Larry Hastings2f936352014-08-05 14:04:04 +100010685
10686/*[clinic input]
10687os.device_encoding
10688 fd: int
10689
10690Return a string describing the encoding of a terminal's file descriptor.
10691
10692The file descriptor must be attached to a terminal.
10693If the device is not a terminal, return None.
10694[clinic start generated code]*/
10695
Larry Hastings2f936352014-08-05 14:04:04 +100010696static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010697os_device_encoding_impl(PyObject *module, int fd)
10698/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010699{
Brett Cannonefb00c02012-02-29 18:31:31 -050010700 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000010701}
10702
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010703
Larry Hastings2f936352014-08-05 14:04:04 +100010704#ifdef HAVE_SETRESUID
10705/*[clinic input]
10706os.setresuid
10707
10708 ruid: uid_t
10709 euid: uid_t
10710 suid: uid_t
10711 /
10712
10713Set the current process's real, effective, and saved user ids.
10714[clinic start generated code]*/
10715
Larry Hastings2f936352014-08-05 14:04:04 +100010716static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010717os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
10718/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010719{
Victor Stinner8c62be82010-05-06 00:08:46 +000010720 if (setresuid(ruid, euid, suid) < 0)
10721 return posix_error();
10722 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010723}
Larry Hastings2f936352014-08-05 14:04:04 +100010724#endif /* HAVE_SETRESUID */
10725
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010726
10727#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100010728/*[clinic input]
10729os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010730
Larry Hastings2f936352014-08-05 14:04:04 +100010731 rgid: gid_t
10732 egid: gid_t
10733 sgid: gid_t
10734 /
10735
10736Set the current process's real, effective, and saved group ids.
10737[clinic start generated code]*/
10738
Larry Hastings2f936352014-08-05 14:04:04 +100010739static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010740os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
10741/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010742{
Victor Stinner8c62be82010-05-06 00:08:46 +000010743 if (setresgid(rgid, egid, sgid) < 0)
10744 return posix_error();
10745 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010746}
Larry Hastings2f936352014-08-05 14:04:04 +100010747#endif /* HAVE_SETRESGID */
10748
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010749
10750#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100010751/*[clinic input]
10752os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010753
Larry Hastings2f936352014-08-05 14:04:04 +100010754Return a tuple of the current process's real, effective, and saved user ids.
10755[clinic start generated code]*/
10756
Larry Hastings2f936352014-08-05 14:04:04 +100010757static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010758os_getresuid_impl(PyObject *module)
10759/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010760{
Victor Stinner8c62be82010-05-06 00:08:46 +000010761 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000010762 if (getresuid(&ruid, &euid, &suid) < 0)
10763 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020010764 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
10765 _PyLong_FromUid(euid),
10766 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010767}
Larry Hastings2f936352014-08-05 14:04:04 +100010768#endif /* HAVE_GETRESUID */
10769
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010770
10771#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100010772/*[clinic input]
10773os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010774
Larry Hastings2f936352014-08-05 14:04:04 +100010775Return a tuple of the current process's real, effective, and saved group ids.
10776[clinic start generated code]*/
10777
Larry Hastings2f936352014-08-05 14:04:04 +100010778static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010779os_getresgid_impl(PyObject *module)
10780/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010781{
10782 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000010783 if (getresgid(&rgid, &egid, &sgid) < 0)
10784 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020010785 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
10786 _PyLong_FromGid(egid),
10787 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010788}
Larry Hastings2f936352014-08-05 14:04:04 +100010789#endif /* HAVE_GETRESGID */
10790
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010791
Benjamin Peterson9428d532011-09-14 11:45:52 -040010792#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100010793/*[clinic input]
10794os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040010795
Larry Hastings2f936352014-08-05 14:04:04 +100010796 path: path_t(allow_fd=True)
10797 attribute: path_t
10798 *
10799 follow_symlinks: bool = True
10800
10801Return the value of extended attribute attribute on path.
10802
10803path may be either a string or an open file descriptor.
10804If follow_symlinks is False, and the last element of the path is a symbolic
10805 link, getxattr will examine the symbolic link itself instead of the file
10806 the link points to.
10807
10808[clinic start generated code]*/
10809
Larry Hastings2f936352014-08-05 14:04:04 +100010810static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010811os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040010812 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010813/*[clinic end generated code: output=5f2f44200a43cff2 input=8c8ea3bab78d89c2]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010814{
10815 Py_ssize_t i;
10816 PyObject *buffer = NULL;
10817
10818 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
10819 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010820
Larry Hastings9cf065c2012-06-22 16:30:09 -070010821 for (i = 0; ; i++) {
10822 void *ptr;
10823 ssize_t result;
10824 static Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
10825 Py_ssize_t buffer_size = buffer_sizes[i];
10826 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100010827 path_error(path);
10828 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010829 }
10830 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
10831 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100010832 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010833 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010834
Larry Hastings9cf065c2012-06-22 16:30:09 -070010835 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100010836 if (path->fd >= 0)
10837 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010838 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100010839 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010840 else
Larry Hastings2f936352014-08-05 14:04:04 +100010841 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010842 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010843
Larry Hastings9cf065c2012-06-22 16:30:09 -070010844 if (result < 0) {
10845 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010846 if (errno == ERANGE)
10847 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100010848 path_error(path);
10849 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010850 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010851
Larry Hastings9cf065c2012-06-22 16:30:09 -070010852 if (result != buffer_size) {
10853 /* Can only shrink. */
10854 _PyBytes_Resize(&buffer, result);
10855 }
10856 break;
10857 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010858
Larry Hastings9cf065c2012-06-22 16:30:09 -070010859 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010860}
10861
Larry Hastings2f936352014-08-05 14:04:04 +100010862
10863/*[clinic input]
10864os.setxattr
10865
10866 path: path_t(allow_fd=True)
10867 attribute: path_t
10868 value: Py_buffer
10869 flags: int = 0
10870 *
10871 follow_symlinks: bool = True
10872
10873Set extended attribute attribute on path to value.
10874
10875path may be either a string or an open file descriptor.
10876If follow_symlinks is False, and the last element of the path is a symbolic
10877 link, setxattr will modify the symbolic link itself instead of the file
10878 the link points to.
10879
10880[clinic start generated code]*/
10881
Benjamin Peterson799bd802011-08-31 22:15:17 -040010882static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010883os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040010884 Py_buffer *value, int flags, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010885/*[clinic end generated code: output=98b83f63fdde26bb input=f0d26833992015c2]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040010886{
Larry Hastings2f936352014-08-05 14:04:04 +100010887 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010888
Larry Hastings2f936352014-08-05 14:04:04 +100010889 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040010890 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010891
Benjamin Peterson799bd802011-08-31 22:15:17 -040010892 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100010893 if (path->fd > -1)
10894 result = fsetxattr(path->fd, attribute->narrow,
10895 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010896 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100010897 result = setxattr(path->narrow, attribute->narrow,
10898 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010899 else
Larry Hastings2f936352014-08-05 14:04:04 +100010900 result = lsetxattr(path->narrow, attribute->narrow,
10901 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010902 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010903
Larry Hastings9cf065c2012-06-22 16:30:09 -070010904 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100010905 path_error(path);
10906 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010907 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010908
Larry Hastings2f936352014-08-05 14:04:04 +100010909 Py_RETURN_NONE;
10910}
10911
10912
10913/*[clinic input]
10914os.removexattr
10915
10916 path: path_t(allow_fd=True)
10917 attribute: path_t
10918 *
10919 follow_symlinks: bool = True
10920
10921Remove extended attribute attribute on path.
10922
10923path may be either a string or an open file descriptor.
10924If follow_symlinks is False, and the last element of the path is a symbolic
10925 link, removexattr will modify the symbolic link itself instead of the file
10926 the link points to.
10927
10928[clinic start generated code]*/
10929
Larry Hastings2f936352014-08-05 14:04:04 +100010930static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010931os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040010932 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010933/*[clinic end generated code: output=521a51817980cda6 input=cdb54834161e3329]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010934{
10935 ssize_t result;
10936
10937 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
10938 return NULL;
10939
10940 Py_BEGIN_ALLOW_THREADS;
10941 if (path->fd > -1)
10942 result = fremovexattr(path->fd, attribute->narrow);
10943 else if (follow_symlinks)
10944 result = removexattr(path->narrow, attribute->narrow);
10945 else
10946 result = lremovexattr(path->narrow, attribute->narrow);
10947 Py_END_ALLOW_THREADS;
10948
10949 if (result) {
10950 return path_error(path);
10951 }
10952
10953 Py_RETURN_NONE;
10954}
10955
10956
10957/*[clinic input]
10958os.listxattr
10959
10960 path: path_t(allow_fd=True, nullable=True) = None
10961 *
10962 follow_symlinks: bool = True
10963
10964Return a list of extended attributes on path.
10965
10966path may be either None, a string, or an open file descriptor.
10967if path is None, listxattr will examine the current directory.
10968If follow_symlinks is False, and the last element of the path is a symbolic
10969 link, listxattr will examine the symbolic link itself instead of the file
10970 the link points to.
10971[clinic start generated code]*/
10972
Larry Hastings2f936352014-08-05 14:04:04 +100010973static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010974os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
10975/*[clinic end generated code: output=bebdb4e2ad0ce435 input=08cca53ac0b07c13]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010976{
Larry Hastings9cf065c2012-06-22 16:30:09 -070010977 Py_ssize_t i;
10978 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100010979 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010980 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010981
Larry Hastings2f936352014-08-05 14:04:04 +100010982 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070010983 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010984
Larry Hastings2f936352014-08-05 14:04:04 +100010985 name = path->narrow ? path->narrow : ".";
10986
Larry Hastings9cf065c2012-06-22 16:30:09 -070010987 for (i = 0; ; i++) {
10988 char *start, *trace, *end;
10989 ssize_t length;
10990 static Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
10991 Py_ssize_t buffer_size = buffer_sizes[i];
10992 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020010993 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100010994 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010995 break;
10996 }
10997 buffer = PyMem_MALLOC(buffer_size);
10998 if (!buffer) {
10999 PyErr_NoMemory();
11000 break;
11001 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011002
Larry Hastings9cf065c2012-06-22 16:30:09 -070011003 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011004 if (path->fd > -1)
11005 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011006 else if (follow_symlinks)
11007 length = listxattr(name, buffer, buffer_size);
11008 else
11009 length = llistxattr(name, buffer, buffer_size);
11010 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011011
Larry Hastings9cf065c2012-06-22 16:30:09 -070011012 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020011013 if (errno == ERANGE) {
11014 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050011015 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011016 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020011017 }
Larry Hastings2f936352014-08-05 14:04:04 +100011018 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011019 break;
11020 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011021
Larry Hastings9cf065c2012-06-22 16:30:09 -070011022 result = PyList_New(0);
11023 if (!result) {
11024 goto exit;
11025 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011026
Larry Hastings9cf065c2012-06-22 16:30:09 -070011027 end = buffer + length;
11028 for (trace = start = buffer; trace != end; trace++) {
11029 if (!*trace) {
11030 int error;
11031 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
11032 trace - start);
11033 if (!attribute) {
11034 Py_DECREF(result);
11035 result = NULL;
11036 goto exit;
11037 }
11038 error = PyList_Append(result, attribute);
11039 Py_DECREF(attribute);
11040 if (error) {
11041 Py_DECREF(result);
11042 result = NULL;
11043 goto exit;
11044 }
11045 start = trace + 1;
11046 }
11047 }
11048 break;
11049 }
11050exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070011051 if (buffer)
11052 PyMem_FREE(buffer);
11053 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011054}
Benjamin Peterson9428d532011-09-14 11:45:52 -040011055#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040011056
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011057
Larry Hastings2f936352014-08-05 14:04:04 +100011058/*[clinic input]
11059os.urandom
11060
11061 size: Py_ssize_t
11062 /
11063
11064Return a bytes object containing random bytes suitable for cryptographic use.
11065[clinic start generated code]*/
11066
Larry Hastings2f936352014-08-05 14:04:04 +100011067static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011068os_urandom_impl(PyObject *module, Py_ssize_t size)
11069/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011070{
11071 PyObject *bytes;
11072 int result;
11073
Georg Brandl2fb477c2012-02-21 00:33:36 +010011074 if (size < 0)
11075 return PyErr_Format(PyExc_ValueError,
11076 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100011077 bytes = PyBytes_FromStringAndSize(NULL, size);
11078 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010011079 return NULL;
11080
Larry Hastings2f936352014-08-05 14:04:04 +100011081 result = _PyOS_URandom(PyBytes_AS_STRING(bytes),
11082 PyBytes_GET_SIZE(bytes));
11083 if (result == -1) {
11084 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010011085 return NULL;
11086 }
Larry Hastings2f936352014-08-05 14:04:04 +100011087 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010011088}
11089
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011090/* Terminal size querying */
11091
11092static PyTypeObject TerminalSizeType;
11093
11094PyDoc_STRVAR(TerminalSize_docstring,
11095 "A tuple of (columns, lines) for holding terminal window size");
11096
11097static PyStructSequence_Field TerminalSize_fields[] = {
11098 {"columns", "width of the terminal window in characters"},
11099 {"lines", "height of the terminal window in characters"},
11100 {NULL, NULL}
11101};
11102
11103static PyStructSequence_Desc TerminalSize_desc = {
11104 "os.terminal_size",
11105 TerminalSize_docstring,
11106 TerminalSize_fields,
11107 2,
11108};
11109
11110#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Larry Hastings2f936352014-08-05 14:04:04 +100011111/* AC 3.5: fd should accept None */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011112PyDoc_STRVAR(termsize__doc__,
11113 "Return the size of the terminal window as (columns, lines).\n" \
11114 "\n" \
11115 "The optional argument fd (default standard output) specifies\n" \
11116 "which file descriptor should be queried.\n" \
11117 "\n" \
11118 "If the file descriptor is not connected to a terminal, an OSError\n" \
11119 "is thrown.\n" \
11120 "\n" \
11121 "This function will only be defined if an implementation is\n" \
11122 "available for this system.\n" \
11123 "\n" \
11124 "shutil.get_terminal_size is the high-level function which should \n" \
11125 "normally be used, os.get_terminal_size is the low-level implementation.");
11126
11127static PyObject*
11128get_terminal_size(PyObject *self, PyObject *args)
11129{
11130 int columns, lines;
11131 PyObject *termsize;
11132
11133 int fd = fileno(stdout);
11134 /* Under some conditions stdout may not be connected and
11135 * fileno(stdout) may point to an invalid file descriptor. For example
11136 * GUI apps don't have valid standard streams by default.
11137 *
11138 * If this happens, and the optional fd argument is not present,
11139 * the ioctl below will fail returning EBADF. This is what we want.
11140 */
11141
11142 if (!PyArg_ParseTuple(args, "|i", &fd))
11143 return NULL;
11144
11145#ifdef TERMSIZE_USE_IOCTL
11146 {
11147 struct winsize w;
11148 if (ioctl(fd, TIOCGWINSZ, &w))
11149 return PyErr_SetFromErrno(PyExc_OSError);
11150 columns = w.ws_col;
11151 lines = w.ws_row;
11152 }
11153#endif /* TERMSIZE_USE_IOCTL */
11154
11155#ifdef TERMSIZE_USE_CONIO
11156 {
11157 DWORD nhandle;
11158 HANDLE handle;
11159 CONSOLE_SCREEN_BUFFER_INFO csbi;
11160 switch (fd) {
11161 case 0: nhandle = STD_INPUT_HANDLE;
11162 break;
11163 case 1: nhandle = STD_OUTPUT_HANDLE;
11164 break;
11165 case 2: nhandle = STD_ERROR_HANDLE;
11166 break;
11167 default:
11168 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
11169 }
11170 handle = GetStdHandle(nhandle);
11171 if (handle == NULL)
11172 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
11173 if (handle == INVALID_HANDLE_VALUE)
11174 return PyErr_SetFromWindowsErr(0);
11175
11176 if (!GetConsoleScreenBufferInfo(handle, &csbi))
11177 return PyErr_SetFromWindowsErr(0);
11178
11179 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
11180 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
11181 }
11182#endif /* TERMSIZE_USE_CONIO */
11183
11184 termsize = PyStructSequence_New(&TerminalSizeType);
11185 if (termsize == NULL)
11186 return NULL;
11187 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
11188 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
11189 if (PyErr_Occurred()) {
11190 Py_DECREF(termsize);
11191 return NULL;
11192 }
11193 return termsize;
11194}
11195#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
11196
Larry Hastings2f936352014-08-05 14:04:04 +100011197
11198/*[clinic input]
11199os.cpu_count
11200
11201Return the number of CPUs in the system; return None if indeterminable.
11202[clinic start generated code]*/
11203
Larry Hastings2f936352014-08-05 14:04:04 +100011204static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011205os_cpu_count_impl(PyObject *module)
11206/*[clinic end generated code: output=5fc29463c3936a9c input=d55e2f8f3823a628]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011207{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020011208 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011209#ifdef MS_WINDOWS
11210 SYSTEM_INFO sysinfo;
11211 GetSystemInfo(&sysinfo);
11212 ncpu = sysinfo.dwNumberOfProcessors;
11213#elif defined(__hpux)
11214 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
11215#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
11216 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011217#elif defined(__DragonFly__) || \
11218 defined(__OpenBSD__) || \
11219 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020011220 defined(__NetBSD__) || \
11221 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020011222 int mib[2];
11223 size_t len = sizeof(ncpu);
11224 mib[0] = CTL_HW;
11225 mib[1] = HW_NCPU;
11226 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
11227 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011228#endif
11229 if (ncpu >= 1)
11230 return PyLong_FromLong(ncpu);
11231 else
11232 Py_RETURN_NONE;
11233}
11234
Victor Stinnerdaf45552013-08-28 00:53:59 +020011235
Larry Hastings2f936352014-08-05 14:04:04 +100011236/*[clinic input]
11237os.get_inheritable -> bool
11238
11239 fd: int
11240 /
11241
11242Get the close-on-exe flag of the specified file descriptor.
11243[clinic start generated code]*/
11244
Larry Hastings2f936352014-08-05 14:04:04 +100011245static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011246os_get_inheritable_impl(PyObject *module, int fd)
11247/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011248{
Steve Dower8fc89802015-04-12 00:26:27 -040011249 int return_value;
11250 if (!_PyVerify_fd(fd)) {
Larry Hastings2f936352014-08-05 14:04:04 +100011251 posix_error();
11252 return -1;
11253 }
11254
Steve Dower8fc89802015-04-12 00:26:27 -040011255 _Py_BEGIN_SUPPRESS_IPH
11256 return_value = _Py_get_inheritable(fd);
11257 _Py_END_SUPPRESS_IPH
11258 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100011259}
11260
11261
11262/*[clinic input]
11263os.set_inheritable
11264 fd: int
11265 inheritable: int
11266 /
11267
11268Set the inheritable flag of the specified file descriptor.
11269[clinic start generated code]*/
11270
Larry Hastings2f936352014-08-05 14:04:04 +100011271static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011272os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
11273/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020011274{
Steve Dower8fc89802015-04-12 00:26:27 -040011275 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011276 if (!_PyVerify_fd(fd))
11277 return posix_error();
11278
Steve Dower8fc89802015-04-12 00:26:27 -040011279 _Py_BEGIN_SUPPRESS_IPH
11280 result = _Py_set_inheritable(fd, inheritable, NULL);
11281 _Py_END_SUPPRESS_IPH
11282 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020011283 return NULL;
11284 Py_RETURN_NONE;
11285}
11286
11287
11288#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100011289/*[clinic input]
11290os.get_handle_inheritable -> bool
11291 handle: Py_intptr_t
11292 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020011293
Larry Hastings2f936352014-08-05 14:04:04 +100011294Get the close-on-exe flag of the specified file descriptor.
11295[clinic start generated code]*/
11296
Larry Hastings2f936352014-08-05 14:04:04 +100011297static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011298os_get_handle_inheritable_impl(PyObject *module, Py_intptr_t handle)
11299/*[clinic end generated code: output=9e5389b0aa0916ce input=5f7759443aae3dc5]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011300{
11301 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011302
11303 if (!GetHandleInformation((HANDLE)handle, &flags)) {
11304 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100011305 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011306 }
11307
Larry Hastings2f936352014-08-05 14:04:04 +100011308 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011309}
11310
Victor Stinnerdaf45552013-08-28 00:53:59 +020011311
Larry Hastings2f936352014-08-05 14:04:04 +100011312/*[clinic input]
11313os.set_handle_inheritable
11314 handle: Py_intptr_t
11315 inheritable: bool
11316 /
11317
11318Set the inheritable flag of the specified handle.
11319[clinic start generated code]*/
11320
Larry Hastings2f936352014-08-05 14:04:04 +100011321static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011322os_set_handle_inheritable_impl(PyObject *module, Py_intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040011323 int inheritable)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011324/*[clinic end generated code: output=b1e67bfa3213d745 input=e64b2b2730469def]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011325{
11326 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011327 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
11328 PyErr_SetFromWindowsErr(0);
11329 return NULL;
11330 }
11331 Py_RETURN_NONE;
11332}
Larry Hastings2f936352014-08-05 14:04:04 +100011333#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011334
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011335#ifndef MS_WINDOWS
11336PyDoc_STRVAR(get_blocking__doc__,
11337 "get_blocking(fd) -> bool\n" \
11338 "\n" \
11339 "Get the blocking mode of the file descriptor:\n" \
11340 "False if the O_NONBLOCK flag is set, True if the flag is cleared.");
11341
11342static PyObject*
11343posix_get_blocking(PyObject *self, PyObject *args)
11344{
11345 int fd;
11346 int blocking;
11347
11348 if (!PyArg_ParseTuple(args, "i:get_blocking", &fd))
11349 return NULL;
11350
11351 if (!_PyVerify_fd(fd))
11352 return posix_error();
11353
Steve Dower8fc89802015-04-12 00:26:27 -040011354 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011355 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040011356 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011357 if (blocking < 0)
11358 return NULL;
11359 return PyBool_FromLong(blocking);
11360}
11361
11362PyDoc_STRVAR(set_blocking__doc__,
11363 "set_blocking(fd, blocking)\n" \
11364 "\n" \
11365 "Set the blocking mode of the specified file descriptor.\n" \
11366 "Set the O_NONBLOCK flag if blocking is False,\n" \
11367 "clear the O_NONBLOCK flag otherwise.");
11368
11369static PyObject*
11370posix_set_blocking(PyObject *self, PyObject *args)
11371{
Steve Dower8fc89802015-04-12 00:26:27 -040011372 int fd, blocking, result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011373
11374 if (!PyArg_ParseTuple(args, "ii:set_blocking", &fd, &blocking))
11375 return NULL;
11376
11377 if (!_PyVerify_fd(fd))
11378 return posix_error();
11379
Steve Dower8fc89802015-04-12 00:26:27 -040011380 _Py_BEGIN_SUPPRESS_IPH
11381 result = _Py_set_blocking(fd, blocking);
11382 _Py_END_SUPPRESS_IPH
11383 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011384 return NULL;
11385 Py_RETURN_NONE;
11386}
11387#endif /* !MS_WINDOWS */
11388
11389
Victor Stinner6036e442015-03-08 01:58:04 +010011390PyDoc_STRVAR(posix_scandir__doc__,
11391"scandir(path='.') -> iterator of DirEntry objects for given path");
11392
11393static char *follow_symlinks_keywords[] = {"follow_symlinks", NULL};
11394
11395typedef struct {
11396 PyObject_HEAD
11397 PyObject *name;
11398 PyObject *path;
11399 PyObject *stat;
11400 PyObject *lstat;
11401#ifdef MS_WINDOWS
11402 struct _Py_stat_struct win32_lstat;
11403 __int64 win32_file_index;
11404 int got_file_index;
11405#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010011406#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010011407 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010011408#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011409 ino_t d_ino;
11410#endif
11411} DirEntry;
11412
11413static void
11414DirEntry_dealloc(DirEntry *entry)
11415{
11416 Py_XDECREF(entry->name);
11417 Py_XDECREF(entry->path);
11418 Py_XDECREF(entry->stat);
11419 Py_XDECREF(entry->lstat);
11420 Py_TYPE(entry)->tp_free((PyObject *)entry);
11421}
11422
11423/* Forward reference */
11424static int
11425DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
11426
11427/* Set exception and return -1 on error, 0 for False, 1 for True */
11428static int
11429DirEntry_is_symlink(DirEntry *self)
11430{
11431#ifdef MS_WINDOWS
11432 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010011433#elif defined(HAVE_DIRENT_D_TYPE)
11434 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010011435 if (self->d_type != DT_UNKNOWN)
11436 return self->d_type == DT_LNK;
11437 else
11438 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010011439#else
11440 /* POSIX without d_type */
11441 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010011442#endif
11443}
11444
11445static PyObject *
11446DirEntry_py_is_symlink(DirEntry *self)
11447{
11448 int result;
11449
11450 result = DirEntry_is_symlink(self);
11451 if (result == -1)
11452 return NULL;
11453 return PyBool_FromLong(result);
11454}
11455
11456static PyObject *
11457DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
11458{
11459 int result;
11460 struct _Py_stat_struct st;
11461
11462#ifdef MS_WINDOWS
11463 wchar_t *path;
11464
11465 path = PyUnicode_AsUnicode(self->path);
11466 if (!path)
11467 return NULL;
11468
11469 if (follow_symlinks)
11470 result = win32_stat_w(path, &st);
11471 else
11472 result = win32_lstat_w(path, &st);
11473
11474 if (result != 0) {
11475 return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError,
11476 0, self->path);
11477 }
11478#else /* POSIX */
11479 PyObject *bytes;
11480 char *path;
11481
11482 if (!PyUnicode_FSConverter(self->path, &bytes))
11483 return NULL;
11484 path = PyBytes_AS_STRING(bytes);
11485
11486 if (follow_symlinks)
11487 result = STAT(path, &st);
11488 else
11489 result = LSTAT(path, &st);
11490 Py_DECREF(bytes);
11491
11492 if (result != 0)
11493 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, self->path);
11494#endif
11495
11496 return _pystat_fromstructstat(&st);
11497}
11498
11499static PyObject *
11500DirEntry_get_lstat(DirEntry *self)
11501{
11502 if (!self->lstat) {
11503#ifdef MS_WINDOWS
11504 self->lstat = _pystat_fromstructstat(&self->win32_lstat);
11505#else /* POSIX */
11506 self->lstat = DirEntry_fetch_stat(self, 0);
11507#endif
11508 }
11509 Py_XINCREF(self->lstat);
11510 return self->lstat;
11511}
11512
11513static PyObject *
11514DirEntry_get_stat(DirEntry *self, int follow_symlinks)
11515{
11516 if (!follow_symlinks)
11517 return DirEntry_get_lstat(self);
11518
11519 if (!self->stat) {
11520 int result = DirEntry_is_symlink(self);
11521 if (result == -1)
11522 return NULL;
11523 else if (result)
11524 self->stat = DirEntry_fetch_stat(self, 1);
11525 else
11526 self->stat = DirEntry_get_lstat(self);
11527 }
11528
11529 Py_XINCREF(self->stat);
11530 return self->stat;
11531}
11532
11533static PyObject *
11534DirEntry_stat(DirEntry *self, PyObject *args, PyObject *kwargs)
11535{
11536 int follow_symlinks = 1;
11537
11538 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.stat",
11539 follow_symlinks_keywords, &follow_symlinks))
11540 return NULL;
11541
11542 return DirEntry_get_stat(self, follow_symlinks);
11543}
11544
11545/* Set exception and return -1 on error, 0 for False, 1 for True */
11546static int
11547DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
11548{
11549 PyObject *stat = NULL;
11550 PyObject *st_mode = NULL;
11551 long mode;
11552 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010011553#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011554 int is_symlink;
11555 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010011556#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011557#ifdef MS_WINDOWS
11558 unsigned long dir_bits;
11559#endif
Victor Stinner35a97c02015-03-08 02:59:09 +010011560 _Py_IDENTIFIER(st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010011561
11562#ifdef MS_WINDOWS
11563 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
11564 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010011565#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011566 is_symlink = self->d_type == DT_LNK;
11567 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
11568#endif
11569
Victor Stinner35a97c02015-03-08 02:59:09 +010011570#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011571 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010011572#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011573 stat = DirEntry_get_stat(self, follow_symlinks);
11574 if (!stat) {
11575 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
11576 /* If file doesn't exist (anymore), then return False
11577 (i.e., say it's not a file/directory) */
11578 PyErr_Clear();
11579 return 0;
11580 }
11581 goto error;
11582 }
11583 st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode);
11584 if (!st_mode)
11585 goto error;
11586
11587 mode = PyLong_AsLong(st_mode);
11588 if (mode == -1 && PyErr_Occurred())
11589 goto error;
11590 Py_CLEAR(st_mode);
11591 Py_CLEAR(stat);
11592 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010011593#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011594 }
11595 else if (is_symlink) {
11596 assert(mode_bits != S_IFLNK);
11597 result = 0;
11598 }
11599 else {
11600 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
11601#ifdef MS_WINDOWS
11602 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
11603 if (mode_bits == S_IFDIR)
11604 result = dir_bits != 0;
11605 else
11606 result = dir_bits == 0;
11607#else /* POSIX */
11608 if (mode_bits == S_IFDIR)
11609 result = self->d_type == DT_DIR;
11610 else
11611 result = self->d_type == DT_REG;
11612#endif
11613 }
Victor Stinner35a97c02015-03-08 02:59:09 +010011614#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011615
11616 return result;
11617
11618error:
11619 Py_XDECREF(st_mode);
11620 Py_XDECREF(stat);
11621 return -1;
11622}
11623
11624static PyObject *
11625DirEntry_py_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
11626{
11627 int result;
11628
11629 result = DirEntry_test_mode(self, follow_symlinks, mode_bits);
11630 if (result == -1)
11631 return NULL;
11632 return PyBool_FromLong(result);
11633}
11634
11635static PyObject *
11636DirEntry_is_dir(DirEntry *self, PyObject *args, PyObject *kwargs)
11637{
11638 int follow_symlinks = 1;
11639
11640 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_dir",
11641 follow_symlinks_keywords, &follow_symlinks))
11642 return NULL;
11643
11644 return DirEntry_py_test_mode(self, follow_symlinks, S_IFDIR);
11645}
11646
11647static PyObject *
11648DirEntry_is_file(DirEntry *self, PyObject *args, PyObject *kwargs)
11649{
11650 int follow_symlinks = 1;
11651
11652 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_file",
11653 follow_symlinks_keywords, &follow_symlinks))
11654 return NULL;
11655
11656 return DirEntry_py_test_mode(self, follow_symlinks, S_IFREG);
11657}
11658
11659static PyObject *
11660DirEntry_inode(DirEntry *self)
11661{
11662#ifdef MS_WINDOWS
11663 if (!self->got_file_index) {
11664 wchar_t *path;
11665 struct _Py_stat_struct stat;
11666
11667 path = PyUnicode_AsUnicode(self->path);
11668 if (!path)
11669 return NULL;
11670
11671 if (win32_lstat_w(path, &stat) != 0) {
11672 return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError,
11673 0, self->path);
11674 }
11675
11676 self->win32_file_index = stat.st_ino;
11677 self->got_file_index = 1;
11678 }
11679 return PyLong_FromLongLong((PY_LONG_LONG)self->win32_file_index);
11680#else /* POSIX */
11681#ifdef HAVE_LARGEFILE_SUPPORT
11682 return PyLong_FromLongLong((PY_LONG_LONG)self->d_ino);
11683#else
11684 return PyLong_FromLong((long)self->d_ino);
11685#endif
11686#endif
11687}
11688
11689static PyObject *
11690DirEntry_repr(DirEntry *self)
11691{
11692 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
11693}
11694
11695static PyMemberDef DirEntry_members[] = {
11696 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
11697 "the entry's base filename, relative to scandir() \"path\" argument"},
11698 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
11699 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
11700 {NULL}
11701};
11702
11703static PyMethodDef DirEntry_methods[] = {
11704 {"is_dir", (PyCFunction)DirEntry_is_dir, METH_VARARGS | METH_KEYWORDS,
11705 "return True if the entry is a directory; cached per entry"
11706 },
11707 {"is_file", (PyCFunction)DirEntry_is_file, METH_VARARGS | METH_KEYWORDS,
11708 "return True if the entry is a file; cached per entry"
11709 },
11710 {"is_symlink", (PyCFunction)DirEntry_py_is_symlink, METH_NOARGS,
11711 "return True if the entry is a symbolic link; cached per entry"
11712 },
11713 {"stat", (PyCFunction)DirEntry_stat, METH_VARARGS | METH_KEYWORDS,
11714 "return stat_result object for the entry; cached per entry"
11715 },
11716 {"inode", (PyCFunction)DirEntry_inode, METH_NOARGS,
11717 "return inode of the entry; cached per entry",
11718 },
11719 {NULL}
11720};
11721
Benjamin Peterson5646de42015-04-12 17:56:34 -040011722static PyTypeObject DirEntryType = {
Victor Stinner6036e442015-03-08 01:58:04 +010011723 PyVarObject_HEAD_INIT(NULL, 0)
11724 MODNAME ".DirEntry", /* tp_name */
11725 sizeof(DirEntry), /* tp_basicsize */
11726 0, /* tp_itemsize */
11727 /* methods */
11728 (destructor)DirEntry_dealloc, /* tp_dealloc */
11729 0, /* tp_print */
11730 0, /* tp_getattr */
11731 0, /* tp_setattr */
11732 0, /* tp_compare */
11733 (reprfunc)DirEntry_repr, /* tp_repr */
11734 0, /* tp_as_number */
11735 0, /* tp_as_sequence */
11736 0, /* tp_as_mapping */
11737 0, /* tp_hash */
11738 0, /* tp_call */
11739 0, /* tp_str */
11740 0, /* tp_getattro */
11741 0, /* tp_setattro */
11742 0, /* tp_as_buffer */
11743 Py_TPFLAGS_DEFAULT, /* tp_flags */
11744 0, /* tp_doc */
11745 0, /* tp_traverse */
11746 0, /* tp_clear */
11747 0, /* tp_richcompare */
11748 0, /* tp_weaklistoffset */
11749 0, /* tp_iter */
11750 0, /* tp_iternext */
11751 DirEntry_methods, /* tp_methods */
11752 DirEntry_members, /* tp_members */
11753};
11754
11755#ifdef MS_WINDOWS
11756
11757static wchar_t *
11758join_path_filenameW(wchar_t *path_wide, wchar_t* filename)
11759{
11760 Py_ssize_t path_len;
11761 Py_ssize_t size;
11762 wchar_t *result;
11763 wchar_t ch;
11764
11765 if (!path_wide) { /* Default arg: "." */
11766 path_wide = L".";
11767 path_len = 1;
11768 }
11769 else {
11770 path_len = wcslen(path_wide);
11771 }
11772
11773 /* The +1's are for the path separator and the NUL */
11774 size = path_len + 1 + wcslen(filename) + 1;
11775 result = PyMem_New(wchar_t, size);
11776 if (!result) {
11777 PyErr_NoMemory();
11778 return NULL;
11779 }
11780 wcscpy(result, path_wide);
11781 if (path_len > 0) {
11782 ch = result[path_len - 1];
11783 if (ch != SEP && ch != ALTSEP && ch != L':')
11784 result[path_len++] = SEP;
11785 wcscpy(result + path_len, filename);
11786 }
11787 return result;
11788}
11789
11790static PyObject *
11791DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
11792{
11793 DirEntry *entry;
11794 BY_HANDLE_FILE_INFORMATION file_info;
11795 ULONG reparse_tag;
11796 wchar_t *joined_path;
11797
11798 entry = PyObject_New(DirEntry, &DirEntryType);
11799 if (!entry)
11800 return NULL;
11801 entry->name = NULL;
11802 entry->path = NULL;
11803 entry->stat = NULL;
11804 entry->lstat = NULL;
11805 entry->got_file_index = 0;
11806
11807 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
11808 if (!entry->name)
11809 goto error;
11810
11811 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
11812 if (!joined_path)
11813 goto error;
11814
11815 entry->path = PyUnicode_FromWideChar(joined_path, -1);
11816 PyMem_Free(joined_path);
11817 if (!entry->path)
11818 goto error;
11819
11820 find_data_to_file_info_w(dataW, &file_info, &reparse_tag);
11821 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
11822
11823 return (PyObject *)entry;
11824
11825error:
11826 Py_DECREF(entry);
11827 return NULL;
11828}
11829
11830#else /* POSIX */
11831
11832static char *
11833join_path_filename(char *path_narrow, char* filename, Py_ssize_t filename_len)
11834{
11835 Py_ssize_t path_len;
11836 Py_ssize_t size;
11837 char *result;
11838
11839 if (!path_narrow) { /* Default arg: "." */
11840 path_narrow = ".";
11841 path_len = 1;
11842 }
11843 else {
11844 path_len = strlen(path_narrow);
11845 }
11846
11847 if (filename_len == -1)
11848 filename_len = strlen(filename);
11849
11850 /* The +1's are for the path separator and the NUL */
11851 size = path_len + 1 + filename_len + 1;
11852 result = PyMem_New(char, size);
11853 if (!result) {
11854 PyErr_NoMemory();
11855 return NULL;
11856 }
11857 strcpy(result, path_narrow);
11858 if (path_len > 0 && result[path_len - 1] != '/')
11859 result[path_len++] = '/';
11860 strcpy(result + path_len, filename);
11861 return result;
11862}
11863
11864static PyObject *
11865DirEntry_from_posix_info(path_t *path, char *name, Py_ssize_t name_len,
Victor Stinner35a97c02015-03-08 02:59:09 +010011866 ino_t d_ino
11867#ifdef HAVE_DIRENT_D_TYPE
11868 , unsigned char d_type
11869#endif
11870 )
Victor Stinner6036e442015-03-08 01:58:04 +010011871{
11872 DirEntry *entry;
11873 char *joined_path;
11874
11875 entry = PyObject_New(DirEntry, &DirEntryType);
11876 if (!entry)
11877 return NULL;
11878 entry->name = NULL;
11879 entry->path = NULL;
11880 entry->stat = NULL;
11881 entry->lstat = NULL;
11882
11883 joined_path = join_path_filename(path->narrow, name, name_len);
11884 if (!joined_path)
11885 goto error;
11886
11887 if (!path->narrow || !PyBytes_Check(path->object)) {
11888 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
11889 entry->path = PyUnicode_DecodeFSDefault(joined_path);
11890 }
11891 else {
11892 entry->name = PyBytes_FromStringAndSize(name, name_len);
11893 entry->path = PyBytes_FromString(joined_path);
11894 }
11895 PyMem_Free(joined_path);
11896 if (!entry->name || !entry->path)
11897 goto error;
11898
Victor Stinner35a97c02015-03-08 02:59:09 +010011899#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010011900 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010011901#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011902 entry->d_ino = d_ino;
11903
11904 return (PyObject *)entry;
11905
11906error:
11907 Py_XDECREF(entry);
11908 return NULL;
11909}
11910
11911#endif
11912
11913
11914typedef struct {
11915 PyObject_HEAD
11916 path_t path;
11917#ifdef MS_WINDOWS
11918 HANDLE handle;
11919 WIN32_FIND_DATAW file_data;
11920 int first_time;
11921#else /* POSIX */
11922 DIR *dirp;
11923#endif
11924} ScandirIterator;
11925
11926#ifdef MS_WINDOWS
11927
11928static void
11929ScandirIterator_close(ScandirIterator *iterator)
11930{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030011931 HANDLE handle = iterator->handle;
11932
11933 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010011934 return;
11935
Victor Stinner6036e442015-03-08 01:58:04 +010011936 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030011937 Py_BEGIN_ALLOW_THREADS
11938 FindClose(handle);
11939 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010011940}
11941
11942static PyObject *
11943ScandirIterator_iternext(ScandirIterator *iterator)
11944{
11945 WIN32_FIND_DATAW *file_data = &iterator->file_data;
11946 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011947 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010011948
11949 /* Happens if the iterator is iterated twice */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011950 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010011951 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010011952
11953 while (1) {
11954 if (!iterator->first_time) {
11955 Py_BEGIN_ALLOW_THREADS
11956 success = FindNextFileW(iterator->handle, file_data);
11957 Py_END_ALLOW_THREADS
11958 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011959 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010011960 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011961 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010011962 break;
11963 }
11964 }
11965 iterator->first_time = 0;
11966
11967 /* Skip over . and .. */
11968 if (wcscmp(file_data->cFileName, L".") != 0 &&
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011969 wcscmp(file_data->cFileName, L"..") != 0) {
11970 entry = DirEntry_from_find_data(&iterator->path, file_data);
11971 if (!entry)
11972 break;
11973 return entry;
11974 }
Victor Stinner6036e442015-03-08 01:58:04 +010011975
11976 /* Loop till we get a non-dot directory or finish iterating */
11977 }
11978
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011979 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010011980 ScandirIterator_close(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010011981 return NULL;
11982}
11983
11984#else /* POSIX */
11985
11986static void
11987ScandirIterator_close(ScandirIterator *iterator)
11988{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030011989 DIR *dirp = iterator->dirp;
11990
11991 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010011992 return;
11993
Victor Stinner6036e442015-03-08 01:58:04 +010011994 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030011995 Py_BEGIN_ALLOW_THREADS
11996 closedir(dirp);
11997 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010011998 return;
11999}
12000
12001static PyObject *
12002ScandirIterator_iternext(ScandirIterator *iterator)
12003{
12004 struct dirent *direntp;
12005 Py_ssize_t name_len;
12006 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012007 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012008
12009 /* Happens if the iterator is iterated twice */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012010 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010012011 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012012
12013 while (1) {
12014 errno = 0;
12015 Py_BEGIN_ALLOW_THREADS
12016 direntp = readdir(iterator->dirp);
12017 Py_END_ALLOW_THREADS
12018
12019 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012020 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010012021 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012022 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012023 break;
12024 }
12025
12026 /* Skip over . and .. */
12027 name_len = NAMLEN(direntp);
12028 is_dot = direntp->d_name[0] == '.' &&
12029 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
12030 if (!is_dot) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012031 entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name,
Victor Stinner35a97c02015-03-08 02:59:09 +010012032 name_len, direntp->d_ino
12033#ifdef HAVE_DIRENT_D_TYPE
12034 , direntp->d_type
12035#endif
12036 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012037 if (!entry)
12038 break;
12039 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012040 }
12041
12042 /* Loop till we get a non-dot directory or finish iterating */
12043 }
12044
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012045 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010012046 ScandirIterator_close(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010012047 return NULL;
12048}
12049
12050#endif
12051
12052static void
12053ScandirIterator_dealloc(ScandirIterator *iterator)
12054{
12055 ScandirIterator_close(iterator);
12056 Py_XDECREF(iterator->path.object);
12057 path_cleanup(&iterator->path);
12058 Py_TYPE(iterator)->tp_free((PyObject *)iterator);
12059}
12060
Benjamin Peterson5646de42015-04-12 17:56:34 -040012061static PyTypeObject ScandirIteratorType = {
Victor Stinner6036e442015-03-08 01:58:04 +010012062 PyVarObject_HEAD_INIT(NULL, 0)
12063 MODNAME ".ScandirIterator", /* tp_name */
12064 sizeof(ScandirIterator), /* tp_basicsize */
12065 0, /* tp_itemsize */
12066 /* methods */
12067 (destructor)ScandirIterator_dealloc, /* tp_dealloc */
12068 0, /* tp_print */
12069 0, /* tp_getattr */
12070 0, /* tp_setattr */
12071 0, /* tp_compare */
12072 0, /* tp_repr */
12073 0, /* tp_as_number */
12074 0, /* tp_as_sequence */
12075 0, /* tp_as_mapping */
12076 0, /* tp_hash */
12077 0, /* tp_call */
12078 0, /* tp_str */
12079 0, /* tp_getattro */
12080 0, /* tp_setattro */
12081 0, /* tp_as_buffer */
12082 Py_TPFLAGS_DEFAULT, /* tp_flags */
12083 0, /* tp_doc */
12084 0, /* tp_traverse */
12085 0, /* tp_clear */
12086 0, /* tp_richcompare */
12087 0, /* tp_weaklistoffset */
12088 PyObject_SelfIter, /* tp_iter */
12089 (iternextfunc)ScandirIterator_iternext, /* tp_iternext */
12090};
12091
12092static PyObject *
12093posix_scandir(PyObject *self, PyObject *args, PyObject *kwargs)
12094{
12095 ScandirIterator *iterator;
12096 static char *keywords[] = {"path", NULL};
12097#ifdef MS_WINDOWS
12098 wchar_t *path_strW;
12099#else
12100 char *path;
12101#endif
12102
12103 iterator = PyObject_New(ScandirIterator, &ScandirIteratorType);
12104 if (!iterator)
12105 return NULL;
12106 memset(&iterator->path, 0, sizeof(path_t));
12107 iterator->path.function_name = "scandir";
12108 iterator->path.nullable = 1;
12109
12110#ifdef MS_WINDOWS
12111 iterator->handle = INVALID_HANDLE_VALUE;
12112#else
12113 iterator->dirp = NULL;
12114#endif
12115
12116 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:scandir", keywords,
12117 path_converter, &iterator->path))
12118 goto error;
12119
12120 /* path_converter doesn't keep path.object around, so do it
12121 manually for the lifetime of the iterator here (the refcount
12122 is decremented in ScandirIterator_dealloc)
12123 */
12124 Py_XINCREF(iterator->path.object);
12125
12126#ifdef MS_WINDOWS
12127 if (iterator->path.narrow) {
12128 PyErr_SetString(PyExc_TypeError,
12129 "os.scandir() doesn't support bytes path on Windows, use Unicode instead");
12130 goto error;
12131 }
12132 iterator->first_time = 1;
12133
12134 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
12135 if (!path_strW)
12136 goto error;
12137
12138 Py_BEGIN_ALLOW_THREADS
12139 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
12140 Py_END_ALLOW_THREADS
12141
12142 PyMem_Free(path_strW);
12143
12144 if (iterator->handle == INVALID_HANDLE_VALUE) {
12145 path_error(&iterator->path);
12146 goto error;
12147 }
12148#else /* POSIX */
12149 if (iterator->path.narrow)
12150 path = iterator->path.narrow;
12151 else
12152 path = ".";
12153
12154 errno = 0;
12155 Py_BEGIN_ALLOW_THREADS
12156 iterator->dirp = opendir(path);
12157 Py_END_ALLOW_THREADS
12158
12159 if (!iterator->dirp) {
12160 path_error(&iterator->path);
12161 goto error;
12162 }
12163#endif
12164
12165 return (PyObject *)iterator;
12166
12167error:
12168 Py_DECREF(iterator);
12169 return NULL;
12170}
12171
12172
Serhiy Storchakaa4c6bad2015-04-04 23:35:52 +030012173#include "clinic/posixmodule.c.h"
12174
Larry Hastings7726ac92014-01-31 22:03:12 -080012175/*[clinic input]
12176dump buffer
12177[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030012178/*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/
Larry Hastings7726ac92014-01-31 22:03:12 -080012179
Larry Hastings31826802013-10-19 00:09:25 -070012180
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012181static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070012182
12183 OS_STAT_METHODDEF
12184 OS_ACCESS_METHODDEF
12185 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100012186 OS_CHDIR_METHODDEF
12187 OS_CHFLAGS_METHODDEF
12188 OS_CHMOD_METHODDEF
12189 OS_FCHMOD_METHODDEF
12190 OS_LCHMOD_METHODDEF
12191 OS_CHOWN_METHODDEF
12192 OS_FCHOWN_METHODDEF
12193 OS_LCHOWN_METHODDEF
12194 OS_LCHFLAGS_METHODDEF
12195 OS_CHROOT_METHODDEF
12196 OS_CTERMID_METHODDEF
12197 OS_GETCWD_METHODDEF
12198 OS_GETCWDB_METHODDEF
12199 OS_LINK_METHODDEF
12200 OS_LISTDIR_METHODDEF
12201 OS_LSTAT_METHODDEF
12202 OS_MKDIR_METHODDEF
12203 OS_NICE_METHODDEF
12204 OS_GETPRIORITY_METHODDEF
12205 OS_SETPRIORITY_METHODDEF
Guido van Rossumb6775db1994-08-01 11:34:53 +000012206#ifdef HAVE_READLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -070012207 {"readlink", (PyCFunction)posix_readlink,
12208 METH_VARARGS | METH_KEYWORDS,
12209 readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000012210#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000012211#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Larry Hastings9cf065c2012-06-22 16:30:09 -070012212 {"readlink", (PyCFunction)win_readlink,
12213 METH_VARARGS | METH_KEYWORDS,
12214 readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000012215#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Larry Hastings2f936352014-08-05 14:04:04 +100012216 OS_RENAME_METHODDEF
12217 OS_REPLACE_METHODDEF
12218 OS_RMDIR_METHODDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000012219 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Larry Hastings2f936352014-08-05 14:04:04 +100012220 OS_SYMLINK_METHODDEF
12221 OS_SYSTEM_METHODDEF
12222 OS_UMASK_METHODDEF
12223 OS_UNAME_METHODDEF
12224 OS_UNLINK_METHODDEF
12225 OS_REMOVE_METHODDEF
12226 OS_UTIME_METHODDEF
12227 OS_TIMES_METHODDEF
12228 OS__EXIT_METHODDEF
12229 OS_EXECV_METHODDEF
12230 OS_EXECVE_METHODDEF
12231 OS_SPAWNV_METHODDEF
12232 OS_SPAWNVE_METHODDEF
12233 OS_FORK1_METHODDEF
12234 OS_FORK_METHODDEF
12235 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
12236 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
12237 OS_SCHED_GETPARAM_METHODDEF
12238 OS_SCHED_GETSCHEDULER_METHODDEF
12239 OS_SCHED_RR_GET_INTERVAL_METHODDEF
12240 OS_SCHED_SETPARAM_METHODDEF
12241 OS_SCHED_SETSCHEDULER_METHODDEF
12242 OS_SCHED_YIELD_METHODDEF
12243 OS_SCHED_SETAFFINITY_METHODDEF
12244 OS_SCHED_GETAFFINITY_METHODDEF
12245 OS_OPENPTY_METHODDEF
12246 OS_FORKPTY_METHODDEF
12247 OS_GETEGID_METHODDEF
12248 OS_GETEUID_METHODDEF
12249 OS_GETGID_METHODDEF
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020012250#ifdef HAVE_GETGROUPLIST
12251 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
12252#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012253 OS_GETGROUPS_METHODDEF
12254 OS_GETPID_METHODDEF
12255 OS_GETPGRP_METHODDEF
12256 OS_GETPPID_METHODDEF
12257 OS_GETUID_METHODDEF
12258 OS_GETLOGIN_METHODDEF
12259 OS_KILL_METHODDEF
12260 OS_KILLPG_METHODDEF
12261 OS_PLOCK_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000012262#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000012263 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +000012264#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012265 OS_SETUID_METHODDEF
12266 OS_SETEUID_METHODDEF
12267 OS_SETREUID_METHODDEF
12268 OS_SETGID_METHODDEF
12269 OS_SETEGID_METHODDEF
12270 OS_SETREGID_METHODDEF
12271 OS_SETGROUPS_METHODDEF
Antoine Pitroub7572f02009-12-02 20:46:48 +000012272#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000012273 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000012274#endif /* HAVE_INITGROUPS */
Larry Hastings2f936352014-08-05 14:04:04 +100012275 OS_GETPGID_METHODDEF
12276 OS_SETPGRP_METHODDEF
12277 OS_WAIT_METHODDEF
12278 OS_WAIT3_METHODDEF
12279 OS_WAIT4_METHODDEF
12280 OS_WAITID_METHODDEF
12281 OS_WAITPID_METHODDEF
12282 OS_GETSID_METHODDEF
12283 OS_SETSID_METHODDEF
12284 OS_SETPGID_METHODDEF
12285 OS_TCGETPGRP_METHODDEF
12286 OS_TCSETPGRP_METHODDEF
12287 OS_OPEN_METHODDEF
12288 OS_CLOSE_METHODDEF
12289 OS_CLOSERANGE_METHODDEF
12290 OS_DEVICE_ENCODING_METHODDEF
12291 OS_DUP_METHODDEF
12292 OS_DUP2_METHODDEF
12293 OS_LOCKF_METHODDEF
12294 OS_LSEEK_METHODDEF
12295 OS_READ_METHODDEF
12296 OS_READV_METHODDEF
12297 OS_PREAD_METHODDEF
12298 OS_WRITE_METHODDEF
12299 OS_WRITEV_METHODDEF
12300 OS_PWRITE_METHODDEF
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012301#ifdef HAVE_SENDFILE
12302 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
12303 posix_sendfile__doc__},
12304#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012305 OS_FSTAT_METHODDEF
12306 OS_ISATTY_METHODDEF
12307 OS_PIPE_METHODDEF
12308 OS_PIPE2_METHODDEF
12309 OS_MKFIFO_METHODDEF
12310 OS_MKNOD_METHODDEF
12311 OS_MAJOR_METHODDEF
12312 OS_MINOR_METHODDEF
12313 OS_MAKEDEV_METHODDEF
12314 OS_FTRUNCATE_METHODDEF
12315 OS_TRUNCATE_METHODDEF
12316 OS_POSIX_FALLOCATE_METHODDEF
12317 OS_POSIX_FADVISE_METHODDEF
12318 OS_PUTENV_METHODDEF
12319 OS_UNSETENV_METHODDEF
12320 OS_STRERROR_METHODDEF
12321 OS_FCHDIR_METHODDEF
12322 OS_FSYNC_METHODDEF
12323 OS_SYNC_METHODDEF
12324 OS_FDATASYNC_METHODDEF
12325 OS_WCOREDUMP_METHODDEF
12326 OS_WIFCONTINUED_METHODDEF
12327 OS_WIFSTOPPED_METHODDEF
12328 OS_WIFSIGNALED_METHODDEF
12329 OS_WIFEXITED_METHODDEF
12330 OS_WEXITSTATUS_METHODDEF
12331 OS_WTERMSIG_METHODDEF
12332 OS_WSTOPSIG_METHODDEF
12333 OS_FSTATVFS_METHODDEF
12334 OS_STATVFS_METHODDEF
12335 OS_CONFSTR_METHODDEF
12336 OS_SYSCONF_METHODDEF
12337 OS_FPATHCONF_METHODDEF
12338 OS_PATHCONF_METHODDEF
12339 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030012340 OS__GETFULLPATHNAME_METHODDEF
12341 OS__ISDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100012342 OS__GETDISKUSAGE_METHODDEF
12343 OS__GETFINALPATHNAME_METHODDEF
12344 OS__GETVOLUMEPATHNAME_METHODDEF
12345 OS_GETLOADAVG_METHODDEF
12346 OS_URANDOM_METHODDEF
12347 OS_SETRESUID_METHODDEF
12348 OS_SETRESGID_METHODDEF
12349 OS_GETRESUID_METHODDEF
12350 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012351
Larry Hastings2f936352014-08-05 14:04:04 +100012352 OS_GETXATTR_METHODDEF
12353 OS_SETXATTR_METHODDEF
12354 OS_REMOVEXATTR_METHODDEF
12355 OS_LISTXATTR_METHODDEF
12356
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012357#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
12358 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
12359#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012360 OS_CPU_COUNT_METHODDEF
12361 OS_GET_INHERITABLE_METHODDEF
12362 OS_SET_INHERITABLE_METHODDEF
12363 OS_GET_HANDLE_INHERITABLE_METHODDEF
12364 OS_SET_HANDLE_INHERITABLE_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012365#ifndef MS_WINDOWS
12366 {"get_blocking", posix_get_blocking, METH_VARARGS, get_blocking__doc__},
12367 {"set_blocking", posix_set_blocking, METH_VARARGS, set_blocking__doc__},
12368#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012369 {"scandir", (PyCFunction)posix_scandir,
12370 METH_VARARGS | METH_KEYWORDS,
12371 posix_scandir__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000012372 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012373};
12374
12375
Brian Curtin52173d42010-12-02 18:29:18 +000012376#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000012377static int
Brian Curtin52173d42010-12-02 18:29:18 +000012378enable_symlink()
12379{
12380 HANDLE tok;
12381 TOKEN_PRIVILEGES tok_priv;
12382 LUID luid;
12383 int meth_idx = 0;
12384
12385 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000012386 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000012387
12388 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000012389 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000012390
12391 tok_priv.PrivilegeCount = 1;
12392 tok_priv.Privileges[0].Luid = luid;
12393 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
12394
12395 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
12396 sizeof(TOKEN_PRIVILEGES),
12397 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000012398 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000012399
Brian Curtin3b4499c2010-12-28 14:31:47 +000012400 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
12401 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000012402}
12403#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
12404
Barry Warsaw4a342091996-12-19 23:50:02 +000012405static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012406all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000012407{
Guido van Rossum94f6f721999-01-06 18:42:14 +000012408#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012409 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012410#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000012411#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012412 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012413#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000012414#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012415 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012416#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000012417#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012418 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012419#endif
Fred Drakec9680921999-12-13 16:37:25 +000012420#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012421 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000012422#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012423#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012424 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012425#endif
Fred Drake106c1a02002-04-23 15:58:02 +000012426#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012427 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000012428#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000012429#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012430 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012431#endif
Fred Drake106c1a02002-04-23 15:58:02 +000012432#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012433 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000012434#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000012435#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012436 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012437#endif
12438#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012439 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012440#endif
12441#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012442 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012443#endif
12444#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012445 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012446#endif
12447#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012448 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012449#endif
12450#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012451 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012452#endif
12453#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012454 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012455#endif
12456#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012457 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012458#endif
12459#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012460 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012461#endif
12462#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012463 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012464#endif
12465#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012466 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012467#endif
12468#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012469 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012470#endif
12471#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012472 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012473#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000012474#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012475 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000012476#endif
12477#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012478 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000012479#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020012480#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012481 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020012482#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012483#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012484 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012485#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +000012486#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012487 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000012488#endif
12489#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012490 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000012491#endif
Jesus Ceacf381202012-04-24 20:44:40 +020012492#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012493 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020012494#endif
12495#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012496 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020012497#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050012498#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012499 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050012500#endif
Jesus Ceacf381202012-04-24 20:44:40 +020012501#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012502 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020012503#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020012504#ifdef O_TMPFILE
12505 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
12506#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012507#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012508 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012509#endif
12510#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012511 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012512#endif
12513#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012514 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012515#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020012516#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012517 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020012518#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020012519#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012520 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020012521#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012522
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012523
Jesus Cea94363612012-06-22 18:32:07 +020012524#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012525 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020012526#endif
12527#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012528 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020012529#endif
12530
Tim Peters5aa91602002-01-30 05:46:57 +000012531/* MS Windows */
12532#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000012533 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012534 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012535#endif
12536#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000012537 /* Optimize for short life (keep in memory). */
12538 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012539 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012540#endif
12541#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000012542 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012543 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012544#endif
12545#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000012546 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012547 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012548#endif
12549#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000012550 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012551 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012552#endif
12553
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012554/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000012555#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000012556 /* Send a SIGIO signal whenever input or output
12557 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012558 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000012559#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012560#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000012561 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012562 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012563#endif
12564#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000012565 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012566 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012567#endif
12568#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000012569 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012570 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012571#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020012572#ifdef O_NOLINKS
12573 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012574 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020012575#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000012576#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000012577 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012578 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000012579#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000012580
Victor Stinner8c62be82010-05-06 00:08:46 +000012581 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012582#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012583 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012584#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012585#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012586 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012587#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012588#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012589 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012590#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012591#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012592 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012593#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012594#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012595 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012596#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012597#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012598 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012599#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012600#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012601 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012602#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012603#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012604 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012605#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012606#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012607 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012608#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012609#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012610 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012611#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012612#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012613 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012614#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012615#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012616 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012617#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012618#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012619 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012620#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012621#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012622 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012623#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012624#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012625 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012626#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012627#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012628 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012629#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012630#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012631 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012632#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012633
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000012634 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000012635#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012636 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000012637#endif /* ST_RDONLY */
12638#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012639 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000012640#endif /* ST_NOSUID */
12641
doko@ubuntu.comca616a22013-12-08 15:23:07 +010012642 /* GNU extensions */
12643#ifdef ST_NODEV
12644 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
12645#endif /* ST_NODEV */
12646#ifdef ST_NOEXEC
12647 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
12648#endif /* ST_NOEXEC */
12649#ifdef ST_SYNCHRONOUS
12650 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
12651#endif /* ST_SYNCHRONOUS */
12652#ifdef ST_MANDLOCK
12653 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
12654#endif /* ST_MANDLOCK */
12655#ifdef ST_WRITE
12656 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
12657#endif /* ST_WRITE */
12658#ifdef ST_APPEND
12659 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
12660#endif /* ST_APPEND */
12661#ifdef ST_NOATIME
12662 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
12663#endif /* ST_NOATIME */
12664#ifdef ST_NODIRATIME
12665 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
12666#endif /* ST_NODIRATIME */
12667#ifdef ST_RELATIME
12668 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
12669#endif /* ST_RELATIME */
12670
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012671 /* FreeBSD sendfile() constants */
12672#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012673 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012674#endif
12675#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012676 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012677#endif
12678#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012679 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012680#endif
12681
Ross Lagerwall7807c352011-03-17 20:20:30 +020012682 /* constants for posix_fadvise */
12683#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012684 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012685#endif
12686#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012687 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012688#endif
12689#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012690 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012691#endif
12692#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012693 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012694#endif
12695#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012696 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012697#endif
12698#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012699 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012700#endif
12701
12702 /* constants for waitid */
12703#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012704 if (PyModule_AddIntMacro(m, P_PID)) return -1;
12705 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
12706 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012707#endif
12708#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012709 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012710#endif
12711#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012712 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012713#endif
12714#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012715 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012716#endif
12717#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012718 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012719#endif
12720#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012721 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012722#endif
12723#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012724 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012725#endif
12726#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012727 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012728#endif
12729
12730 /* constants for lockf */
12731#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012732 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012733#endif
12734#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012735 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012736#endif
12737#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012738 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012739#endif
12740#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012741 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012742#endif
12743
Guido van Rossum246bc171999-02-01 23:54:31 +000012744#ifdef HAVE_SPAWNV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012745 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
12746 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
12747 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
12748 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
12749 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000012750#endif
12751
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012752#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070012753#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012754 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070012755#endif
12756#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012757 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070012758#endif
12759#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012760 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070012761#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012762#ifdef SCHED_SPORADIC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012763 if (PyModule_AddIntMacro(m, SCHED_SPORADIC) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012764#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012765#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012766 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012767#endif
12768#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012769 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012770#endif
12771#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012772 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012773#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020012774#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012775 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020012776#endif
12777#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012778 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020012779#endif
12780#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012781 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020012782#endif
12783#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012784 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020012785#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012786#endif
12787
Benjamin Peterson9428d532011-09-14 11:45:52 -040012788#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012789 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
12790 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
12791 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012792#endif
12793
Victor Stinner8b905bd2011-10-25 13:34:04 +020012794#ifdef RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012795 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020012796#endif
12797#ifdef RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012798 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020012799#endif
12800#ifdef RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012801 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020012802#endif
12803#ifdef RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012804 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020012805#endif
12806#ifdef RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012807 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020012808#endif
12809#ifdef RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012810 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020012811#endif
12812#ifdef RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012813 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020012814#endif
12815
Victor Stinner8c62be82010-05-06 00:08:46 +000012816 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000012817}
12818
12819
Martin v. Löwis1a214512008-06-11 05:26:20 +000012820static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000012821 PyModuleDef_HEAD_INIT,
12822 MODNAME,
12823 posix__doc__,
12824 -1,
12825 posix_methods,
12826 NULL,
12827 NULL,
12828 NULL,
12829 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000012830};
12831
12832
Larry Hastings9cf065c2012-06-22 16:30:09 -070012833static char *have_functions[] = {
12834
12835#ifdef HAVE_FACCESSAT
12836 "HAVE_FACCESSAT",
12837#endif
12838
12839#ifdef HAVE_FCHDIR
12840 "HAVE_FCHDIR",
12841#endif
12842
12843#ifdef HAVE_FCHMOD
12844 "HAVE_FCHMOD",
12845#endif
12846
12847#ifdef HAVE_FCHMODAT
12848 "HAVE_FCHMODAT",
12849#endif
12850
12851#ifdef HAVE_FCHOWN
12852 "HAVE_FCHOWN",
12853#endif
12854
Larry Hastings00964ed2013-08-12 13:49:30 -040012855#ifdef HAVE_FCHOWNAT
12856 "HAVE_FCHOWNAT",
12857#endif
12858
Larry Hastings9cf065c2012-06-22 16:30:09 -070012859#ifdef HAVE_FEXECVE
12860 "HAVE_FEXECVE",
12861#endif
12862
12863#ifdef HAVE_FDOPENDIR
12864 "HAVE_FDOPENDIR",
12865#endif
12866
Georg Brandl306336b2012-06-24 12:55:33 +020012867#ifdef HAVE_FPATHCONF
12868 "HAVE_FPATHCONF",
12869#endif
12870
Larry Hastings9cf065c2012-06-22 16:30:09 -070012871#ifdef HAVE_FSTATAT
12872 "HAVE_FSTATAT",
12873#endif
12874
12875#ifdef HAVE_FSTATVFS
12876 "HAVE_FSTATVFS",
12877#endif
12878
Steve Dowerfe0a41a2015-03-20 19:50:46 -070012879#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Georg Brandl306336b2012-06-24 12:55:33 +020012880 "HAVE_FTRUNCATE",
12881#endif
12882
Larry Hastings9cf065c2012-06-22 16:30:09 -070012883#ifdef HAVE_FUTIMENS
12884 "HAVE_FUTIMENS",
12885#endif
12886
12887#ifdef HAVE_FUTIMES
12888 "HAVE_FUTIMES",
12889#endif
12890
12891#ifdef HAVE_FUTIMESAT
12892 "HAVE_FUTIMESAT",
12893#endif
12894
12895#ifdef HAVE_LINKAT
12896 "HAVE_LINKAT",
12897#endif
12898
12899#ifdef HAVE_LCHFLAGS
12900 "HAVE_LCHFLAGS",
12901#endif
12902
12903#ifdef HAVE_LCHMOD
12904 "HAVE_LCHMOD",
12905#endif
12906
12907#ifdef HAVE_LCHOWN
12908 "HAVE_LCHOWN",
12909#endif
12910
12911#ifdef HAVE_LSTAT
12912 "HAVE_LSTAT",
12913#endif
12914
12915#ifdef HAVE_LUTIMES
12916 "HAVE_LUTIMES",
12917#endif
12918
12919#ifdef HAVE_MKDIRAT
12920 "HAVE_MKDIRAT",
12921#endif
12922
12923#ifdef HAVE_MKFIFOAT
12924 "HAVE_MKFIFOAT",
12925#endif
12926
12927#ifdef HAVE_MKNODAT
12928 "HAVE_MKNODAT",
12929#endif
12930
12931#ifdef HAVE_OPENAT
12932 "HAVE_OPENAT",
12933#endif
12934
12935#ifdef HAVE_READLINKAT
12936 "HAVE_READLINKAT",
12937#endif
12938
12939#ifdef HAVE_RENAMEAT
12940 "HAVE_RENAMEAT",
12941#endif
12942
12943#ifdef HAVE_SYMLINKAT
12944 "HAVE_SYMLINKAT",
12945#endif
12946
12947#ifdef HAVE_UNLINKAT
12948 "HAVE_UNLINKAT",
12949#endif
12950
12951#ifdef HAVE_UTIMENSAT
12952 "HAVE_UTIMENSAT",
12953#endif
12954
12955#ifdef MS_WINDOWS
12956 "MS_WINDOWS",
12957#endif
12958
12959 NULL
12960};
12961
12962
Mark Hammondfe51c6d2002-08-02 02:27:13 +000012963PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000012964INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000012965{
Victor Stinner8c62be82010-05-06 00:08:46 +000012966 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012967 PyObject *list;
12968 char **trace;
Tim Peters5aa91602002-01-30 05:46:57 +000012969
Brian Curtin52173d42010-12-02 18:29:18 +000012970#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000012971 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000012972#endif
12973
Victor Stinner8c62be82010-05-06 00:08:46 +000012974 m = PyModule_Create(&posixmodule);
12975 if (m == NULL)
12976 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000012977
Victor Stinner8c62be82010-05-06 00:08:46 +000012978 /* Initialize environ dictionary */
12979 v = convertenviron();
12980 Py_XINCREF(v);
12981 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
12982 return NULL;
12983 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000012984
Victor Stinner8c62be82010-05-06 00:08:46 +000012985 if (all_ins(m))
12986 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000012987
Victor Stinner8c62be82010-05-06 00:08:46 +000012988 if (setup_confname_tables(m))
12989 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000012990
Victor Stinner8c62be82010-05-06 00:08:46 +000012991 Py_INCREF(PyExc_OSError);
12992 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000012993
Guido van Rossumb3d39562000-01-31 18:41:26 +000012994#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000012995 if (posix_putenv_garbage == NULL)
12996 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000012997#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000012998
Victor Stinner8c62be82010-05-06 00:08:46 +000012999 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020013000#if defined(HAVE_WAITID) && !defined(__APPLE__)
13001 waitid_result_desc.name = MODNAME ".waitid_result";
Victor Stinner1c8f0592013-07-22 22:24:54 +020013002 if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0)
13003 return NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013004#endif
13005
Christian Heimes25827622013-10-12 01:27:08 +020013006 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
Victor Stinner8c62be82010-05-06 00:08:46 +000013007 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
13008 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
13009 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Victor Stinner1c8f0592013-07-22 22:24:54 +020013010 if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0)
13011 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000013012 structseq_new = StatResultType.tp_new;
13013 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000013014
Christian Heimes25827622013-10-12 01:27:08 +020013015 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
Victor Stinner1c8f0592013-07-22 22:24:54 +020013016 if (PyStructSequence_InitType2(&StatVFSResultType,
13017 &statvfs_result_desc) < 0)
13018 return NULL;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000013019#ifdef NEED_TICKS_PER_SECOND
13020# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000013021 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000013022# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000013023 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000013024# else
Victor Stinner8c62be82010-05-06 00:08:46 +000013025 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000013026# endif
13027#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013028
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050013029#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013030 sched_param_desc.name = MODNAME ".sched_param";
Victor Stinner1c8f0592013-07-22 22:24:54 +020013031 if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0)
13032 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100013033 SchedParamType.tp_new = os_sched_param;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013034#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013035
13036 /* initialize TerminalSize_info */
Victor Stinner1c8f0592013-07-22 22:24:54 +020013037 if (PyStructSequence_InitType2(&TerminalSizeType,
13038 &TerminalSize_desc) < 0)
13039 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013040
13041 /* initialize scandir types */
13042 if (PyType_Ready(&ScandirIteratorType) < 0)
13043 return NULL;
13044 if (PyType_Ready(&DirEntryType) < 0)
13045 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000013046 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020013047#if defined(HAVE_WAITID) && !defined(__APPLE__)
13048 Py_INCREF((PyObject*) &WaitidResultType);
13049 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
13050#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000013051 Py_INCREF((PyObject*) &StatResultType);
13052 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
13053 Py_INCREF((PyObject*) &StatVFSResultType);
13054 PyModule_AddObject(m, "statvfs_result",
13055 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050013056
13057#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013058 Py_INCREF(&SchedParamType);
13059 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050013060#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000013061
Larry Hastings605a62d2012-06-24 04:33:36 -070013062 times_result_desc.name = MODNAME ".times_result";
Victor Stinner1c8f0592013-07-22 22:24:54 +020013063 if (PyStructSequence_InitType2(&TimesResultType, &times_result_desc) < 0)
13064 return NULL;
Larry Hastings605a62d2012-06-24 04:33:36 -070013065 PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType);
13066
13067 uname_result_desc.name = MODNAME ".uname_result";
Victor Stinner1c8f0592013-07-22 22:24:54 +020013068 if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0)
13069 return NULL;
Larry Hastings605a62d2012-06-24 04:33:36 -070013070 PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType);
13071
Thomas Wouters477c8d52006-05-27 19:21:47 +000013072#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000013073 /*
13074 * Step 2 of weak-linking support on Mac OS X.
13075 *
13076 * The code below removes functions that are not available on the
13077 * currently active platform.
13078 *
13079 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070013080 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000013081 * OSX 10.4.
13082 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000013083#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000013084 if (fstatvfs == NULL) {
13085 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
13086 return NULL;
13087 }
13088 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000013089#endif /* HAVE_FSTATVFS */
13090
13091#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000013092 if (statvfs == NULL) {
13093 if (PyObject_DelAttrString(m, "statvfs") == -1) {
13094 return NULL;
13095 }
13096 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000013097#endif /* HAVE_STATVFS */
13098
13099# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000013100 if (lchown == NULL) {
13101 if (PyObject_DelAttrString(m, "lchown") == -1) {
13102 return NULL;
13103 }
13104 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000013105#endif /* HAVE_LCHOWN */
13106
13107
13108#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013109
Antoine Pitrou9d20e0e2012-09-12 18:01:36 +020013110 Py_INCREF(&TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013111 PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType);
13112
Larry Hastings6fe20b32012-04-19 15:07:49 -070013113 billion = PyLong_FromLong(1000000000);
13114 if (!billion)
13115 return NULL;
13116
Larry Hastings9cf065c2012-06-22 16:30:09 -070013117 /* suppress "function not used" warnings */
13118 {
13119 int ignored;
13120 fd_specified("", -1);
13121 follow_symlinks_specified("", 1);
13122 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
13123 dir_fd_converter(Py_None, &ignored);
13124 dir_fd_unavailable(Py_None, &ignored);
13125 }
13126
13127 /*
13128 * provide list of locally available functions
13129 * so os.py can populate support_* lists
13130 */
13131 list = PyList_New(0);
13132 if (!list)
13133 return NULL;
13134 for (trace = have_functions; *trace; trace++) {
13135 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
13136 if (!unicode)
13137 return NULL;
13138 if (PyList_Append(list, unicode))
13139 return NULL;
13140 Py_DECREF(unicode);
13141 }
13142 PyModule_AddObject(m, "_have_functions", list);
13143
13144 initialized = 1;
13145
Victor Stinner8c62be82010-05-06 00:08:46 +000013146 return m;
Guido van Rossumb6775db1994-08-01 11:34:53 +000013147}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000013148
13149#ifdef __cplusplus
13150}
13151#endif