blob: 54f6cd2b22cf9f619ff0836d07b59ddf787b80ab [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
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 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
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Thomas Wouters477c8d52006-05-27 19:21:47 +000014#ifdef __APPLE__
15 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000016 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000017 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
18 * at the end of this file for more information.
19 */
20# pragma weak lchown
21# pragma weak statvfs
22# pragma weak fstatvfs
23
24#endif /* __APPLE__ */
25
Thomas Wouters68bc4f92006-03-01 01:05:10 +000026#define PY_SSIZE_T_CLEAN
27
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000028#include "Python.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000029
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000030#if defined(__VMS)
Victor Stinnerb90db4c2011-04-26 22:48:24 +020031# error "PEP 11: VMS is now unsupported, code will be removed in Python 3.4"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000032# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#endif /* defined(__VMS) */
34
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000035#ifdef __cplusplus
36extern "C" {
37#endif
38
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000039PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000040"This module provides access to operating system functionality that is\n\
41standardized by the C Standard and the POSIX standard (a thinly\n\
42disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000043corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000045
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000046#if defined(PYOS_OS2)
Victor Stinnerb90db4c2011-04-26 22:48:24 +020047#error "PEP 11: OS/2 is now unsupported, code will be removed in Python 3.4"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000048#define INCL_DOS
49#define INCL_DOSERRORS
50#define INCL_DOSPROCESS
51#define INCL_NOPMAPI
52#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000053#if defined(PYCC_GCC)
54#include <ctype.h>
55#include <io.h>
56#include <stdio.h>
57#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000058#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000059#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000060#endif
61
Ross Lagerwall4d076da2011-03-18 06:56:53 +020062#ifdef HAVE_SYS_UIO_H
63#include <sys/uio.h>
64#endif
65
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000067#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068#endif /* HAVE_SYS_TYPES_H */
69
70#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000071#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000072#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000073
Guido van Rossum36bc6801995-06-14 22:54:23 +000074#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000075#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000076#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000077
Thomas Wouters0e3f5912006-08-11 14:57:12 +000078#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000079#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000080#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000081
Guido van Rossumb6775db1994-08-01 11:34:53 +000082#ifdef HAVE_FCNTL_H
83#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000084#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000085
Guido van Rossuma6535fd2001-10-18 19:44:10 +000086#ifdef HAVE_GRP_H
87#include <grp.h>
88#endif
89
Barry Warsaw5676bd12003-01-07 20:57:09 +000090#ifdef HAVE_SYSEXITS_H
91#include <sysexits.h>
92#endif /* HAVE_SYSEXITS_H */
93
Anthony Baxter8a560de2004-10-13 15:30:56 +000094#ifdef HAVE_SYS_LOADAVG_H
95#include <sys/loadavg.h>
96#endif
97
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000098#ifdef HAVE_LANGINFO_H
99#include <langinfo.h>
100#endif
101
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000102#ifdef HAVE_SYS_SENDFILE_H
103#include <sys/sendfile.h>
104#endif
105
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500106#ifdef HAVE_SCHED_H
107#include <sched.h>
108#endif
109
Benjamin Peterson2dbda072012-03-16 10:12:55 -0500110#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
Benjamin Peterson7b51b8d2012-03-14 22:28:25 -0500111#undef HAVE_SCHED_SETAFFINITY
112#endif
113
Benjamin Peterson9428d532011-09-14 11:45:52 -0400114#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__)
115#define USE_XATTRS
116#endif
117
118#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400119#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400120#endif
121
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000122#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
123#ifdef HAVE_SYS_SOCKET_H
124#include <sys/socket.h>
125#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000126#endif
127
Victor Stinner8b905bd2011-10-25 13:34:04 +0200128#ifdef HAVE_DLFCN_H
129#include <dlfcn.h>
130#endif
131
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100132#if defined(MS_WINDOWS)
133# define TERMSIZE_USE_CONIO
134#elif defined(HAVE_SYS_IOCTL_H)
135# include <sys/ioctl.h>
136# if defined(HAVE_TERMIOS_H)
137# include <termios.h>
138# endif
139# if defined(TIOCGWINSZ)
140# define TERMSIZE_USE_IOCTL
141# endif
142#endif /* MS_WINDOWS */
143
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000144/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000145/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000146#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000147#include <process.h>
148#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000149#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000150#define HAVE_GETCWD 1
151#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000152#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000153#if defined(__OS2__)
154#define HAVE_EXECV 1
155#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000156#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000157#include <process.h>
158#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000159#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000160#define HAVE_EXECV 1
161#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000162#define HAVE_OPENDIR 1
163#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000164#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000165#define HAVE_WAIT 1
166#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000167#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000168#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000169#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000170#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000171#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000172#define HAVE_EXECV 1
173#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000174#define HAVE_SYSTEM 1
175#define HAVE_CWAIT 1
176#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000177#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000178#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000179#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
180/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000181#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000182/* Unix functions that the configure script doesn't check for */
183#define HAVE_EXECV 1
184#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000185#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000186#define HAVE_FORK1 1
187#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000188#define HAVE_GETCWD 1
189#define HAVE_GETEGID 1
190#define HAVE_GETEUID 1
191#define HAVE_GETGID 1
192#define HAVE_GETPPID 1
193#define HAVE_GETUID 1
194#define HAVE_KILL 1
195#define HAVE_OPENDIR 1
196#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000197#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000198#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000199#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000200#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000201#endif /* _MSC_VER */
202#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000203#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000204#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000205
Victor Stinnera2f7c002012-02-08 03:36:25 +0100206
207
208
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000209#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000210
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000211#if defined(__sgi)&&_COMPILER_VERSION>=700
212/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
213 (default) */
214extern char *ctermid_r(char *);
215#endif
216
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000217#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000218#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000219extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000220#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000221#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000222extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000223#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000224extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000225#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000226#endif
227#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000228extern int chdir(char *);
229extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000230#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000231extern int chdir(const char *);
232extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000233#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000234#ifdef __BORLANDC__
235extern int chmod(const char *, int);
236#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000237extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000238#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000239/*#ifdef HAVE_FCHMOD
240extern int fchmod(int, mode_t);
241#endif*/
242/*#ifdef HAVE_LCHMOD
243extern int lchmod(const char *, mode_t);
244#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000245extern int chown(const char *, uid_t, gid_t);
246extern char *getcwd(char *, int);
247extern char *strerror(int);
248extern int link(const char *, const char *);
249extern int rename(const char *, const char *);
250extern int stat(const char *, struct stat *);
251extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000253extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000254#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000256extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000257#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000258#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000259
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000260#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261
Guido van Rossumb6775db1994-08-01 11:34:53 +0000262#ifdef HAVE_UTIME_H
263#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000264#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000265
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000266#ifdef HAVE_SYS_UTIME_H
267#include <sys/utime.h>
268#define HAVE_UTIME_H /* pretend we do for the rest of this file */
269#endif /* HAVE_SYS_UTIME_H */
270
Guido van Rossumb6775db1994-08-01 11:34:53 +0000271#ifdef HAVE_SYS_TIMES_H
272#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000273#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000274
275#ifdef HAVE_SYS_PARAM_H
276#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000277#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000278
279#ifdef HAVE_SYS_UTSNAME_H
280#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000281#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000282
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000283#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000284#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000285#define NAMLEN(dirent) strlen((dirent)->d_name)
286#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000287#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000288#include <direct.h>
289#define NAMLEN(dirent) strlen((dirent)->d_name)
290#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000291#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000292#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000293#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000294#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000295#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000296#endif
297#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000298#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000299#endif
300#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000301#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000302#endif
303#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000304
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000305#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000306#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000307#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000308#endif
309#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000310#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000311#endif
312#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000313#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000314#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000315#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000316#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000317#endif
318#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000319#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000320#endif
321#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000322#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000323#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000324#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000325#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000326#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000327#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000328#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000329#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
330#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000331static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000332#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000333#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000334
Guido van Rossumd48f2521997-12-05 22:19:34 +0000335#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000336#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000337#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000338
Tim Petersbc2e10e2002-03-03 23:17:02 +0000339#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000340#if defined(PATH_MAX) && PATH_MAX > 1024
341#define MAXPATHLEN PATH_MAX
342#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000343#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000344#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000345#endif /* MAXPATHLEN */
346
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000347#ifdef UNION_WAIT
348/* Emulate some macros on systems that have a union instead of macros */
349
350#ifndef WIFEXITED
351#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
352#endif
353
354#ifndef WEXITSTATUS
355#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
356#endif
357
358#ifndef WTERMSIG
359#define WTERMSIG(u_wait) ((u_wait).w_termsig)
360#endif
361
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000362#define WAIT_TYPE union wait
363#define WAIT_STATUS_INT(s) (s.w_status)
364
365#else /* !UNION_WAIT */
366#define WAIT_TYPE int
367#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000368#endif /* UNION_WAIT */
369
Greg Wardb48bc172000-03-01 21:51:56 +0000370/* Don't use the "_r" form if we don't need it (also, won't have a
371 prototype for it, at least on Solaris -- maybe others as well?). */
372#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
373#define USE_CTERMID_R
374#endif
375
Fred Drake699f3522000-06-29 21:12:41 +0000376/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000377#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000378#undef FSTAT
379#undef STRUCT_STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000380#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000381# define STAT win32_stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700382# define LSTAT win32_lstat
Victor Stinner8c62be82010-05-06 00:08:46 +0000383# define FSTAT win32_fstat
384# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000385#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000386# define STAT stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700387# define LSTAT lstat
Victor Stinner8c62be82010-05-06 00:08:46 +0000388# define FSTAT fstat
389# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000390#endif
391
Tim Peters11b23062003-04-23 02:39:17 +0000392#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000393#include <sys/mkdev.h>
394#else
395#if defined(MAJOR_IN_SYSMACROS)
396#include <sys/sysmacros.h>
397#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000398#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
399#include <sys/mkdev.h>
400#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000401#endif
Fred Drake699f3522000-06-29 21:12:41 +0000402
Larry Hastings9cf065c2012-06-22 16:30:09 -0700403
404#ifdef MS_WINDOWS
405static int
406win32_warn_bytes_api()
407{
408 return PyErr_WarnEx(PyExc_DeprecationWarning,
409 "The Windows bytes API has been deprecated, "
410 "use Unicode filenames instead",
411 1);
412}
413#endif
414
415
416#ifdef AT_FDCWD
417#define DEFAULT_DIR_FD AT_FDCWD
418#else
419#define DEFAULT_DIR_FD (-100)
420#endif
421
422static int
423_fd_converter(PyObject *o, int *p, int default_value) {
424 long long_value;
425 if (o == Py_None) {
426 *p = default_value;
427 return 1;
428 }
429 if (PyFloat_Check(o)) {
430 PyErr_SetString(PyExc_TypeError,
431 "integer argument expected, got float" );
432 return 0;
433 }
434 long_value = PyLong_AsLong(o);
435 if (long_value == -1 && PyErr_Occurred())
436 return 0;
437 if (long_value > INT_MAX) {
438 PyErr_SetString(PyExc_OverflowError,
439 "signed integer is greater than maximum");
440 return 0;
441 }
442 if (long_value < INT_MIN) {
443 PyErr_SetString(PyExc_OverflowError,
444 "signed integer is less than minimum");
445 return 0;
446 }
447 *p = (int)long_value;
448 return 1;
449}
450
451static int
452dir_fd_converter(PyObject *o, void *p) {
453 return _fd_converter(o, (int *)p, DEFAULT_DIR_FD);
454}
455
456
457
458/*
459 * A PyArg_ParseTuple "converter" function
460 * that handles filesystem paths in the manner
461 * preferred by the os module.
462 *
463 * path_converter accepts (Unicode) strings and their
464 * subclasses, and bytes and their subclasses. What
465 * it does with the argument depends on the platform:
466 *
467 * * On Windows, if we get a (Unicode) string we
468 * extract the wchar_t * and return it; if we get
469 * bytes we extract the char * and return that.
470 *
471 * * On all other platforms, strings are encoded
472 * to bytes using PyUnicode_FSConverter, then we
473 * extract the char * from the bytes object and
474 * return that.
475 *
476 * path_converter also optionally accepts signed
477 * integers (representing open file descriptors) instead
478 * of path strings.
479 *
480 * Input fields:
481 * path.nullable
482 * If nonzero, the path is permitted to be None.
483 * path.allow_fd
484 * If nonzero, the path is permitted to be a file handle
485 * (a signed int) instead of a string.
486 * path.function_name
487 * If non-NULL, path_converter will use that as the name
488 * of the function in error messages.
489 * (If path.argument_name is NULL it omits the function name.)
490 * path.argument_name
491 * If non-NULL, path_converter will use that as the name
492 * of the parameter in error messages.
493 * (If path.argument_name is NULL it uses "path".)
494 *
495 * Output fields:
496 * path.wide
497 * Points to the path if it was expressed as Unicode
498 * and was not encoded. (Only used on Windows.)
499 * path.narrow
500 * Points to the path if it was expressed as bytes,
501 * or it was Unicode and was encoded to bytes.
502 * path.fd
503 * Contains a file descriptor if path.accept_fd was true
504 * and the caller provided a signed integer instead of any
505 * sort of string.
506 *
507 * WARNING: if your "path" parameter is optional, and is
508 * unspecified, path_converter will never get called.
509 * So if you set allow_fd, you *MUST* initialize path.fd = -1
510 * yourself!
511 * path.length
512 * The length of the path in characters, if specified as
513 * a string.
514 * path.object
515 * The original object passed in.
516 * path.cleanup
517 * For internal use only. May point to a temporary object.
518 * (Pay no attention to the man behind the curtain.)
519 *
520 * At most one of path.wide or path.narrow will be non-NULL.
521 * If path was None and path.nullable was set,
522 * or if path was an integer and path.allow_fd was set,
523 * both path.wide and path.narrow will be NULL
524 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +0200525 *
Larry Hastings9cf065c2012-06-22 16:30:09 -0700526 * path_converter takes care to not write to the path_t
527 * unless it's successful. However it must reset the
528 * "cleanup" field each time it's called.
529 *
530 * Use as follows:
531 * path_t path;
532 * memset(&path, 0, sizeof(path));
533 * PyArg_ParseTuple(args, "O&", path_converter, &path);
534 * // ... use values from path ...
535 * path_cleanup(&path);
536 *
537 * (Note that if PyArg_Parse fails you don't need to call
538 * path_cleanup(). However it is safe to do so.)
539 */
540typedef struct {
541 char *function_name;
542 char *argument_name;
543 int nullable;
544 int allow_fd;
545 wchar_t *wide;
546 char *narrow;
547 int fd;
548 Py_ssize_t length;
549 PyObject *object;
550 PyObject *cleanup;
551} path_t;
552
553static void
554path_cleanup(path_t *path) {
555 if (path->cleanup) {
556 Py_DECREF(path->cleanup);
557 path->cleanup = NULL;
558 }
559}
560
561static int
562path_converter(PyObject *o, void *p) {
563 path_t *path = (path_t *)p;
564 PyObject *unicode, *bytes;
565 Py_ssize_t length;
566 char *narrow;
567
568#define FORMAT_EXCEPTION(exc, fmt) \
569 PyErr_Format(exc, "%s%s" fmt, \
570 path->function_name ? path->function_name : "", \
571 path->function_name ? ": " : "", \
572 path->argument_name ? path->argument_name : "path")
573
574 /* Py_CLEANUP_SUPPORTED support */
575 if (o == NULL) {
576 path_cleanup(path);
577 return 1;
578 }
579
580 /* ensure it's always safe to call path_cleanup() */
581 path->cleanup = NULL;
582
583 if (o == Py_None) {
584 if (!path->nullable) {
585 FORMAT_EXCEPTION(PyExc_TypeError,
586 "can't specify None for %s argument");
587 return 0;
588 }
589 path->wide = NULL;
590 path->narrow = NULL;
591 path->length = 0;
592 path->object = o;
593 path->fd = -1;
594 return 1;
595 }
596
597 unicode = PyUnicode_FromObject(o);
598 if (unicode) {
599#ifdef MS_WINDOWS
600 wchar_t *wide;
601 length = PyUnicode_GET_SIZE(unicode);
602 if (length > 32767) {
603 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
604 Py_DECREF(unicode);
605 return 0;
606 }
607
608 wide = PyUnicode_AsUnicode(unicode);
609 if (!wide) {
610 Py_DECREF(unicode);
611 return 0;
612 }
613
614 path->wide = wide;
615 path->narrow = NULL;
616 path->length = length;
617 path->object = o;
618 path->fd = -1;
619 path->cleanup = unicode;
620 return Py_CLEANUP_SUPPORTED;
621#else
622 int converted = PyUnicode_FSConverter(unicode, &bytes);
623 Py_DECREF(unicode);
624 if (!converted)
625 bytes = NULL;
626#endif
627 }
628 else {
629 PyErr_Clear();
630 bytes = PyBytes_FromObject(o);
631 if (!bytes) {
632 PyErr_Clear();
633 if (path->allow_fd) {
634 int fd;
635 /*
636 * note: _fd_converter always permits None.
637 * but we've already done our None check.
638 * so o cannot be None at this point.
639 */
640 int result = _fd_converter(o, &fd, -1);
641 if (result) {
642 path->wide = NULL;
643 path->narrow = NULL;
644 path->length = 0;
645 path->object = o;
646 path->fd = fd;
647 return result;
648 }
649 }
650 }
651 }
652
653 if (!bytes) {
654 if (!PyErr_Occurred())
655 FORMAT_EXCEPTION(PyExc_TypeError, "illegal type for %s parameter");
656 return 0;
657 }
658
659#ifdef MS_WINDOWS
660 if (win32_warn_bytes_api()) {
661 Py_DECREF(bytes);
662 return 0;
663 }
664#endif
665
666 length = PyBytes_GET_SIZE(bytes);
667#ifdef MS_WINDOWS
668 if (length > MAX_PATH) {
669 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
670 Py_DECREF(bytes);
671 return 0;
672 }
673#endif
674
675 narrow = PyBytes_AS_STRING(bytes);
676 if (length != strlen(narrow)) {
677 FORMAT_EXCEPTION(PyExc_ValueError, "embedded NUL character in %s");
678 Py_DECREF(bytes);
679 return 0;
680 }
681
682 path->wide = NULL;
683 path->narrow = narrow;
684 path->length = length;
685 path->object = o;
686 path->fd = -1;
687 path->cleanup = bytes;
688 return Py_CLEANUP_SUPPORTED;
689}
690
691static void
692argument_unavailable_error(char *function_name, char *argument_name) {
693 PyErr_Format(PyExc_NotImplementedError,
694 "%s%s%s unavailable on this platform",
695 (function_name != NULL) ? function_name : "",
696 (function_name != NULL) ? ": ": "",
697 argument_name);
698}
699
700static int
701dir_fd_unavailable(PyObject *o, void *p) {
702 int *dir_fd = (int *)p;
703 int return_value = _fd_converter(o, dir_fd, DEFAULT_DIR_FD);
704 if (!return_value)
705 return 0;
706 if (*dir_fd == DEFAULT_DIR_FD)
707 return 1;
708 argument_unavailable_error(NULL, "dir_fd");
709 return 0;
710}
711
712static int
713fd_specified(char *function_name, int fd) {
714 if (fd == -1)
715 return 0;
716
717 argument_unavailable_error(function_name, "fd");
718 return 1;
719}
720
721static int
722follow_symlinks_specified(char *function_name, int follow_symlinks) {
723 if (follow_symlinks)
724 return 0;
725
726 argument_unavailable_error(function_name, "follow_symlinks");
727 return 1;
728}
729
730static int
731path_and_dir_fd_invalid(char *function_name, path_t *path, int dir_fd) {
732 if (!path->narrow && !path->wide && (dir_fd != DEFAULT_DIR_FD)) {
733 PyErr_Format(PyExc_ValueError,
734 "%s: can't specify dir_fd without matching path",
735 function_name);
736 return 1;
737 }
738 return 0;
739}
740
741static int
742dir_fd_and_fd_invalid(char *function_name, int dir_fd, int fd) {
743 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
744 PyErr_Format(PyExc_ValueError,
745 "%s: can't specify both dir_fd and fd",
746 function_name);
747 return 1;
748 }
749 return 0;
750}
751
752static int
753fd_and_follow_symlinks_invalid(char *function_name, int fd,
754 int follow_symlinks) {
755 if ((fd > 0) && (!follow_symlinks)) {
756 PyErr_Format(PyExc_ValueError,
757 "%s: cannot use fd and follow_symlinks together",
758 function_name);
759 return 1;
760 }
761 return 0;
762}
763
764static int
765dir_fd_and_follow_symlinks_invalid(char *function_name, int dir_fd,
766 int follow_symlinks) {
767 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
768 PyErr_Format(PyExc_ValueError,
769 "%s: cannot use dir_fd and follow_symlinks together",
770 function_name);
771 return 1;
772 }
773 return 0;
774}
775
Ross Lagerwallb1e5d592011-09-19 08:30:43 +0200776/* A helper used by a number of POSIX-only functions */
777#ifndef MS_WINDOWS
Georg Brandla391b112011-02-25 15:23:18 +0000778static int
779_parse_off_t(PyObject* arg, void* addr)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000780{
781#if !defined(HAVE_LARGEFILE_SUPPORT)
782 *((off_t*)addr) = PyLong_AsLong(arg);
783#else
Antoine Pitroudcc20b82011-02-26 13:38:35 +0000784 *((off_t*)addr) = PyLong_AsLongLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000785#endif
786 if (PyErr_Occurred())
787 return 0;
788 return 1;
789}
Ross Lagerwallb1e5d592011-09-19 08:30:43 +0200790#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000791
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000792#if defined _MSC_VER && _MSC_VER >= 1400
793/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
794 * valid and throw an assertion if it isn't.
795 * Normally, an invalid fd is likely to be a C program error and therefore
796 * an assertion can be useful, but it does contradict the POSIX standard
797 * which for write(2) states:
798 * "Otherwise, -1 shall be returned and errno set to indicate the error."
799 * "[EBADF] The fildes argument is not a valid file descriptor open for
800 * writing."
801 * Furthermore, python allows the user to enter any old integer
802 * as a fd and should merely raise a python exception on error.
803 * The Microsoft CRT doesn't provide an official way to check for the
804 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000805 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000806 * internal structures involved.
807 * The structures below must be updated for each version of visual studio
808 * according to the file internal.h in the CRT source, until MS comes
809 * up with a less hacky way to do this.
810 * (all of this is to avoid globally modifying the CRT behaviour using
811 * _set_invalid_parameter_handler() and _CrtSetReportMode())
812 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000813/* The actual size of the structure is determined at runtime.
814 * Only the first items must be present.
815 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000816typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000817 intptr_t osfhnd;
818 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000819} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000820
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000821extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000822#define IOINFO_L2E 5
823#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
824#define IOINFO_ARRAYS 64
825#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
826#define FOPEN 0x01
827#define _NO_CONSOLE_FILENO (intptr_t)-2
828
829/* This function emulates what the windows CRT does to validate file handles */
830int
831_PyVerify_fd(int fd)
832{
Victor Stinner8c62be82010-05-06 00:08:46 +0000833 const int i1 = fd >> IOINFO_L2E;
834 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000835
Antoine Pitrou22e41552010-08-15 18:07:50 +0000836 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000837
Victor Stinner8c62be82010-05-06 00:08:46 +0000838 /* Determine the actual size of the ioinfo structure,
839 * as used by the CRT loaded in memory
840 */
841 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
842 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
843 }
844 if (sizeof_ioinfo == 0) {
845 /* This should not happen... */
846 goto fail;
847 }
848
849 /* See that it isn't a special CLEAR fileno */
850 if (fd != _NO_CONSOLE_FILENO) {
851 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
852 * we check pointer validity and other info
853 */
854 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
855 /* finally, check that the file is open */
856 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
857 if (info->osfile & FOPEN) {
858 return 1;
859 }
860 }
861 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000862 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000863 errno = EBADF;
864 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000865}
866
867/* the special case of checking dup2. The target fd must be in a sensible range */
868static int
869_PyVerify_fd_dup2(int fd1, int fd2)
870{
Victor Stinner8c62be82010-05-06 00:08:46 +0000871 if (!_PyVerify_fd(fd1))
872 return 0;
873 if (fd2 == _NO_CONSOLE_FILENO)
874 return 0;
875 if ((unsigned)fd2 < _NHANDLE_)
876 return 1;
877 else
878 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000879}
880#else
881/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
882#define _PyVerify_fd_dup2(A, B) (1)
883#endif
884
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000885#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000886/* The following structure was copied from
887 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
888 include doesn't seem to be present in the Windows SDK (at least as included
889 with Visual Studio Express). */
890typedef struct _REPARSE_DATA_BUFFER {
891 ULONG ReparseTag;
892 USHORT ReparseDataLength;
893 USHORT Reserved;
894 union {
895 struct {
896 USHORT SubstituteNameOffset;
897 USHORT SubstituteNameLength;
898 USHORT PrintNameOffset;
899 USHORT PrintNameLength;
900 ULONG Flags;
901 WCHAR PathBuffer[1];
902 } SymbolicLinkReparseBuffer;
903
904 struct {
905 USHORT SubstituteNameOffset;
906 USHORT SubstituteNameLength;
907 USHORT PrintNameOffset;
908 USHORT PrintNameLength;
909 WCHAR PathBuffer[1];
910 } MountPointReparseBuffer;
911
912 struct {
913 UCHAR DataBuffer[1];
914 } GenericReparseBuffer;
915 };
916} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
917
918#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
919 GenericReparseBuffer)
920#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
921
922static int
Brian Curtind25aef52011-06-13 15:16:04 -0500923win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000924{
925 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
926 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
927 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000928
929 if (0 == DeviceIoControl(
930 reparse_point_handle,
931 FSCTL_GET_REPARSE_POINT,
932 NULL, 0, /* in buffer */
933 target_buffer, sizeof(target_buffer),
934 &n_bytes_returned,
935 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -0500936 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000937
938 if (reparse_tag)
939 *reparse_tag = rdb->ReparseTag;
940
Brian Curtind25aef52011-06-13 15:16:04 -0500941 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000942}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100943
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000944#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000945
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000946/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000947#ifdef WITH_NEXT_FRAMEWORK
948/* On Darwin/MacOSX a shared library or framework has no access to
949** environ directly, we must obtain it with _NSGetEnviron().
950*/
951#include <crt_externs.h>
952static char **environ;
953#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000954extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000955#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000956
Barry Warsaw53699e91996-12-10 23:23:01 +0000957static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000958convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000959{
Victor Stinner8c62be82010-05-06 00:08:46 +0000960 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000961#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000962 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000963#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000964 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000965#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000966#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000967 APIRET rc;
968 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
969#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000970
Victor Stinner8c62be82010-05-06 00:08:46 +0000971 d = PyDict_New();
972 if (d == NULL)
973 return NULL;
974#ifdef WITH_NEXT_FRAMEWORK
975 if (environ == NULL)
976 environ = *_NSGetEnviron();
977#endif
978#ifdef MS_WINDOWS
979 /* _wenviron must be initialized in this way if the program is started
980 through main() instead of wmain(). */
981 _wgetenv(L"");
982 if (_wenviron == NULL)
983 return d;
984 /* This part ignores errors */
985 for (e = _wenviron; *e != NULL; e++) {
986 PyObject *k;
987 PyObject *v;
988 wchar_t *p = wcschr(*e, L'=');
989 if (p == NULL)
990 continue;
991 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
992 if (k == NULL) {
993 PyErr_Clear();
994 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000995 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000996 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
997 if (v == NULL) {
998 PyErr_Clear();
999 Py_DECREF(k);
1000 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001001 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001002 if (PyDict_GetItem(d, k) == NULL) {
1003 if (PyDict_SetItem(d, k, v) != 0)
1004 PyErr_Clear();
1005 }
1006 Py_DECREF(k);
1007 Py_DECREF(v);
1008 }
1009#else
1010 if (environ == NULL)
1011 return d;
1012 /* This part ignores errors */
1013 for (e = environ; *e != NULL; e++) {
1014 PyObject *k;
1015 PyObject *v;
1016 char *p = strchr(*e, '=');
1017 if (p == NULL)
1018 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +00001019 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +00001020 if (k == NULL) {
1021 PyErr_Clear();
1022 continue;
1023 }
Victor Stinner84ae1182010-05-06 22:05:07 +00001024 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +00001025 if (v == NULL) {
1026 PyErr_Clear();
1027 Py_DECREF(k);
1028 continue;
1029 }
1030 if (PyDict_GetItem(d, k) == NULL) {
1031 if (PyDict_SetItem(d, k, v) != 0)
1032 PyErr_Clear();
1033 }
1034 Py_DECREF(k);
1035 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001036 }
1037#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001038#if defined(PYOS_OS2)
1039 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
1040 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
1041 PyObject *v = PyBytes_FromString(buffer);
1042 PyDict_SetItemString(d, "BEGINLIBPATH", v);
1043 Py_DECREF(v);
1044 }
1045 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
1046 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
1047 PyObject *v = PyBytes_FromString(buffer);
1048 PyDict_SetItemString(d, "ENDLIBPATH", v);
1049 Py_DECREF(v);
1050 }
1051#endif
1052 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001053}
1054
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001055/* Set a POSIX-specific error from errno, and return NULL */
1056
Barry Warsawd58d7641998-07-23 16:14:40 +00001057static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001058posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001059{
Victor Stinner8c62be82010-05-06 00:08:46 +00001060 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001061}
Barry Warsawd58d7641998-07-23 16:14:40 +00001062static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001063posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +00001064{
Victor Stinner8c62be82010-05-06 00:08:46 +00001065 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +00001066}
1067
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001068
Mark Hammondef8b6542001-05-13 08:04:26 +00001069static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +00001070posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +00001071{
Victor Stinner77ccd6d2010-05-08 00:36:42 +00001072 PyObject *name_str, *rc;
1073 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
1074 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +00001075 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +00001076 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
1077 name_str);
1078 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +00001079 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +00001080}
1081
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001082#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001083static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +00001084win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001085{
Victor Stinner8c62be82010-05-06 00:08:46 +00001086 /* XXX We should pass the function name along in the future.
1087 (winreg.c also wants to pass the function name.)
1088 This would however require an additional param to the
1089 Windows error object, which is non-trivial.
1090 */
1091 errno = GetLastError();
1092 if (filename)
1093 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1094 else
1095 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001096}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001097
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001098static PyObject *
Victor Stinnereb5657a2011-09-30 01:44:27 +02001099win32_error_unicode(char* function, wchar_t* filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001100{
Victor Stinner8c62be82010-05-06 00:08:46 +00001101 /* XXX - see win32_error for comments on 'function' */
1102 errno = GetLastError();
1103 if (filename)
1104 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
1105 else
1106 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001107}
1108
Victor Stinnereb5657a2011-09-30 01:44:27 +02001109static PyObject *
1110win32_error_object(char* function, PyObject* filename)
1111{
1112 /* XXX - see win32_error for comments on 'function' */
1113 errno = GetLastError();
1114 if (filename)
1115 return PyErr_SetExcFromWindowsErrWithFilenameObject(
1116 PyExc_WindowsError,
1117 errno,
1118 filename);
1119 else
1120 return PyErr_SetFromWindowsErr(errno);
1121}
1122
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001123#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001124
Larry Hastings9cf065c2012-06-22 16:30:09 -07001125/*
1126 * Some functions return Win32 errors, others only ever use posix_error
1127 * (this is for backwards compatibility with exceptions)
1128 */
1129static PyObject *
1130path_posix_error(char *function_name, path_t *path)
1131{
1132 if (path->narrow)
1133 return posix_error_with_filename(path->narrow);
1134 return posix_error();
1135}
1136
1137static PyObject *
1138path_error(char *function_name, path_t *path)
1139{
1140#ifdef MS_WINDOWS
1141 if (path->narrow)
1142 return win32_error(function_name, path->narrow);
1143 if (path->wide)
1144 return win32_error_unicode(function_name, path->wide);
1145 return win32_error(function_name, NULL);
1146#else
1147 return path_posix_error(function_name, path);
1148#endif
1149}
1150
Guido van Rossumd48f2521997-12-05 22:19:34 +00001151#if defined(PYOS_OS2)
1152/**********************************************************************
1153 * Helper Function to Trim and Format OS/2 Messages
1154 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +00001155static void
Guido van Rossumd48f2521997-12-05 22:19:34 +00001156os2_formatmsg(char *msgbuf, int msglen, char *reason)
1157{
1158 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
1159
1160 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
1161 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
1162
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00001163 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +00001164 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
1165 }
1166
1167 /* Add Optional Reason Text */
1168 if (reason) {
1169 strcat(msgbuf, " : ");
1170 strcat(msgbuf, reason);
1171 }
1172}
1173
1174/**********************************************************************
1175 * Decode an OS/2 Operating System Error Code
1176 *
1177 * A convenience function to lookup an OS/2 error code and return a
1178 * text message we can use to raise a Python exception.
1179 *
1180 * Notes:
1181 * The messages for errors returned from the OS/2 kernel reside in
1182 * the file OSO001.MSG in the \OS2 directory hierarchy.
1183 *
1184 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +00001185static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +00001186os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
1187{
1188 APIRET rc;
1189 ULONG msglen;
1190
1191 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
1192 Py_BEGIN_ALLOW_THREADS
1193 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
1194 errorcode, "oso001.msg", &msglen);
1195 Py_END_ALLOW_THREADS
1196
1197 if (rc == NO_ERROR)
1198 os2_formatmsg(msgbuf, msglen, reason);
1199 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +00001200 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +00001201 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001202
1203 return msgbuf;
1204}
1205
1206/* Set an OS/2-specific error and return NULL. OS/2 kernel
1207 errors are not in a global variable e.g. 'errno' nor are
1208 they congruent with posix error numbers. */
1209
Victor Stinner8c62be82010-05-06 00:08:46 +00001210static PyObject *
1211os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001212{
1213 char text[1024];
1214 PyObject *v;
1215
1216 os2_strerror(text, sizeof(text), code, "");
1217
1218 v = Py_BuildValue("(is)", code, text);
1219 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +00001220 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001221 Py_DECREF(v);
1222 }
1223 return NULL; /* Signal to Python that an Exception is Pending */
1224}
1225
1226#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001227
1228/* POSIX generic methods */
1229
Barry Warsaw53699e91996-12-10 23:23:01 +00001230static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001231posix_fildes(PyObject *fdobj, int (*func)(int))
1232{
Victor Stinner8c62be82010-05-06 00:08:46 +00001233 int fd;
1234 int res;
1235 fd = PyObject_AsFileDescriptor(fdobj);
1236 if (fd < 0)
1237 return NULL;
1238 if (!_PyVerify_fd(fd))
1239 return posix_error();
1240 Py_BEGIN_ALLOW_THREADS
1241 res = (*func)(fd);
1242 Py_END_ALLOW_THREADS
1243 if (res < 0)
1244 return posix_error();
1245 Py_INCREF(Py_None);
1246 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001247}
Guido van Rossum21142a01999-01-08 21:05:37 +00001248
1249static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +00001250posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001251{
Victor Stinner8c62be82010-05-06 00:08:46 +00001252 PyObject *opath1 = NULL;
1253 char *path1;
1254 int res;
1255 if (!PyArg_ParseTuple(args, format,
1256 PyUnicode_FSConverter, &opath1))
1257 return NULL;
1258 path1 = PyBytes_AsString(opath1);
1259 Py_BEGIN_ALLOW_THREADS
1260 res = (*func)(path1);
1261 Py_END_ALLOW_THREADS
1262 if (res < 0)
1263 return posix_error_with_allocated_filename(opath1);
1264 Py_DECREF(opath1);
1265 Py_INCREF(Py_None);
1266 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001267}
1268
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001269
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001270#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001271static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +00001272win32_1str(PyObject* args, char* func,
1273 char* format, BOOL (__stdcall *funcA)(LPCSTR),
1274 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +00001275{
Victor Stinner8c62be82010-05-06 00:08:46 +00001276 PyObject *uni;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001277 const char *ansi;
Victor Stinner8c62be82010-05-06 00:08:46 +00001278 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001279
Victor Stinnereb5657a2011-09-30 01:44:27 +02001280 if (PyArg_ParseTuple(args, wformat, &uni))
1281 {
1282 wchar_t *wstr = PyUnicode_AsUnicode(uni);
1283 if (wstr == NULL)
1284 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001285 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02001286 result = funcW(wstr);
Victor Stinner8c62be82010-05-06 00:08:46 +00001287 Py_END_ALLOW_THREADS
1288 if (!result)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001289 return win32_error_object(func, uni);
Victor Stinner8c62be82010-05-06 00:08:46 +00001290 Py_INCREF(Py_None);
1291 return Py_None;
1292 }
Victor Stinnereb5657a2011-09-30 01:44:27 +02001293 PyErr_Clear();
1294
Victor Stinner8c62be82010-05-06 00:08:46 +00001295 if (!PyArg_ParseTuple(args, format, &ansi))
1296 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001297 if (win32_warn_bytes_api())
1298 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001299 Py_BEGIN_ALLOW_THREADS
1300 result = funcA(ansi);
1301 Py_END_ALLOW_THREADS
1302 if (!result)
1303 return win32_error(func, ansi);
1304 Py_INCREF(Py_None);
1305 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001306
1307}
1308
1309/* This is a reimplementation of the C library's chdir function,
1310 but one that produces Win32 errors instead of DOS error codes.
1311 chdir is essentially a wrapper around SetCurrentDirectory; however,
1312 it also needs to set "magic" environment variables indicating
1313 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001314static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001315win32_chdir(LPCSTR path)
1316{
Victor Stinner8c62be82010-05-06 00:08:46 +00001317 char new_path[MAX_PATH+1];
1318 int result;
1319 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001320
Victor Stinner8c62be82010-05-06 00:08:46 +00001321 if(!SetCurrentDirectoryA(path))
1322 return FALSE;
1323 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
1324 if (!result)
1325 return FALSE;
1326 /* In the ANSI API, there should not be any paths longer
1327 than MAX_PATH. */
1328 assert(result <= MAX_PATH+1);
1329 if (strncmp(new_path, "\\\\", 2) == 0 ||
1330 strncmp(new_path, "//", 2) == 0)
1331 /* UNC path, nothing to do. */
1332 return TRUE;
1333 env[1] = new_path[0];
1334 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001335}
1336
1337/* The Unicode version differs from the ANSI version
1338 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001339static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001340win32_wchdir(LPCWSTR path)
1341{
Victor Stinner8c62be82010-05-06 00:08:46 +00001342 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
1343 int result;
1344 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001345
Victor Stinner8c62be82010-05-06 00:08:46 +00001346 if(!SetCurrentDirectoryW(path))
1347 return FALSE;
1348 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
1349 if (!result)
1350 return FALSE;
1351 if (result > MAX_PATH+1) {
1352 new_path = malloc(result * sizeof(wchar_t));
1353 if (!new_path) {
1354 SetLastError(ERROR_OUTOFMEMORY);
1355 return FALSE;
1356 }
1357 result = GetCurrentDirectoryW(result, new_path);
1358 if (!result) {
1359 free(new_path);
1360 return FALSE;
1361 }
1362 }
1363 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1364 wcsncmp(new_path, L"//", 2) == 0)
1365 /* UNC path, nothing to do. */
1366 return TRUE;
1367 env[1] = new_path[0];
1368 result = SetEnvironmentVariableW(env, new_path);
1369 if (new_path != _new_path)
1370 free(new_path);
1371 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001372}
1373#endif
1374
Martin v. Löwis14694662006-02-03 12:54:16 +00001375#ifdef MS_WINDOWS
1376/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1377 - time stamps are restricted to second resolution
1378 - file modification times suffer from forth-and-back conversions between
1379 UTC and local time
1380 Therefore, we implement our own stat, based on the Win32 API directly.
1381*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001382#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001383
1384struct win32_stat{
1385 int st_dev;
1386 __int64 st_ino;
1387 unsigned short st_mode;
1388 int st_nlink;
1389 int st_uid;
1390 int st_gid;
1391 int st_rdev;
1392 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001393 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001394 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001395 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001396 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001397 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001398 int st_ctime_nsec;
1399};
1400
1401static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
1402
1403static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001404FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +00001405{
Victor Stinner8c62be82010-05-06 00:08:46 +00001406 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
1407 /* Cannot simply cast and dereference in_ptr,
1408 since it might not be aligned properly */
1409 __int64 in;
1410 memcpy(&in, in_ptr, sizeof(in));
1411 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001412 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001413}
1414
Thomas Wouters477c8d52006-05-27 19:21:47 +00001415static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001416time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001417{
Victor Stinner8c62be82010-05-06 00:08:46 +00001418 /* XXX endianness */
1419 __int64 out;
1420 out = time_in + secs_between_epochs;
1421 out = out * 10000000 + nsec_in / 100;
1422 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001423}
1424
Martin v. Löwis14694662006-02-03 12:54:16 +00001425/* Below, we *know* that ugo+r is 0444 */
1426#if _S_IREAD != 0400
1427#error Unsupported C library
1428#endif
1429static int
1430attributes_to_mode(DWORD attr)
1431{
Victor Stinner8c62be82010-05-06 00:08:46 +00001432 int m = 0;
1433 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1434 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1435 else
1436 m |= _S_IFREG;
1437 if (attr & FILE_ATTRIBUTE_READONLY)
1438 m |= 0444;
1439 else
1440 m |= 0666;
1441 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001442}
1443
1444static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001445attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001446{
Victor Stinner8c62be82010-05-06 00:08:46 +00001447 memset(result, 0, sizeof(*result));
1448 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1449 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1450 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1451 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1452 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001453 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001454 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001455 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1456 /* first clear the S_IFMT bits */
1457 result->st_mode ^= (result->st_mode & 0170000);
1458 /* now set the bits that make this a symlink */
1459 result->st_mode |= 0120000;
1460 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001461
Victor Stinner8c62be82010-05-06 00:08:46 +00001462 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001463}
1464
Guido van Rossumd8faa362007-04-27 19:54:29 +00001465static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001466attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001467{
Victor Stinner8c62be82010-05-06 00:08:46 +00001468 HANDLE hFindFile;
1469 WIN32_FIND_DATAA FileData;
1470 hFindFile = FindFirstFileA(pszFile, &FileData);
1471 if (hFindFile == INVALID_HANDLE_VALUE)
1472 return FALSE;
1473 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001474 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001475 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001476 info->dwFileAttributes = FileData.dwFileAttributes;
1477 info->ftCreationTime = FileData.ftCreationTime;
1478 info->ftLastAccessTime = FileData.ftLastAccessTime;
1479 info->ftLastWriteTime = FileData.ftLastWriteTime;
1480 info->nFileSizeHigh = FileData.nFileSizeHigh;
1481 info->nFileSizeLow = FileData.nFileSizeLow;
1482/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001483 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1484 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001485 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001486}
1487
1488static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001489attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001490{
Victor Stinner8c62be82010-05-06 00:08:46 +00001491 HANDLE hFindFile;
1492 WIN32_FIND_DATAW FileData;
1493 hFindFile = FindFirstFileW(pszFile, &FileData);
1494 if (hFindFile == INVALID_HANDLE_VALUE)
1495 return FALSE;
1496 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001497 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001498 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001499 info->dwFileAttributes = FileData.dwFileAttributes;
1500 info->ftCreationTime = FileData.ftCreationTime;
1501 info->ftLastAccessTime = FileData.ftLastAccessTime;
1502 info->ftLastWriteTime = FileData.ftLastWriteTime;
1503 info->nFileSizeHigh = FileData.nFileSizeHigh;
1504 info->nFileSizeLow = FileData.nFileSizeLow;
1505/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001506 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1507 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001508 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001509}
1510
Brian Curtind25aef52011-06-13 15:16:04 -05001511/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
1512static int has_GetFinalPathNameByHandle = 0;
Brian Curtind25aef52011-06-13 15:16:04 -05001513static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1514 DWORD);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001515static int
Brian Curtind25aef52011-06-13 15:16:04 -05001516check_GetFinalPathNameByHandle()
Brian Curtinf5e76d02010-11-24 13:14:05 +00001517{
Brian Curtind25aef52011-06-13 15:16:04 -05001518 HINSTANCE hKernel32;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001519 DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1520 DWORD);
1521
Brian Curtind25aef52011-06-13 15:16:04 -05001522 /* only recheck */
1523 if (!has_GetFinalPathNameByHandle)
1524 {
Martin v. Löwis50590f12012-01-14 17:54:09 +01001525 hKernel32 = GetModuleHandleW(L"KERNEL32");
Brian Curtind25aef52011-06-13 15:16:04 -05001526 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1527 "GetFinalPathNameByHandleA");
1528 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1529 "GetFinalPathNameByHandleW");
1530 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1531 Py_GetFinalPathNameByHandleW;
1532 }
1533 return has_GetFinalPathNameByHandle;
1534}
1535
1536static BOOL
1537get_target_path(HANDLE hdl, wchar_t **target_path)
1538{
1539 int buf_size, result_length;
1540 wchar_t *buf;
1541
1542 /* We have a good handle to the target, use it to determine
1543 the target path name (then we'll call lstat on it). */
1544 buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0,
1545 VOLUME_NAME_DOS);
1546 if(!buf_size)
1547 return FALSE;
1548
1549 buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001550 if (!buf) {
1551 SetLastError(ERROR_OUTOFMEMORY);
1552 return FALSE;
1553 }
1554
Brian Curtind25aef52011-06-13 15:16:04 -05001555 result_length = Py_GetFinalPathNameByHandleW(hdl,
1556 buf, buf_size, VOLUME_NAME_DOS);
1557
1558 if(!result_length) {
1559 free(buf);
1560 return FALSE;
1561 }
1562
1563 if(!CloseHandle(hdl)) {
1564 free(buf);
1565 return FALSE;
1566 }
1567
1568 buf[result_length] = 0;
1569
1570 *target_path = buf;
1571 return TRUE;
1572}
1573
1574static int
1575win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1576 BOOL traverse);
1577static int
1578win32_xstat_impl(const char *path, struct win32_stat *result,
1579 BOOL traverse)
1580{
Victor Stinner26de69d2011-06-17 15:15:38 +02001581 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001582 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001583 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001584 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001585 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001586 const char *dot;
1587
Brian Curtind25aef52011-06-13 15:16:04 -05001588 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001589 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1590 traverse reparse point. */
1591 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001592 }
1593
Brian Curtinf5e76d02010-11-24 13:14:05 +00001594 hFile = CreateFileA(
1595 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001596 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001597 0, /* share mode */
1598 NULL, /* security attributes */
1599 OPEN_EXISTING,
1600 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001601 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1602 Because of this, calls like GetFinalPathNameByHandle will return
1603 the symlink path agin and not the actual final path. */
1604 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1605 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001606 NULL);
1607
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001608 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001609 /* Either the target doesn't exist, or we don't have access to
1610 get a handle to it. If the former, we need to return an error.
1611 If the latter, we can use attributes_from_dir. */
1612 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001613 return -1;
1614 /* Could not get attributes on open file. Fall back to
1615 reading the directory. */
1616 if (!attributes_from_dir(path, &info, &reparse_tag))
1617 /* Very strange. This should not fail now */
1618 return -1;
1619 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1620 if (traverse) {
1621 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001622 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001623 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001624 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001625 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001626 } else {
1627 if (!GetFileInformationByHandle(hFile, &info)) {
1628 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001629 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001630 }
1631 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001632 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1633 return -1;
1634
1635 /* Close the outer open file handle now that we're about to
1636 reopen it with different flags. */
1637 if (!CloseHandle(hFile))
1638 return -1;
1639
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001640 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001641 /* In order to call GetFinalPathNameByHandle we need to open
1642 the file without the reparse handling flag set. */
1643 hFile2 = CreateFileA(
1644 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1645 NULL, OPEN_EXISTING,
1646 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1647 NULL);
1648 if (hFile2 == INVALID_HANDLE_VALUE)
1649 return -1;
1650
1651 if (!get_target_path(hFile2, &target_path))
1652 return -1;
1653
1654 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001655 free(target_path);
1656 return code;
1657 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001658 } else
1659 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001660 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001661 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001662
1663 /* Set S_IEXEC if it is an .exe, .bat, ... */
1664 dot = strrchr(path, '.');
1665 if (dot) {
1666 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1667 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1668 result->st_mode |= 0111;
1669 }
1670 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001671}
1672
1673static int
Brian Curtind25aef52011-06-13 15:16:04 -05001674win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1675 BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001676{
1677 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001678 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001679 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001680 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001681 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001682 const wchar_t *dot;
1683
Brian Curtind25aef52011-06-13 15:16:04 -05001684 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001685 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1686 traverse reparse point. */
1687 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001688 }
1689
Brian Curtinf5e76d02010-11-24 13:14:05 +00001690 hFile = CreateFileW(
1691 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001692 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001693 0, /* share mode */
1694 NULL, /* security attributes */
1695 OPEN_EXISTING,
1696 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001697 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1698 Because of this, calls like GetFinalPathNameByHandle will return
1699 the symlink path agin and not the actual final path. */
Victor Stinner26de69d2011-06-17 15:15:38 +02001700 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
Brian Curtind25aef52011-06-13 15:16:04 -05001701 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001702 NULL);
1703
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001704 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001705 /* Either the target doesn't exist, or we don't have access to
1706 get a handle to it. If the former, we need to return an error.
1707 If the latter, we can use attributes_from_dir. */
1708 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001709 return -1;
1710 /* Could not get attributes on open file. Fall back to
1711 reading the directory. */
1712 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1713 /* Very strange. This should not fail now */
1714 return -1;
1715 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1716 if (traverse) {
1717 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001718 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001719 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001720 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001721 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001722 } else {
1723 if (!GetFileInformationByHandle(hFile, &info)) {
1724 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001725 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001726 }
1727 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001728 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1729 return -1;
1730
1731 /* Close the outer open file handle now that we're about to
1732 reopen it with different flags. */
1733 if (!CloseHandle(hFile))
1734 return -1;
1735
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001736 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001737 /* In order to call GetFinalPathNameByHandle we need to open
1738 the file without the reparse handling flag set. */
1739 hFile2 = CreateFileW(
1740 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1741 NULL, OPEN_EXISTING,
1742 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1743 NULL);
1744 if (hFile2 == INVALID_HANDLE_VALUE)
1745 return -1;
1746
1747 if (!get_target_path(hFile2, &target_path))
1748 return -1;
1749
1750 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001751 free(target_path);
1752 return code;
1753 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001754 } else
1755 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001756 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001757 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001758
1759 /* Set S_IEXEC if it is an .exe, .bat, ... */
1760 dot = wcsrchr(path, '.');
1761 if (dot) {
1762 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1763 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1764 result->st_mode |= 0111;
1765 }
1766 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001767}
1768
1769static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001770win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001771{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001772 /* Protocol violation: we explicitly clear errno, instead of
1773 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001774 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001775 errno = 0;
1776 return code;
1777}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001778
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001779static int
1780win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1781{
1782 /* Protocol violation: we explicitly clear errno, instead of
1783 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001784 int code = win32_xstat_impl_w(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001785 errno = 0;
1786 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001787}
Brian Curtind25aef52011-06-13 15:16:04 -05001788/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001789
1790 In Posix, stat automatically traverses symlinks and returns the stat
1791 structure for the target. In Windows, the equivalent GetFileAttributes by
1792 default does not traverse symlinks and instead returns attributes for
1793 the symlink.
1794
1795 Therefore, win32_lstat will get the attributes traditionally, and
1796 win32_stat will first explicitly resolve the symlink target and then will
1797 call win32_lstat on that result.
1798
Ezio Melotti4969f702011-03-15 05:59:46 +02001799 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001800
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001801static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001802win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001803{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001804 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001805}
1806
Victor Stinner8c62be82010-05-06 00:08:46 +00001807static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001808win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001809{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001810 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001811}
1812
1813static int
1814win32_stat(const char* path, struct win32_stat *result)
1815{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001816 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001817}
1818
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001819static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001820win32_stat_w(const wchar_t* path, struct win32_stat *result)
1821{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001822 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001823}
1824
1825static int
1826win32_fstat(int file_number, struct win32_stat *result)
1827{
Victor Stinner8c62be82010-05-06 00:08:46 +00001828 BY_HANDLE_FILE_INFORMATION info;
1829 HANDLE h;
1830 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001831
Richard Oudkerk2240ac12012-07-06 12:05:32 +01001832 if (!_PyVerify_fd(file_number))
1833 h = INVALID_HANDLE_VALUE;
1834 else
1835 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001836
Victor Stinner8c62be82010-05-06 00:08:46 +00001837 /* Protocol violation: we explicitly clear errno, instead of
1838 setting it to a POSIX error. Callers should use GetLastError. */
1839 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001840
Victor Stinner8c62be82010-05-06 00:08:46 +00001841 if (h == INVALID_HANDLE_VALUE) {
1842 /* This is really a C library error (invalid file handle).
1843 We set the Win32 error to the closes one matching. */
1844 SetLastError(ERROR_INVALID_HANDLE);
1845 return -1;
1846 }
1847 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001848
Victor Stinner8c62be82010-05-06 00:08:46 +00001849 type = GetFileType(h);
1850 if (type == FILE_TYPE_UNKNOWN) {
1851 DWORD error = GetLastError();
1852 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001853 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001854 }
1855 /* else: valid but unknown file */
1856 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001857
Victor Stinner8c62be82010-05-06 00:08:46 +00001858 if (type != FILE_TYPE_DISK) {
1859 if (type == FILE_TYPE_CHAR)
1860 result->st_mode = _S_IFCHR;
1861 else if (type == FILE_TYPE_PIPE)
1862 result->st_mode = _S_IFIFO;
1863 return 0;
1864 }
1865
1866 if (!GetFileInformationByHandle(h, &info)) {
1867 return -1;
1868 }
1869
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001870 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001871 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001872 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1873 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001874}
1875
1876#endif /* MS_WINDOWS */
1877
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001878PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07001879"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001880This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001881 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001882or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1883\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001884Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1885or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001886\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001887See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001888
1889static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001890 {"st_mode", "protection bits"},
1891 {"st_ino", "inode"},
1892 {"st_dev", "device"},
1893 {"st_nlink", "number of hard links"},
1894 {"st_uid", "user ID of owner"},
1895 {"st_gid", "group ID of owner"},
1896 {"st_size", "total size, in bytes"},
1897 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1898 {NULL, "integer time of last access"},
1899 {NULL, "integer time of last modification"},
1900 {NULL, "integer time of last change"},
1901 {"st_atime", "time of last access"},
1902 {"st_mtime", "time of last modification"},
1903 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07001904 {"st_atime_ns", "time of last access in nanoseconds"},
1905 {"st_mtime_ns", "time of last modification in nanoseconds"},
1906 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001907#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001908 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001909#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001910#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001911 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001912#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001913#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001914 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001915#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001916#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001917 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001918#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001919#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001920 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001921#endif
1922#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001923 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001924#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001925 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001926};
1927
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001928#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07001929#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001930#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07001931#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001932#endif
1933
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001934#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001935#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1936#else
1937#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1938#endif
1939
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001940#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001941#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1942#else
1943#define ST_RDEV_IDX ST_BLOCKS_IDX
1944#endif
1945
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001946#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1947#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1948#else
1949#define ST_FLAGS_IDX ST_RDEV_IDX
1950#endif
1951
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001952#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001953#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001954#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001955#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001956#endif
1957
1958#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1959#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1960#else
1961#define ST_BIRTHTIME_IDX ST_GEN_IDX
1962#endif
1963
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001964static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001965 "stat_result", /* name */
1966 stat_result__doc__, /* doc */
1967 stat_result_fields,
1968 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001969};
1970
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001971PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001972"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1973This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001974 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001975or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001976\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001977See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001978
1979static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001980 {"f_bsize", },
1981 {"f_frsize", },
1982 {"f_blocks", },
1983 {"f_bfree", },
1984 {"f_bavail", },
1985 {"f_files", },
1986 {"f_ffree", },
1987 {"f_favail", },
1988 {"f_flag", },
1989 {"f_namemax",},
1990 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001991};
1992
1993static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001994 "statvfs_result", /* name */
1995 statvfs_result__doc__, /* doc */
1996 statvfs_result_fields,
1997 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001998};
1999
Ross Lagerwall7807c352011-03-17 20:20:30 +02002000#if defined(HAVE_WAITID) && !defined(__APPLE__)
2001PyDoc_STRVAR(waitid_result__doc__,
2002"waitid_result: Result from waitid.\n\n\
2003This object may be accessed either as a tuple of\n\
2004 (si_pid, si_uid, si_signo, si_status, si_code),\n\
2005or via the attributes si_pid, si_uid, and so on.\n\
2006\n\
2007See os.waitid for more information.");
2008
2009static PyStructSequence_Field waitid_result_fields[] = {
2010 {"si_pid", },
2011 {"si_uid", },
2012 {"si_signo", },
2013 {"si_status", },
2014 {"si_code", },
2015 {0}
2016};
2017
2018static PyStructSequence_Desc waitid_result_desc = {
2019 "waitid_result", /* name */
2020 waitid_result__doc__, /* doc */
2021 waitid_result_fields,
2022 5
2023};
2024static PyTypeObject WaitidResultType;
2025#endif
2026
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002027static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002028static PyTypeObject StatResultType;
2029static PyTypeObject StatVFSResultType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05002030#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05002031static PyTypeObject SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05002032#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002033static newfunc structseq_new;
2034
2035static PyObject *
2036statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2037{
Victor Stinner8c62be82010-05-06 00:08:46 +00002038 PyStructSequence *result;
2039 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002040
Victor Stinner8c62be82010-05-06 00:08:46 +00002041 result = (PyStructSequence*)structseq_new(type, args, kwds);
2042 if (!result)
2043 return NULL;
2044 /* If we have been initialized from a tuple,
2045 st_?time might be set to None. Initialize it
2046 from the int slots. */
2047 for (i = 7; i <= 9; i++) {
2048 if (result->ob_item[i+3] == Py_None) {
2049 Py_DECREF(Py_None);
2050 Py_INCREF(result->ob_item[i]);
2051 result->ob_item[i+3] = result->ob_item[i];
2052 }
2053 }
2054 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002055}
2056
2057
2058
2059/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00002060static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002061
2062PyDoc_STRVAR(stat_float_times__doc__,
2063"stat_float_times([newval]) -> oldval\n\n\
2064Determine whether os.[lf]stat represents time stamps as float objects.\n\
2065If newval is True, future calls to stat() return floats, if it is False,\n\
2066future calls return ints. \n\
2067If newval is omitted, return the current setting.\n");
2068
2069static PyObject*
2070stat_float_times(PyObject* self, PyObject *args)
2071{
Victor Stinner8c62be82010-05-06 00:08:46 +00002072 int newval = -1;
2073 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
2074 return NULL;
Victor Stinner034d0aa2012-06-05 01:22:15 +02002075 if (PyErr_WarnEx(PyExc_DeprecationWarning,
2076 "stat_float_times() is deprecated",
2077 1))
2078 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002079 if (newval == -1)
2080 /* Return old value */
2081 return PyBool_FromLong(_stat_float_times);
2082 _stat_float_times = newval;
2083 Py_INCREF(Py_None);
2084 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002085}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002086
Larry Hastings6fe20b32012-04-19 15:07:49 -07002087static PyObject *billion = NULL;
2088
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002089static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01002090fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002091{
Larry Hastings6fe20b32012-04-19 15:07:49 -07002092 PyObject *s = _PyLong_FromTime_t(sec);
2093 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
2094 PyObject *s_in_ns = NULL;
2095 PyObject *ns_total = NULL;
2096 PyObject *float_s = NULL;
2097
2098 if (!(s && ns_fractional))
2099 goto exit;
2100
2101 s_in_ns = PyNumber_Multiply(s, billion);
2102 if (!s_in_ns)
2103 goto exit;
2104
2105 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
2106 if (!ns_total)
2107 goto exit;
2108
Victor Stinner4195b5c2012-02-08 23:03:19 +01002109 if (_stat_float_times) {
Larry Hastings6fe20b32012-04-19 15:07:49 -07002110 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
2111 if (!float_s)
2112 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00002113 }
Larry Hastings6fe20b32012-04-19 15:07:49 -07002114 else {
2115 float_s = s;
2116 Py_INCREF(float_s);
2117 }
2118
2119 PyStructSequence_SET_ITEM(v, index, s);
2120 PyStructSequence_SET_ITEM(v, index+3, float_s);
2121 PyStructSequence_SET_ITEM(v, index+6, ns_total);
2122 s = NULL;
2123 float_s = NULL;
2124 ns_total = NULL;
2125exit:
2126 Py_XDECREF(s);
2127 Py_XDECREF(ns_fractional);
2128 Py_XDECREF(s_in_ns);
2129 Py_XDECREF(ns_total);
2130 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002131}
2132
Tim Peters5aa91602002-01-30 05:46:57 +00002133/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00002134 (used by posix_stat() and posix_fstat()) */
2135static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01002136_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00002137{
Victor Stinner8c62be82010-05-06 00:08:46 +00002138 unsigned long ansec, mnsec, cnsec;
2139 PyObject *v = PyStructSequence_New(&StatResultType);
2140 if (v == NULL)
2141 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00002142
Victor Stinner8c62be82010-05-06 00:08:46 +00002143 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00002144#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00002145 PyStructSequence_SET_ITEM(v, 1,
2146 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00002147#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002148 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00002149#endif
2150#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00002151 PyStructSequence_SET_ITEM(v, 2,
2152 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002153#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002154 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002155#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002156 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
2157 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
2158 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00002159#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00002160 PyStructSequence_SET_ITEM(v, 6,
2161 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00002162#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002163 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00002164#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002165
Martin v. Löwis14694662006-02-03 12:54:16 +00002166#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002167 ansec = st->st_atim.tv_nsec;
2168 mnsec = st->st_mtim.tv_nsec;
2169 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002170#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002171 ansec = st->st_atimespec.tv_nsec;
2172 mnsec = st->st_mtimespec.tv_nsec;
2173 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002174#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002175 ansec = st->st_atime_nsec;
2176 mnsec = st->st_mtime_nsec;
2177 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002178#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002179 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002180#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002181 fill_time(v, 7, st->st_atime, ansec);
2182 fill_time(v, 8, st->st_mtime, mnsec);
2183 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002184
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002185#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002186 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2187 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002188#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002189#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002190 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2191 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002192#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002193#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002194 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2195 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002196#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002197#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002198 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2199 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002200#endif
2201#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002202 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002203 PyObject *val;
2204 unsigned long bsec,bnsec;
2205 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002206#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002207 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002208#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002209 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002210#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002211 if (_stat_float_times) {
2212 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
2213 } else {
2214 val = PyLong_FromLong((long)bsec);
2215 }
2216 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2217 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002218 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002219#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002220#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002221 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2222 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002223#endif
Fred Drake699f3522000-06-29 21:12:41 +00002224
Victor Stinner8c62be82010-05-06 00:08:46 +00002225 if (PyErr_Occurred()) {
2226 Py_DECREF(v);
2227 return NULL;
2228 }
Fred Drake699f3522000-06-29 21:12:41 +00002229
Victor Stinner8c62be82010-05-06 00:08:46 +00002230 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002231}
2232
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002233/* POSIX methods */
2234
Guido van Rossum94f6f721999-01-06 18:42:14 +00002235
2236static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07002237posix_do_stat(char *function_name, path_t *path,
2238 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002239{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002240 STRUCT_STAT st;
2241 int result;
2242
2243#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2244 if (follow_symlinks_specified(function_name, follow_symlinks))
2245 return NULL;
2246#endif
2247
2248 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2249 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2250 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2251 return NULL;
2252
2253 Py_BEGIN_ALLOW_THREADS
2254 if (path->fd != -1)
2255 result = FSTAT(path->fd, &st);
2256 else
2257#ifdef MS_WINDOWS
2258 if (path->wide) {
2259 if (follow_symlinks)
2260 result = win32_stat_w(path->wide, &st);
2261 else
2262 result = win32_lstat_w(path->wide, &st);
2263 }
2264 else
2265#endif
2266#if defined(HAVE_LSTAT) || defined(MS_WINDOWS)
2267 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2268 result = LSTAT(path->narrow, &st);
2269 else
2270#endif
2271#ifdef HAVE_FSTATAT
2272 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
2273 result = fstatat(dir_fd, path->narrow, &st,
2274 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2275 else
2276#endif
2277 result = STAT(path->narrow, &st);
2278 Py_END_ALLOW_THREADS
2279
2280 if (result != 0)
2281 return path_error("stat", path);
2282
2283 return _pystat_fromstructstat(&st);
2284}
2285
2286PyDoc_STRVAR(posix_stat__doc__,
2287"stat(path, *, dir_fd=None, follow_symlinks=True) -> stat result\n\n\
2288Perform a stat system call on the given path.\n\
2289\n\
2290path may be specified as either a string or as an open file descriptor.\n\
2291\n\
2292If dir_fd is not None, it should be a file descriptor open to a directory,\n\
2293 and path should be relative; path will then be relative to that directory.\n\
2294 dir_fd may not be supported on your platform; if it is unavailable, using\n\
2295 it will raise a NotImplementedError.\n\
2296If follow_symlinks is False, and the last element of the path is a symbolic\n\
2297 link, stat will examine the symbolic link itself instead of the file the\n\
2298 link points to.\n\
2299It is an error to use dir_fd or follow_symlinks when specifying path as\n\
2300 an open file descriptor.");
2301
2302static PyObject *
2303posix_stat(PyObject *self, PyObject *args, PyObject *kwargs)
2304{
2305 static char *keywords[] = {"path", "dir_fd", "follow_symlinks", NULL};
2306 path_t path;
2307 int dir_fd = DEFAULT_DIR_FD;
2308 int follow_symlinks = 1;
2309 PyObject *return_value;
2310
2311 memset(&path, 0, sizeof(path));
2312 path.allow_fd = 1;
2313 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&p:stat", keywords,
2314 path_converter, &path,
2315#ifdef HAVE_FSTATAT
2316 dir_fd_converter, &dir_fd,
2317#else
2318 dir_fd_unavailable, &dir_fd,
2319#endif
2320 &follow_symlinks))
2321 return NULL;
2322 return_value = posix_do_stat("stat", &path, dir_fd, follow_symlinks);
2323 path_cleanup(&path);
2324 return return_value;
2325}
2326
2327PyDoc_STRVAR(posix_lstat__doc__,
2328"lstat(path, *, dir_fd=None) -> stat result\n\n\
2329Like stat(), but do not follow symbolic links.\n\
2330Equivalent to stat(path, follow_symlinks=False).");
2331
2332static PyObject *
2333posix_lstat(PyObject *self, PyObject *args, PyObject *kwargs)
2334{
2335 static char *keywords[] = {"path", "dir_fd", NULL};
2336 path_t path;
2337 int dir_fd = DEFAULT_DIR_FD;
2338 int follow_symlinks = 0;
2339 PyObject *return_value;
2340
2341 memset(&path, 0, sizeof(path));
2342 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:lstat", keywords,
2343 path_converter, &path,
2344#ifdef HAVE_FSTATAT
2345 dir_fd_converter, &dir_fd
2346#else
2347 dir_fd_unavailable, &dir_fd
2348#endif
2349 ))
2350 return NULL;
2351 return_value = posix_do_stat("stat", &path, dir_fd, follow_symlinks);
2352 path_cleanup(&path);
2353 return return_value;
2354}
2355
2356PyDoc_STRVAR(posix_access__doc__,
2357"access(path, mode, *, dir_fd=None, effective_ids=False,\
2358 follow_symlinks=True)\n\n\
2359Use the real uid/gid to test for access to a path. Returns True if granted,\n\
2360False otherwise.\n\
2361\n\
2362If dir_fd is not None, it should be a file descriptor open to a directory,\n\
2363 and path should be relative; path will then be relative to that directory.\n\
2364If effective_ids is True, access will use the effective uid/gid instead of\n\
2365 the real uid/gid.\n\
2366If follow_symlinks is False, and the last element of the path is a symbolic\n\
2367 link, access will examine the symbolic link itself instead of the file the\n\
2368 link points to.\n\
2369dir_fd, effective_ids, and follow_symlinks may not be implemented\n\
2370 on your platform. If they are unavailable, using them will raise a\n\
2371 NotImplementedError.\n\
2372\n\
2373Note that most operations will use the effective uid/gid, therefore this\n\
2374 routine can be used in a suid/sgid environment to test if the invoking user\n\
2375 has the specified access to the path.\n\
2376The mode argument can be F_OK to test existence, or the inclusive-OR\n\
2377 of R_OK, W_OK, and X_OK.");
2378
2379static PyObject *
2380posix_access(PyObject *self, PyObject *args, PyObject *kwargs)
2381{
2382 static char *keywords[] = {"path", "mode", "dir_fd", "effective_ids",
2383 "follow_symlinks", NULL};
2384 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00002385 int mode;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002386 int dir_fd = DEFAULT_DIR_FD;
2387 int effective_ids = 0;
2388 int follow_symlinks = 1;
2389 PyObject *return_value = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002390
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002391#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002392 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002393#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002394 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002395#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002396
2397 memset(&path, 0, sizeof(path));
2398 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&pp:access", keywords,
2399 path_converter, &path, &mode,
2400#ifdef HAVE_FACCESSAT
2401 dir_fd_converter, &dir_fd,
2402#else
2403 dir_fd_unavailable, &dir_fd,
2404#endif
2405 &effective_ids, &follow_symlinks))
2406 return NULL;
2407
2408#ifndef HAVE_FACCESSAT
2409 if (follow_symlinks_specified("access", follow_symlinks))
2410 goto exit;
2411
2412 if (effective_ids) {
2413 argument_unavailable_error("access", "effective_ids");
2414 goto exit;
2415 }
2416#endif
2417
2418#ifdef MS_WINDOWS
2419 Py_BEGIN_ALLOW_THREADS
2420 if (path.wide != NULL)
2421 attr = GetFileAttributesW(path.wide);
2422 else
2423 attr = GetFileAttributesA(path.narrow);
2424 Py_END_ALLOW_THREADS
2425
2426 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002427 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002428 * * we didn't get a -1, and
2429 * * write access wasn't requested,
2430 * * or the file isn't read-only,
2431 * * or it's a directory.
2432 * (Directories cannot be read-only on Windows.)
2433 */
2434 return_value = PyBool_FromLong(
2435 (attr != 0xFFFFFFFF) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002436 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002437 !(attr & FILE_ATTRIBUTE_READONLY) ||
2438 (attr & FILE_ATTRIBUTE_DIRECTORY)));
2439#else
2440
2441 Py_BEGIN_ALLOW_THREADS
2442#ifdef HAVE_FACCESSAT
2443 if ((dir_fd != DEFAULT_DIR_FD) ||
2444 effective_ids ||
2445 !follow_symlinks) {
2446 int flags = 0;
2447 if (!follow_symlinks)
2448 flags |= AT_SYMLINK_NOFOLLOW;
2449 if (effective_ids)
2450 flags |= AT_EACCESS;
2451 result = faccessat(dir_fd, path.narrow, mode, flags);
2452 }
2453 else
2454#endif
2455 result = access(path.narrow, mode);
2456 Py_END_ALLOW_THREADS
2457 return_value = PyBool_FromLong(!result);
2458#endif
2459
2460#ifndef HAVE_FACCESSAT
2461exit:
2462#endif
2463 path_cleanup(&path);
2464 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002465}
2466
Guido van Rossumd371ff11999-01-25 16:12:23 +00002467#ifndef F_OK
2468#define F_OK 0
2469#endif
2470#ifndef R_OK
2471#define R_OK 4
2472#endif
2473#ifndef W_OK
2474#define W_OK 2
2475#endif
2476#ifndef X_OK
2477#define X_OK 1
2478#endif
2479
2480#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002481PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002482"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002483Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00002484
2485static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002486posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002487{
Victor Stinner8c62be82010-05-06 00:08:46 +00002488 int id;
2489 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002490
Victor Stinner8c62be82010-05-06 00:08:46 +00002491 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
2492 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002493
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002494#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00002495 /* file descriptor 0 only, the default input device (stdin) */
2496 if (id == 0) {
2497 ret = ttyname();
2498 }
2499 else {
2500 ret = NULL;
2501 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002502#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002503 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002504#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002505 if (ret == NULL)
2506 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002507 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002508}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002509#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002510
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002511#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002512PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002513"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002514Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002515
2516static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002517posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002518{
Victor Stinner8c62be82010-05-06 00:08:46 +00002519 char *ret;
2520 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002521
Greg Wardb48bc172000-03-01 21:51:56 +00002522#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002523 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002524#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002525 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002526#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002527 if (ret == NULL)
2528 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002529 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002530}
2531#endif
2532
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002533PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002534"chdir(path)\n\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07002535Change the current working directory to the specified path.\n\
2536\n\
2537path may always be specified as a string.\n\
2538On some platforms, path may also be specified as an open file descriptor.\n\
2539 If this functionality is unavailable, using it raises an exception.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002540
Barry Warsaw53699e91996-12-10 23:23:01 +00002541static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07002542posix_chdir(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002543{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002544 path_t path;
2545 int result;
2546 PyObject *return_value = NULL;
2547 static char *keywords[] = {"path", NULL};
2548
2549 memset(&path, 0, sizeof(path));
2550#ifdef HAVE_FCHDIR
2551 path.allow_fd = 1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002552#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002553 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:chdir", keywords,
2554 path_converter, &path
2555 ))
2556 return NULL;
2557
2558 Py_BEGIN_ALLOW_THREADS
2559#ifdef MS_WINDOWS
2560 if (path.wide)
2561 result = win32_wchdir(path.wide);
2562 else
2563 result = win32_chdir(path.narrow);
2564 result = !result; /* on unix, success = 0, on windows, success = !0 */
2565#elif defined(PYOS_OS2) && defined(PYCC_GCC)
2566 result = _chdir2(path.narrow);
2567#else
2568#ifdef HAVE_FCHDIR
2569 if (path.fd != -1)
2570 result = fchdir(path.fd);
2571 else
2572#endif
2573 result = chdir(path.narrow);
2574#endif
2575 Py_END_ALLOW_THREADS
2576
2577 if (result) {
2578 return_value = path_error("chdir", &path);
2579 goto exit;
2580 }
2581
2582 return_value = Py_None;
2583 Py_INCREF(Py_None);
Georg Brandlf7875592012-06-24 13:58:31 +02002584
Larry Hastings9cf065c2012-06-22 16:30:09 -07002585exit:
2586 path_cleanup(&path);
2587 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002588}
2589
Fred Drake4d1e64b2002-04-15 19:40:07 +00002590#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002591PyDoc_STRVAR(posix_fchdir__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002592"fchdir(fd)\n\n\
2593Change to the directory of the given file descriptor. fd must be\n\
2594opened on a directory, not a file. Equivalent to os.chdir(fd).");
Fred Drake4d1e64b2002-04-15 19:40:07 +00002595
2596static PyObject *
2597posix_fchdir(PyObject *self, PyObject *fdobj)
2598{
Victor Stinner8c62be82010-05-06 00:08:46 +00002599 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002600}
2601#endif /* HAVE_FCHDIR */
2602
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002603
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002604PyDoc_STRVAR(posix_chmod__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002605"chmod(path, mode, *, dir_fd=None, follow_symlinks=True)\n\n\
2606Change the access permissions of a file.\n\
2607\n\
2608path may always be specified as a string.\n\
2609On some platforms, path may also be specified as an open file descriptor.\n\
2610 If this functionality is unavailable, using it raises an exception.\n\
2611If dir_fd is not None, it should be a file descriptor open to a directory,\n\
2612 and path should be relative; path will then be relative to that directory.\n\
2613If follow_symlinks is False, and the last element of the path is a symbolic\n\
2614 link, chmod will modify the symbolic link itself instead of the file the\n\
2615 link points to.\n\
2616It is an error to use dir_fd or follow_symlinks when specifying path as\n\
2617 an open file descriptor.\n\
2618dir_fd and follow_symlinks may not be implemented on your platform.\n\
2619 If they are unavailable, using them will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002620
Barry Warsaw53699e91996-12-10 23:23:01 +00002621static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07002622posix_chmod(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002623{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002624 path_t path;
2625 int mode;
2626 int dir_fd = DEFAULT_DIR_FD;
2627 int follow_symlinks = 1;
2628 int result;
2629 PyObject *return_value = NULL;
2630 static char *keywords[] = {"path", "mode", "dir_fd",
2631 "follow_symlinks", NULL};
2632
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002633#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002634 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002635#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002636
Larry Hastings9cf065c2012-06-22 16:30:09 -07002637#ifdef HAVE_FCHMODAT
2638 int fchmodat_nofollow_unsupported = 0;
2639#endif
2640
2641 memset(&path, 0, sizeof(path));
2642#ifdef HAVE_FCHMOD
2643 path.allow_fd = 1;
2644#endif
2645 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&p:chmod", keywords,
2646 path_converter, &path,
2647 &mode,
2648#ifdef HAVE_FCHMODAT
2649 dir_fd_converter, &dir_fd,
2650#else
2651 dir_fd_unavailable, &dir_fd,
2652#endif
2653 &follow_symlinks))
Victor Stinner8c62be82010-05-06 00:08:46 +00002654 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002655
2656#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
2657 if (follow_symlinks_specified("chmod", follow_symlinks))
2658 goto exit;
2659#endif
2660
2661#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002662 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002663 if (path.wide)
2664 attr = GetFileAttributesW(path.wide);
2665 else
2666 attr = GetFileAttributesA(path.narrow);
2667 if (attr == 0xFFFFFFFF)
2668 result = 0;
2669 else {
2670 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00002671 attr &= ~FILE_ATTRIBUTE_READONLY;
2672 else
2673 attr |= FILE_ATTRIBUTE_READONLY;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002674 if (path.wide)
2675 result = SetFileAttributesW(path.wide, attr);
2676 else
2677 result = SetFileAttributesA(path.narrow, attr);
2678 }
2679 Py_END_ALLOW_THREADS
2680
2681 if (!result) {
2682 return_value = win32_error_object("chmod", path.object);
2683 goto exit;
2684 }
2685#else /* MS_WINDOWS */
2686 Py_BEGIN_ALLOW_THREADS
2687#ifdef HAVE_FCHMOD
2688 if (path.fd != -1)
2689 result = fchmod(path.fd, mode);
2690 else
2691#endif
2692#ifdef HAVE_LCHMOD
2693 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2694 result = lchmod(path.narrow, mode);
2695 else
2696#endif
2697#ifdef HAVE_FCHMODAT
2698 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
2699 /*
2700 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
2701 * The documentation specifically shows how to use it,
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002702 * and then says it isn't implemented yet.
2703 * (true on linux with glibc 2.15, and openindiana 3.x)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002704 *
2705 * Once it is supported, os.chmod will automatically
2706 * support dir_fd and follow_symlinks=False. (Hopefully.)
2707 * Until then, we need to be careful what exception we raise.
2708 */
2709 result = fchmodat(dir_fd, path.narrow, mode,
2710 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2711 /*
2712 * But wait! We can't throw the exception without allowing threads,
2713 * and we can't do that in this nested scope. (Macro trickery, sigh.)
2714 */
2715 fchmodat_nofollow_unsupported =
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002716 result &&
2717 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
2718 !follow_symlinks;
Victor Stinner8c62be82010-05-06 00:08:46 +00002719 }
2720 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002721#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002722 result = chmod(path.narrow, mode);
2723 Py_END_ALLOW_THREADS
2724
2725 if (result) {
2726#ifdef HAVE_FCHMODAT
2727 if (fchmodat_nofollow_unsupported) {
2728 if (dir_fd != DEFAULT_DIR_FD)
2729 dir_fd_and_follow_symlinks_invalid("chmod",
2730 dir_fd, follow_symlinks);
2731 else
2732 follow_symlinks_specified("chmod", follow_symlinks);
2733 }
2734 else
2735#endif
2736 return_value = path_error("chmod", &path);
2737 goto exit;
2738 }
2739#endif
2740
2741 Py_INCREF(Py_None);
2742 return_value = Py_None;
2743exit:
2744 path_cleanup(&path);
2745 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002746}
2747
Larry Hastings9cf065c2012-06-22 16:30:09 -07002748
Christian Heimes4e30a842007-11-30 22:12:06 +00002749#ifdef HAVE_FCHMOD
2750PyDoc_STRVAR(posix_fchmod__doc__,
2751"fchmod(fd, mode)\n\n\
2752Change the access permissions of the file given by file\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07002753descriptor fd. Equivalent to os.chmod(fd, mode).");
Christian Heimes4e30a842007-11-30 22:12:06 +00002754
2755static PyObject *
2756posix_fchmod(PyObject *self, PyObject *args)
2757{
Victor Stinner8c62be82010-05-06 00:08:46 +00002758 int fd, mode, res;
2759 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2760 return NULL;
2761 Py_BEGIN_ALLOW_THREADS
2762 res = fchmod(fd, mode);
2763 Py_END_ALLOW_THREADS
2764 if (res < 0)
2765 return posix_error();
2766 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002767}
2768#endif /* HAVE_FCHMOD */
2769
2770#ifdef HAVE_LCHMOD
2771PyDoc_STRVAR(posix_lchmod__doc__,
2772"lchmod(path, mode)\n\n\
2773Change the access permissions of a file. If path is a symlink, this\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07002774affects the link itself rather than the target.\n\
2775Equivalent to chmod(path, mode, follow_symlinks=False).");
Christian Heimes4e30a842007-11-30 22:12:06 +00002776
2777static PyObject *
2778posix_lchmod(PyObject *self, PyObject *args)
2779{
Victor Stinner8c62be82010-05-06 00:08:46 +00002780 PyObject *opath;
2781 char *path;
2782 int i;
2783 int res;
2784 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2785 &opath, &i))
2786 return NULL;
2787 path = PyBytes_AsString(opath);
2788 Py_BEGIN_ALLOW_THREADS
2789 res = lchmod(path, i);
2790 Py_END_ALLOW_THREADS
2791 if (res < 0)
2792 return posix_error_with_allocated_filename(opath);
2793 Py_DECREF(opath);
2794 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002795}
2796#endif /* HAVE_LCHMOD */
2797
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002798
Thomas Wouterscf297e42007-02-23 15:07:44 +00002799#ifdef HAVE_CHFLAGS
2800PyDoc_STRVAR(posix_chflags__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002801"chflags(path, flags, *, follow_symlinks=True)\n\n\
2802Set file flags.\n\
2803\n\
2804If follow_symlinks is False, and the last element of the path is a symbolic\n\
2805 link, chflags will change flags on the symbolic link itself instead of the\n\
2806 file the link points to.\n\
2807follow_symlinks may not be implemented on your platform. If it is\n\
2808unavailable, using it will raise a NotImplementedError.");
Thomas Wouterscf297e42007-02-23 15:07:44 +00002809
2810static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07002811posix_chflags(PyObject *self, PyObject *args, PyObject *kwargs)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002812{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002813 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00002814 unsigned long flags;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002815 int follow_symlinks = 1;
2816 int result;
2817 PyObject *return_value;
2818 static char *keywords[] = {"path", "flags", "follow_symlinks", NULL};
2819
2820 memset(&path, 0, sizeof(path));
2821 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&k|$i:chflags", keywords,
2822 path_converter, &path,
2823 &flags, &follow_symlinks))
Victor Stinner8c62be82010-05-06 00:08:46 +00002824 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002825
2826#ifndef HAVE_LCHFLAGS
2827 if (follow_symlinks_specified("chflags", follow_symlinks))
2828 goto exit;
2829#endif
2830
Victor Stinner8c62be82010-05-06 00:08:46 +00002831 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002832#ifdef HAVE_LCHFLAGS
2833 if (!follow_symlinks)
2834 result = lchflags(path.narrow, flags);
2835 else
2836#endif
2837 result = chflags(path.narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00002838 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002839
2840 if (result) {
2841 return_value = path_posix_error("chflags", &path);
2842 goto exit;
2843 }
2844
2845 return_value = Py_None;
Victor Stinner8c62be82010-05-06 00:08:46 +00002846 Py_INCREF(Py_None);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002847
2848exit:
2849 path_cleanup(&path);
2850 return return_value;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002851}
2852#endif /* HAVE_CHFLAGS */
2853
2854#ifdef HAVE_LCHFLAGS
2855PyDoc_STRVAR(posix_lchflags__doc__,
2856"lchflags(path, flags)\n\n\
2857Set file flags.\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07002858This function will not follow symbolic links.\n\
2859Equivalent to chflags(path, flags, follow_symlinks=False).");
Thomas Wouterscf297e42007-02-23 15:07:44 +00002860
2861static PyObject *
2862posix_lchflags(PyObject *self, PyObject *args)
2863{
Victor Stinner8c62be82010-05-06 00:08:46 +00002864 PyObject *opath;
2865 char *path;
2866 unsigned long flags;
2867 int res;
2868 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2869 PyUnicode_FSConverter, &opath, &flags))
2870 return NULL;
2871 path = PyBytes_AsString(opath);
2872 Py_BEGIN_ALLOW_THREADS
2873 res = lchflags(path, flags);
2874 Py_END_ALLOW_THREADS
2875 if (res < 0)
2876 return posix_error_with_allocated_filename(opath);
2877 Py_DECREF(opath);
2878 Py_INCREF(Py_None);
2879 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002880}
2881#endif /* HAVE_LCHFLAGS */
2882
Martin v. Löwis244edc82001-10-04 22:44:26 +00002883#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002884PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002885"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002886Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002887
2888static PyObject *
2889posix_chroot(PyObject *self, PyObject *args)
2890{
Victor Stinner8c62be82010-05-06 00:08:46 +00002891 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002892}
2893#endif
2894
Guido van Rossum21142a01999-01-08 21:05:37 +00002895#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002896PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002897"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002898force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002899
2900static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002901posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002902{
Stefan Krah0e803b32010-11-26 16:16:47 +00002903 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002904}
2905#endif /* HAVE_FSYNC */
2906
Ross Lagerwall7807c352011-03-17 20:20:30 +02002907#ifdef HAVE_SYNC
2908PyDoc_STRVAR(posix_sync__doc__,
2909"sync()\n\n\
2910Force write of everything to disk.");
2911
2912static PyObject *
2913posix_sync(PyObject *self, PyObject *noargs)
2914{
2915 Py_BEGIN_ALLOW_THREADS
2916 sync();
2917 Py_END_ALLOW_THREADS
2918 Py_RETURN_NONE;
2919}
2920#endif
2921
Guido van Rossum21142a01999-01-08 21:05:37 +00002922#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002923
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002924#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002925extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2926#endif
2927
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002928PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002929"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002930force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002931 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002932
2933static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002934posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002935{
Stefan Krah0e803b32010-11-26 16:16:47 +00002936 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002937}
2938#endif /* HAVE_FDATASYNC */
2939
2940
Fredrik Lundh10723342000-07-10 16:38:09 +00002941#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002942PyDoc_STRVAR(posix_chown__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002943"chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True)\n\n\
2944Change the owner and group id of path to the numeric uid and gid.\n\
2945\n\
2946path may always be specified as a string.\n\
2947On some platforms, path may also be specified as an open file descriptor.\n\
2948 If this functionality is unavailable, using it raises an exception.\n\
2949If dir_fd is not None, it should be a file descriptor open to a directory,\n\
2950 and path should be relative; path will then be relative to that directory.\n\
2951If follow_symlinks is False, and the last element of the path is a symbolic\n\
2952 link, chown will modify the symbolic link itself instead of the file the\n\
2953 link points to.\n\
2954It is an error to use dir_fd or follow_symlinks when specifying path as\n\
2955 an open file descriptor.\n\
2956dir_fd and follow_symlinks may not be implemented on your platform.\n\
2957 If they are unavailable, using them will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002958
Barry Warsaw53699e91996-12-10 23:23:01 +00002959static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07002960posix_chown(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002961{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002962 path_t path;
2963 long uid_l, gid_l;
2964 uid_t uid;
2965 gid_t gid;
2966 int dir_fd = DEFAULT_DIR_FD;
2967 int follow_symlinks = 1;
2968 int result;
2969 PyObject *return_value = NULL;
2970 static char *keywords[] = {"path", "uid", "gid", "dir_fd",
2971 "follow_symlinks", NULL};
2972
2973 memset(&path, 0, sizeof(path));
2974#ifdef HAVE_FCHOWN
2975 path.allow_fd = 1;
2976#endif
2977 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&ll|$O&p:chown", keywords,
2978 path_converter, &path,
2979 &uid_l, &gid_l,
2980#ifdef HAVE_FCHOWNAT
2981 dir_fd_converter, &dir_fd,
2982#else
2983 dir_fd_unavailable, &dir_fd,
2984#endif
2985 &follow_symlinks))
Victor Stinner8c62be82010-05-06 00:08:46 +00002986 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002987
2988#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
2989 if (follow_symlinks_specified("chown", follow_symlinks))
2990 goto exit;
2991#endif
2992 if (dir_fd_and_fd_invalid("chown", dir_fd, path.fd) ||
2993 fd_and_follow_symlinks_invalid("chown", path.fd, follow_symlinks))
2994 goto exit;
2995
2996#ifdef __APPLE__
2997 /*
2998 * This is for Mac OS X 10.3, which doesn't have lchown.
2999 * (But we still have an lchown symbol because of weak-linking.)
3000 * It doesn't have fchownat either. So there's no possibility
3001 * of a graceful failover.
Georg Brandlf7875592012-06-24 13:58:31 +02003002 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003003 if ((!follow_symlinks) && (lchown == NULL)) {
3004 follow_symlinks_specified("chown", follow_symlinks);
3005 goto exit;
3006 }
3007#endif
3008
Victor Stinner8c62be82010-05-06 00:08:46 +00003009 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003010 uid = (uid_t)uid_l;
3011 gid = (uid_t)gid_l;
3012#ifdef HAVE_FCHOWN
3013 if (path.fd != -1)
3014 result = fchown(path.fd, uid, gid);
3015 else
3016#endif
3017#ifdef HAVE_LCHOWN
3018 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
3019 result = lchown(path.narrow, uid, gid);
3020 else
3021#endif
3022#ifdef HAVE_FCHOWNAT
3023 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
3024 result = fchownat(dir_fd, path.narrow, uid, gid,
3025 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3026 else
3027#endif
3028 result = chown(path.narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003029 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003030
3031 if (result) {
3032 return_value = path_posix_error("chown", &path);
3033 goto exit;
3034 }
3035
3036 return_value = Py_None;
Victor Stinner8c62be82010-05-06 00:08:46 +00003037 Py_INCREF(Py_None);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003038
3039exit:
3040 path_cleanup(&path);
3041 return return_value;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003042}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003043#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003044
Christian Heimes4e30a842007-11-30 22:12:06 +00003045#ifdef HAVE_FCHOWN
3046PyDoc_STRVAR(posix_fchown__doc__,
3047"fchown(fd, uid, gid)\n\n\
3048Change the owner and group id of the file given by file descriptor\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07003049fd to the numeric uid and gid. Equivalent to os.chown(fd, uid, gid).");
Christian Heimes4e30a842007-11-30 22:12:06 +00003050
3051static PyObject *
3052posix_fchown(PyObject *self, PyObject *args)
3053{
Victor Stinner8c62be82010-05-06 00:08:46 +00003054 int fd;
3055 long uid, gid;
3056 int res;
Victor Stinner26de69d2011-06-17 15:15:38 +02003057 if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00003058 return NULL;
3059 Py_BEGIN_ALLOW_THREADS
3060 res = fchown(fd, (uid_t) uid, (gid_t) gid);
3061 Py_END_ALLOW_THREADS
3062 if (res < 0)
3063 return posix_error();
3064 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003065}
3066#endif /* HAVE_FCHOWN */
3067
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003068#ifdef HAVE_LCHOWN
3069PyDoc_STRVAR(posix_lchown__doc__,
3070"lchown(path, uid, gid)\n\n\
3071Change the owner and group id of path to the numeric uid and gid.\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07003072This function will not follow symbolic links.\n\
3073Equivalent to os.chown(path, uid, gid, follow_symlinks=False).");
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003074
3075static PyObject *
3076posix_lchown(PyObject *self, PyObject *args)
3077{
Victor Stinner8c62be82010-05-06 00:08:46 +00003078 PyObject *opath;
3079 char *path;
3080 long uid, gid;
3081 int res;
3082 if (!PyArg_ParseTuple(args, "O&ll:lchown",
3083 PyUnicode_FSConverter, &opath,
3084 &uid, &gid))
3085 return NULL;
3086 path = PyBytes_AsString(opath);
3087 Py_BEGIN_ALLOW_THREADS
3088 res = lchown(path, (uid_t) uid, (gid_t) gid);
3089 Py_END_ALLOW_THREADS
3090 if (res < 0)
3091 return posix_error_with_allocated_filename(opath);
3092 Py_DECREF(opath);
3093 Py_INCREF(Py_None);
3094 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003095}
3096#endif /* HAVE_LCHOWN */
3097
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003098
Guido van Rossum36bc6801995-06-14 22:54:23 +00003099#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00003100static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003101posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003102{
Victor Stinner8c62be82010-05-06 00:08:46 +00003103 char buf[1026];
3104 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003105
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003106#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003107 if (!use_bytes) {
3108 wchar_t wbuf[1026];
3109 wchar_t *wbuf2 = wbuf;
3110 PyObject *resobj;
3111 DWORD len;
3112 Py_BEGIN_ALLOW_THREADS
3113 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
3114 /* If the buffer is large enough, len does not include the
3115 terminating \0. If the buffer is too small, len includes
3116 the space needed for the terminator. */
3117 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
3118 wbuf2 = malloc(len * sizeof(wchar_t));
3119 if (wbuf2)
3120 len = GetCurrentDirectoryW(len, wbuf2);
3121 }
3122 Py_END_ALLOW_THREADS
3123 if (!wbuf2) {
3124 PyErr_NoMemory();
3125 return NULL;
3126 }
3127 if (!len) {
3128 if (wbuf2 != wbuf) free(wbuf2);
3129 return win32_error("getcwdu", NULL);
3130 }
3131 resobj = PyUnicode_FromWideChar(wbuf2, len);
3132 if (wbuf2 != wbuf) free(wbuf2);
3133 return resobj;
3134 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003135
3136 if (win32_warn_bytes_api())
3137 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003138#endif
3139
Victor Stinner8c62be82010-05-06 00:08:46 +00003140 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003141#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003142 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003143#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003144 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003145#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003146 Py_END_ALLOW_THREADS
3147 if (res == NULL)
3148 return posix_error();
3149 if (use_bytes)
3150 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00003151 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003152}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003153
3154PyDoc_STRVAR(posix_getcwd__doc__,
3155"getcwd() -> path\n\n\
3156Return a unicode string representing the current working directory.");
3157
3158static PyObject *
3159posix_getcwd_unicode(PyObject *self)
3160{
3161 return posix_getcwd(0);
3162}
3163
3164PyDoc_STRVAR(posix_getcwdb__doc__,
3165"getcwdb() -> path\n\n\
3166Return a bytes string representing the current working directory.");
3167
3168static PyObject *
3169posix_getcwd_bytes(PyObject *self)
3170{
3171 return posix_getcwd(1);
3172}
Guido van Rossum36bc6801995-06-14 22:54:23 +00003173#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003174
Larry Hastings9cf065c2012-06-22 16:30:09 -07003175#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3176#define HAVE_LINK 1
3177#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003178
Guido van Rossumb6775db1994-08-01 11:34:53 +00003179#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003180PyDoc_STRVAR(posix_link__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003181"link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True)\n\n\
3182Create a hard link to a file.\n\
3183\n\
3184If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\
3185 descriptor open to a directory, and the respective path string (src or dst)\n\
3186 should be relative; the path will then be relative to that directory.\n\
3187If follow_symlinks is False, and the last element of src is a symbolic\n\
3188 link, link will create a link to the symbolic link itself instead of the\n\
3189 file the link points to.\n\
3190src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your\n\
3191 platform. If they are unavailable, using them will raise a\n\
3192 NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003193
Barry Warsaw53699e91996-12-10 23:23:01 +00003194static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07003195posix_link(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003196{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003197 path_t src, dst;
3198 int src_dir_fd = DEFAULT_DIR_FD;
3199 int dst_dir_fd = DEFAULT_DIR_FD;
3200 int follow_symlinks = 1;
3201 PyObject *return_value = NULL;
3202 static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd",
3203 "follow_symlinks", NULL};
3204#ifdef MS_WINDOWS
3205 BOOL result;
3206#else
3207 int result;
3208#endif
3209
3210 memset(&src, 0, sizeof(src));
3211 memset(&dst, 0, sizeof(dst));
3212 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|O&O&p:link", keywords,
3213 path_converter, &src,
3214 path_converter, &dst,
3215 dir_fd_converter, &src_dir_fd,
3216 dir_fd_converter, &dst_dir_fd,
3217 &follow_symlinks))
3218 return NULL;
3219
3220#ifndef HAVE_LINKAT
3221 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3222 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
3223 goto exit;
3224 }
3225#endif
3226
3227 if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) {
3228 PyErr_SetString(PyExc_NotImplementedError,
3229 "link: src and dst must be the same type");
3230 goto exit;
3231 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003232
Brian Curtin1b9df392010-11-24 20:24:31 +00003233#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003234 Py_BEGIN_ALLOW_THREADS
3235 if (src.wide)
3236 result = CreateHardLinkW(dst.wide, src.wide, NULL);
3237 else
3238 result = CreateHardLinkA(dst.narrow, src.narrow, NULL);
3239 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003240
Larry Hastings9cf065c2012-06-22 16:30:09 -07003241 if (!result) {
3242 return_value = win32_error_object("link", dst.object);
3243 goto exit;
Brian Curtinfc889c42010-11-28 23:59:46 +00003244 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003245#else
3246 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003247#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003248 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3249 (dst_dir_fd != DEFAULT_DIR_FD) ||
3250 (!follow_symlinks))
3251 result = linkat(src_dir_fd, src.narrow,
3252 dst_dir_fd, dst.narrow,
3253 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3254 else
3255#endif
3256 result = link(src.narrow, dst.narrow);
3257 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003258
Larry Hastings9cf065c2012-06-22 16:30:09 -07003259 if (result) {
3260 return_value = path_error("link", &dst);
3261 goto exit;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003262 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003263#endif
3264
3265 return_value = Py_None;
3266 Py_INCREF(Py_None);
3267
3268exit:
3269 path_cleanup(&src);
3270 path_cleanup(&dst);
3271 return return_value;
Brian Curtin1b9df392010-11-24 20:24:31 +00003272}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003273#endif
3274
Brian Curtin1b9df392010-11-24 20:24:31 +00003275
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003276
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003277PyDoc_STRVAR(posix_listdir__doc__,
Larry Hastingsfdaea062012-06-25 04:42:23 -07003278"listdir(path='.') -> list_of_filenames\n\n\
3279Return a list containing the names of the files in the directory.\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003280The list is in arbitrary order. It does not include the special\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07003281entries '.' and '..' even if they are present in the directory.\n\
3282\n\
Larry Hastingsfdaea062012-06-25 04:42:23 -07003283path can be specified as either str or bytes. If path is bytes,\n\
3284 the filenames returned will also be bytes; in all other circumstances\n\
3285 the filenames returned will be str.\n\
3286On some platforms, path may also be specified as an open file descriptor;\n\
3287 the file descriptor must refer to a directory.\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07003288 If this functionality is unavailable, using it raises NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003289
Barry Warsaw53699e91996-12-10 23:23:01 +00003290static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07003291posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003292{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003293 path_t path;
3294 PyObject *list = NULL;
3295 static char *keywords[] = {"path", NULL};
3296 int fd = -1;
3297
3298#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3299 PyObject *v;
3300 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3301 BOOL result;
3302 WIN32_FIND_DATA FileData;
3303 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
3304 char *bufptr = namebuf;
3305 /* only claim to have space for MAX_PATH */
3306 Py_ssize_t len = sizeof(namebuf)-5;
3307 PyObject *po = NULL;
3308 wchar_t *wnamebuf = NULL;
3309#elif defined(PYOS_OS2)
3310#ifndef MAX_PATH
3311#define MAX_PATH CCHMAXPATH
3312#endif
3313 char *pt;
3314 PyObject *v;
3315 char namebuf[MAX_PATH+5];
3316 HDIR hdir = 1;
3317 ULONG srchcnt = 1;
3318 FILEFINDBUF3 ep;
3319 APIRET rc;
3320#else
3321 PyObject *v;
3322 DIR *dirp = NULL;
3323 struct dirent *ep;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003324 int return_str; /* if false, return bytes */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003325#endif
3326
3327 memset(&path, 0, sizeof(path));
3328 path.nullable = 1;
3329#ifdef HAVE_FDOPENDIR
3330 path.allow_fd = 1;
3331 path.fd = -1;
3332#endif
3333 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:listdir", keywords,
3334 path_converter, &path
3335 ))
3336 return NULL;
3337
Victor Stinner8c62be82010-05-06 00:08:46 +00003338 /* XXX Should redo this putting the (now four) versions of opendir
3339 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003340#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003341 if (!path.narrow) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003342 WIN32_FIND_DATAW wFileData;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003343 wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003344
Larry Hastings9cf065c2012-06-22 16:30:09 -07003345 if (!path.wide) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00003346 po_wchars = L".";
3347 len = 1;
3348 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003349 po_wchars = path.wide;
3350 len = wcslen(path.wide);
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00003351 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003352 /* The +5 is so we can append "\\*.*\0" */
Victor Stinner8c62be82010-05-06 00:08:46 +00003353 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
3354 if (!wnamebuf) {
3355 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003356 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003357 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00003358 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003359 if (len > 0) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003360 wchar_t wch = wnamebuf[len-1];
Victor Stinner8c62be82010-05-06 00:08:46 +00003361 if (wch != L'/' && wch != L'\\' && wch != L':')
3362 wnamebuf[len++] = L'\\';
3363 wcscpy(wnamebuf + len, L"*.*");
3364 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003365 if ((list = PyList_New(0)) == NULL) {
3366 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003367 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00003368 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003369 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003370 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003371 if (hFindFile == INVALID_HANDLE_VALUE) {
3372 int error = GetLastError();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003373 if (error == ERROR_FILE_NOT_FOUND)
3374 goto exit;
3375 Py_DECREF(list);
3376 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003377 win32_error_unicode("FindFirstFileW", wnamebuf);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003378 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003379 }
3380 do {
3381 /* Skip over . and .. */
3382 if (wcscmp(wFileData.cFileName, L".") != 0 &&
3383 wcscmp(wFileData.cFileName, L"..") != 0) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003384 v = PyUnicode_FromWideChar(wFileData.cFileName,
3385 wcslen(wFileData.cFileName));
Victor Stinner8c62be82010-05-06 00:08:46 +00003386 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003387 Py_DECREF(list);
3388 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003389 break;
3390 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003391 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003392 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003393 Py_DECREF(list);
3394 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003395 break;
3396 }
3397 Py_DECREF(v);
3398 }
3399 Py_BEGIN_ALLOW_THREADS
3400 result = FindNextFileW(hFindFile, &wFileData);
3401 Py_END_ALLOW_THREADS
3402 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3403 it got to the end of the directory. */
3404 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003405 Py_DECREF(list);
3406 list = win32_error_unicode("FindNextFileW", wnamebuf);
3407 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003408 }
3409 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003410
Larry Hastings9cf065c2012-06-22 16:30:09 -07003411 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003412 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003413 strcpy(namebuf, path.narrow);
3414 len = path.length;
Victor Stinner8c62be82010-05-06 00:08:46 +00003415 if (len > 0) {
3416 char ch = namebuf[len-1];
3417 if (ch != SEP && ch != ALTSEP && ch != ':')
3418 namebuf[len++] = '/';
3419 strcpy(namebuf + len, "*.*");
3420 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00003421
Larry Hastings9cf065c2012-06-22 16:30:09 -07003422 if ((list = PyList_New(0)) == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00003423 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003424
Antoine Pitroub73caab2010-08-09 23:39:31 +00003425 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003426 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003427 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003428 if (hFindFile == INVALID_HANDLE_VALUE) {
3429 int error = GetLastError();
3430 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003431 goto exit;
3432 Py_DECREF(list);
3433 list = win32_error("FindFirstFile", namebuf);
3434 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003435 }
3436 do {
3437 /* Skip over . and .. */
3438 if (strcmp(FileData.cFileName, ".") != 0 &&
3439 strcmp(FileData.cFileName, "..") != 0) {
3440 v = PyBytes_FromString(FileData.cFileName);
3441 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003442 Py_DECREF(list);
3443 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003444 break;
3445 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003446 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003447 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003448 Py_DECREF(list);
3449 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003450 break;
3451 }
3452 Py_DECREF(v);
3453 }
3454 Py_BEGIN_ALLOW_THREADS
3455 result = FindNextFile(hFindFile, &FileData);
3456 Py_END_ALLOW_THREADS
3457 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3458 it got to the end of the directory. */
3459 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003460 Py_DECREF(list);
3461 list = win32_error("FindNextFile", namebuf);
3462 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003463 }
3464 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003465
Larry Hastings9cf065c2012-06-22 16:30:09 -07003466exit:
3467 if (hFindFile != INVALID_HANDLE_VALUE) {
3468 if (FindClose(hFindFile) == FALSE) {
3469 if (list != NULL) {
3470 Py_DECREF(list);
3471 list = win32_error_object("FindClose", path.object);
3472 }
3473 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003474 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003475 if (wnamebuf)
3476 free(wnamebuf);
3477 path_cleanup(&path);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003478
Larry Hastings9cf065c2012-06-22 16:30:09 -07003479 return list;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003480
Tim Peters0bb44a42000-09-15 07:44:49 +00003481#elif defined(PYOS_OS2)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003482 if (path.length >= MAX_PATH) {
Neal Norwitz6c913782007-10-14 03:23:09 +00003483 PyErr_SetString(PyExc_ValueError, "path too long");
Larry Hastings9cf065c2012-06-22 16:30:09 -07003484 goto exit;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003485 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003486 strcpy(namebuf, path.narrow);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003487 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003488 if (*pt == ALTSEP)
3489 *pt = SEP;
3490 if (namebuf[len-1] != SEP)
3491 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003492 strcpy(namebuf + len, "*.*");
3493
Larry Hastings9cf065c2012-06-22 16:30:09 -07003494 if ((list = PyList_New(0)) == NULL) {
3495 goto exit;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00003496 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003497
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003498 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
3499 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003500 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003501 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
3502 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
3503 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003504
3505 if (rc != NO_ERROR) {
3506 errno = ENOENT;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003507 Py_DECREF(list);
3508 list = posix_error_with_filename(path.narrow);
3509 goto exit;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003510 }
3511
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003512 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003513 do {
3514 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003515 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003516 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003517
3518 strcpy(namebuf, ep.achName);
3519
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003520 /* Leave Case of Name Alone -- In Native Form */
3521 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003522
Christian Heimes72b710a2008-05-26 13:28:38 +00003523 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003524 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003525 Py_DECREF(list);
3526 list = NULL;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003527 break;
3528 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003529 if (PyList_Append(list, v) != 0) {
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003530 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003531 Py_DECREF(list);
3532 list = NULL;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003533 break;
3534 }
3535 Py_DECREF(v);
3536 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
3537 }
3538
Larry Hastings9cf065c2012-06-22 16:30:09 -07003539exit:
3540 path_cleanup(&path);
3541
3542 return list;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003543#else
Just van Rossum96b1c902003-03-03 17:32:15 +00003544
Victor Stinner8c62be82010-05-06 00:08:46 +00003545 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003546#ifdef HAVE_FDOPENDIR
3547 if (path.fd != -1) {
3548 /* closedir() closes the FD, so we duplicate it */
Antoine Pitroud3ccde82010-09-04 17:21:57 +00003549 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003550 fd = dup(path.fd);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00003551 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003552
3553 if (fd == -1) {
3554 list = posix_error();
3555 goto exit;
3556 }
3557
Larry Hastingsfdaea062012-06-25 04:42:23 -07003558 return_str = 1;
3559
Larry Hastings9cf065c2012-06-22 16:30:09 -07003560 Py_BEGIN_ALLOW_THREADS
3561 dirp = fdopendir(fd);
3562 Py_END_ALLOW_THREADS
3563 }
3564 else
3565#endif
3566 {
Larry Hastingsfdaea062012-06-25 04:42:23 -07003567 char *name;
3568 if (path.narrow) {
3569 name = path.narrow;
3570 /* only return bytes if they specified a bytes object */
3571 return_str = !(PyBytes_Check(path.object));
3572 }
3573 else {
3574 name = ".";
3575 return_str = 1;
3576 }
3577
Larry Hastings9cf065c2012-06-22 16:30:09 -07003578 Py_BEGIN_ALLOW_THREADS
3579 dirp = opendir(name);
3580 Py_END_ALLOW_THREADS
3581 }
3582
3583 if (dirp == NULL) {
3584 list = path_error("listdir", &path);
3585 goto exit;
3586 }
3587 if ((list = PyList_New(0)) == NULL) {
3588 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003589 }
3590 for (;;) {
3591 errno = 0;
3592 Py_BEGIN_ALLOW_THREADS
3593 ep = readdir(dirp);
3594 Py_END_ALLOW_THREADS
3595 if (ep == NULL) {
3596 if (errno == 0) {
3597 break;
3598 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003599 Py_DECREF(list);
3600 list = path_error("listdir", &path);
3601 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003602 }
3603 }
3604 if (ep->d_name[0] == '.' &&
3605 (NAMLEN(ep) == 1 ||
3606 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3607 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003608 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00003609 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3610 else
3611 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003612 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003613 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003614 break;
3615 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003616 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003617 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003618 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003619 break;
3620 }
3621 Py_DECREF(v);
3622 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003623
Larry Hastings9cf065c2012-06-22 16:30:09 -07003624exit:
3625 if (dirp != NULL) {
3626 Py_BEGIN_ALLOW_THREADS
3627 if (fd > -1)
3628 rewinddir(dirp);
3629 closedir(dirp);
3630 Py_END_ALLOW_THREADS
3631 }
3632
3633 path_cleanup(&path);
3634
3635 return list;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003636
Tim Peters0bb44a42000-09-15 07:44:49 +00003637#endif /* which OS */
3638} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003639
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003640#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003641/* A helper function for abspath on win32 */
3642static PyObject *
3643posix__getfullpathname(PyObject *self, PyObject *args)
3644{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003645 const char *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00003646 char outbuf[MAX_PATH*2];
3647 char *temp;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003648 PyObject *po;
3649
3650 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po))
3651 {
3652 wchar_t *wpath;
3653 wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
3654 wchar_t *wtemp;
Victor Stinner8c62be82010-05-06 00:08:46 +00003655 DWORD result;
3656 PyObject *v;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003657
3658 wpath = PyUnicode_AsUnicode(po);
3659 if (wpath == NULL)
3660 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003661 result = GetFullPathNameW(wpath,
Victor Stinner63941882011-09-29 00:42:28 +02003662 Py_ARRAY_LENGTH(woutbuf),
Victor Stinner8c62be82010-05-06 00:08:46 +00003663 woutbuf, &wtemp);
Victor Stinner63941882011-09-29 00:42:28 +02003664 if (result > Py_ARRAY_LENGTH(woutbuf)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003665 woutbufp = malloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003666 if (!woutbufp)
3667 return PyErr_NoMemory();
3668 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
3669 }
3670 if (result)
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003671 v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp));
Victor Stinner8c62be82010-05-06 00:08:46 +00003672 else
Victor Stinnereb5657a2011-09-30 01:44:27 +02003673 v = win32_error_object("GetFullPathNameW", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00003674 if (woutbufp != woutbuf)
3675 free(woutbufp);
3676 return v;
3677 }
3678 /* Drop the argument parsing error as narrow strings
3679 are also valid. */
3680 PyErr_Clear();
Victor Stinnereb5657a2011-09-30 01:44:27 +02003681
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003682 if (!PyArg_ParseTuple (args, "y:_getfullpathname",
3683 &path))
Victor Stinner8c62be82010-05-06 00:08:46 +00003684 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003685 if (win32_warn_bytes_api())
3686 return NULL;
Victor Stinner63941882011-09-29 00:42:28 +02003687 if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf),
Victor Stinner8c62be82010-05-06 00:08:46 +00003688 outbuf, &temp)) {
3689 win32_error("GetFullPathName", path);
Victor Stinner8c62be82010-05-06 00:08:46 +00003690 return NULL;
3691 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003692 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
3693 return PyUnicode_Decode(outbuf, strlen(outbuf),
3694 Py_FileSystemDefaultEncoding, NULL);
3695 }
3696 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00003697} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00003698
Brian Curtind25aef52011-06-13 15:16:04 -05003699
Brian Curtinf5e76d02010-11-24 13:14:05 +00003700
Brian Curtind40e6f72010-07-08 21:39:08 +00003701/* A helper function for samepath on windows */
3702static PyObject *
3703posix__getfinalpathname(PyObject *self, PyObject *args)
3704{
3705 HANDLE hFile;
3706 int buf_size;
3707 wchar_t *target_path;
3708 int result_length;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003709 PyObject *po, *result;
Brian Curtind40e6f72010-07-08 21:39:08 +00003710 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003711
Victor Stinnereb5657a2011-09-30 01:44:27 +02003712 if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po))
Brian Curtind40e6f72010-07-08 21:39:08 +00003713 return NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003714 path = PyUnicode_AsUnicode(po);
3715 if (path == NULL)
3716 return NULL;
Brian Curtind40e6f72010-07-08 21:39:08 +00003717
3718 if(!check_GetFinalPathNameByHandle()) {
3719 /* If the OS doesn't have GetFinalPathNameByHandle, return a
3720 NotImplementedError. */
3721 return PyErr_Format(PyExc_NotImplementedError,
3722 "GetFinalPathNameByHandle not available on this platform");
3723 }
3724
3725 hFile = CreateFileW(
3726 path,
3727 0, /* desired access */
3728 0, /* share mode */
3729 NULL, /* security attributes */
3730 OPEN_EXISTING,
3731 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3732 FILE_FLAG_BACKUP_SEMANTICS,
3733 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003734
Victor Stinnereb5657a2011-09-30 01:44:27 +02003735 if(hFile == INVALID_HANDLE_VALUE)
3736 return win32_error_object("CreateFileW", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003737
3738 /* We have a good handle to the target, use it to determine the
3739 target path name. */
3740 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
3741
3742 if(!buf_size)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003743 return win32_error_object("GetFinalPathNameByHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003744
3745 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
3746 if(!target_path)
3747 return PyErr_NoMemory();
3748
3749 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
3750 buf_size, VOLUME_NAME_DOS);
3751 if(!result_length)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003752 return win32_error_object("GetFinalPathNamyByHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003753
3754 if(!CloseHandle(hFile))
Victor Stinnereb5657a2011-09-30 01:44:27 +02003755 return win32_error_object("CloseHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003756
3757 target_path[result_length] = 0;
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003758 result = PyUnicode_FromWideChar(target_path, result_length);
Brian Curtind40e6f72010-07-08 21:39:08 +00003759 free(target_path);
3760 return result;
3761
3762} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00003763
3764static PyObject *
3765posix__getfileinformation(PyObject *self, PyObject *args)
3766{
3767 HANDLE hFile;
3768 BY_HANDLE_FILE_INFORMATION info;
3769 int fd;
3770
3771 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
3772 return NULL;
3773
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003774 if (!_PyVerify_fd(fd))
3775 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003776
3777 hFile = (HANDLE)_get_osfhandle(fd);
3778 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003779 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003780
3781 if (!GetFileInformationByHandle(hFile, &info))
3782 return win32_error("_getfileinformation", NULL);
3783
3784 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
3785 info.nFileIndexHigh,
3786 info.nFileIndexLow);
3787}
Brian Curtin9c669cc2011-06-08 18:17:18 -05003788
Brian Curtin95d028f2011-06-09 09:10:38 -05003789PyDoc_STRVAR(posix__isdir__doc__,
3790"Return true if the pathname refers to an existing directory.");
3791
Brian Curtin9c669cc2011-06-08 18:17:18 -05003792static PyObject *
3793posix__isdir(PyObject *self, PyObject *args)
3794{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003795 const char *path;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003796 PyObject *po;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003797 DWORD attributes;
3798
3799 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003800 wchar_t *wpath = PyUnicode_AsUnicode(po);
3801 if (wpath == NULL)
3802 return NULL;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003803
3804 attributes = GetFileAttributesW(wpath);
3805 if (attributes == INVALID_FILE_ATTRIBUTES)
3806 Py_RETURN_FALSE;
3807 goto check;
3808 }
3809 /* Drop the argument parsing error as narrow strings
3810 are also valid. */
3811 PyErr_Clear();
3812
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003813 if (!PyArg_ParseTuple(args, "y:_isdir", &path))
Brian Curtin9c669cc2011-06-08 18:17:18 -05003814 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003815 if (win32_warn_bytes_api())
3816 return NULL;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003817 attributes = GetFileAttributesA(path);
3818 if (attributes == INVALID_FILE_ATTRIBUTES)
3819 Py_RETURN_FALSE;
3820
3821check:
3822 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3823 Py_RETURN_TRUE;
3824 else
3825 Py_RETURN_FALSE;
3826}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003827#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003828
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003829PyDoc_STRVAR(posix_mkdir__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003830"mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
3831Create a directory.\n\
3832\n\
3833If dir_fd is not None, it should be a file descriptor open to a directory,\n\
3834 and path should be relative; path will then be relative to that directory.\n\
3835dir_fd may not be implemented on your platform.\n\
3836 If it is unavailable, using it will raise a NotImplementedError.\n\
3837\n\
3838The mode argument is ignored on Windows.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003839
Barry Warsaw53699e91996-12-10 23:23:01 +00003840static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07003841posix_mkdir(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003842{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003843 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00003844 int mode = 0777;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003845 int dir_fd = DEFAULT_DIR_FD;
3846 static char *keywords[] = {"path", "mode", "dir_fd", NULL};
3847 PyObject *return_value = NULL;
3848 int result;
3849
3850 memset(&path, 0, sizeof(path));
3851 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkdir", keywords,
3852 path_converter, &path, &mode,
3853#ifdef HAVE_MKDIRAT
3854 dir_fd_converter, &dir_fd
3855#else
3856 dir_fd_unavailable, &dir_fd
3857#endif
3858 ))
3859 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003860
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003861#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003862 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003863 if (path.wide)
3864 result = CreateDirectoryW(path.wide, NULL);
3865 else
3866 result = CreateDirectoryA(path.narrow, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00003867 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003868
Larry Hastings9cf065c2012-06-22 16:30:09 -07003869 if (!result) {
3870 return_value = win32_error_object("mkdir", path.object);
3871 goto exit;
3872 }
3873#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003874 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003875#if HAVE_MKDIRAT
3876 if (dir_fd != DEFAULT_DIR_FD)
3877 result = mkdirat(dir_fd, path.narrow, mode);
3878 else
3879#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00003880#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003881 result = mkdir(path.narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003882#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07003883 result = mkdir(path.narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003884#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003885 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003886 if (result < 0) {
3887 return_value = path_error("mkdir", &path);
3888 goto exit;
3889 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003890#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003891 return_value = Py_None;
3892 Py_INCREF(Py_None);
3893exit:
3894 path_cleanup(&path);
3895 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003896}
3897
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003898
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003899/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3900#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003901#include <sys/resource.h>
3902#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003903
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003904
3905#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003906PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003907"nice(inc) -> new_priority\n\n\
3908Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003909
Barry Warsaw53699e91996-12-10 23:23:01 +00003910static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003911posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003912{
Victor Stinner8c62be82010-05-06 00:08:46 +00003913 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003914
Victor Stinner8c62be82010-05-06 00:08:46 +00003915 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3916 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003917
Victor Stinner8c62be82010-05-06 00:08:46 +00003918 /* There are two flavours of 'nice': one that returns the new
3919 priority (as required by almost all standards out there) and the
3920 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3921 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003922
Victor Stinner8c62be82010-05-06 00:08:46 +00003923 If we are of the nice family that returns the new priority, we
3924 need to clear errno before the call, and check if errno is filled
3925 before calling posix_error() on a returnvalue of -1, because the
3926 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003927
Victor Stinner8c62be82010-05-06 00:08:46 +00003928 errno = 0;
3929 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003930#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003931 if (value == 0)
3932 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003933#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003934 if (value == -1 && errno != 0)
3935 /* either nice() or getpriority() returned an error */
3936 return posix_error();
3937 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003938}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003939#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003940
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003941
3942#ifdef HAVE_GETPRIORITY
3943PyDoc_STRVAR(posix_getpriority__doc__,
3944"getpriority(which, who) -> current_priority\n\n\
3945Get program scheduling priority.");
3946
3947static PyObject *
3948posix_getpriority(PyObject *self, PyObject *args)
3949{
3950 int which, who, retval;
3951
3952 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3953 return NULL;
3954 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003955 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003956 if (errno != 0)
3957 return posix_error();
3958 return PyLong_FromLong((long)retval);
3959}
3960#endif /* HAVE_GETPRIORITY */
3961
3962
3963#ifdef HAVE_SETPRIORITY
3964PyDoc_STRVAR(posix_setpriority__doc__,
3965"setpriority(which, who, prio) -> None\n\n\
3966Set program scheduling priority.");
3967
3968static PyObject *
3969posix_setpriority(PyObject *self, PyObject *args)
3970{
3971 int which, who, prio, retval;
3972
3973 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3974 return NULL;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003975 retval = setpriority(which, who, prio);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003976 if (retval == -1)
3977 return posix_error();
3978 Py_RETURN_NONE;
3979}
3980#endif /* HAVE_SETPRIORITY */
3981
3982
Barry Warsaw53699e91996-12-10 23:23:01 +00003983static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07003984internal_rename(PyObject *args, PyObject *kwargs, int is_replace)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003985{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003986 char *function_name = is_replace ? "replace" : "rename";
3987 path_t src;
3988 path_t dst;
3989 int src_dir_fd = DEFAULT_DIR_FD;
3990 int dst_dir_fd = DEFAULT_DIR_FD;
3991 int dir_fd_specified;
3992 PyObject *return_value = NULL;
3993 char format[24];
3994 static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL};
3995
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003996#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003997 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003998 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003999#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004000 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004001#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004002
4003 memset(&src, 0, sizeof(src));
4004 memset(&dst, 0, sizeof(dst));
4005 strcpy(format, "O&O&|$O&O&:");
4006 strcat(format, function_name);
4007 if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, keywords,
4008 path_converter, &src,
4009 path_converter, &dst,
4010 dir_fd_converter, &src_dir_fd,
4011 dir_fd_converter, &dst_dir_fd))
4012 return NULL;
4013
4014 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
4015 (dst_dir_fd != DEFAULT_DIR_FD);
4016#ifndef HAVE_RENAMEAT
4017 if (dir_fd_specified) {
4018 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
4019 goto exit;
4020 }
4021#endif
4022
4023 if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) {
4024 PyErr_Format(PyExc_ValueError,
4025 "%s: src and dst must be the same type", function_name);
4026 goto exit;
4027 }
4028
4029#ifdef MS_WINDOWS
4030 Py_BEGIN_ALLOW_THREADS
4031 if (src.wide)
4032 result = MoveFileExW(src.wide, dst.wide, flags);
4033 else
4034 result = MoveFileExA(src.narrow, dst.narrow, flags);
4035 Py_END_ALLOW_THREADS
4036
4037 if (!result) {
4038 return_value = win32_error_object(function_name, dst.object);
4039 goto exit;
4040 }
4041
4042#else
4043 Py_BEGIN_ALLOW_THREADS
4044#ifdef HAVE_RENAMEAT
4045 if (dir_fd_specified)
4046 result = renameat(src_dir_fd, src.narrow, dst_dir_fd, dst.narrow);
4047 else
4048#endif
4049 result = rename(src.narrow, dst.narrow);
4050 Py_END_ALLOW_THREADS
4051
4052 if (result) {
4053 return_value = path_error(function_name, &dst);
4054 goto exit;
4055 }
4056#endif
4057
4058 Py_INCREF(Py_None);
4059 return_value = Py_None;
4060exit:
4061 path_cleanup(&src);
4062 path_cleanup(&dst);
4063 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004064}
4065
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004066PyDoc_STRVAR(posix_rename__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004067"rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\
4068Rename a file or directory.\n\
4069\n\
4070If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\
4071 descriptor open to a directory, and the respective path string (src or dst)\n\
4072 should be relative; the path will then be relative to that directory.\n\
4073src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\
4074 If they are unavailable, using them will raise a NotImplementedError.");
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004075
4076static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07004077posix_rename(PyObject *self, PyObject *args, PyObject *kwargs)
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004078{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004079 return internal_rename(args, kwargs, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004080}
4081
4082PyDoc_STRVAR(posix_replace__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004083"replace(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\
4084Rename a file or directory, overwriting the destination.\n\
4085\n\
4086If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\
4087 descriptor open to a directory, and the respective path string (src or dst)\n\
4088 should be relative; the path will then be relative to that directory.\n\
4089src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\
4090 If they are unavailable, using them will raise a NotImplementedError.");
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004091
4092static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07004093posix_replace(PyObject *self, PyObject *args, PyObject *kwargs)
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004094{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004095 return internal_rename(args, kwargs, 1);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004096}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004097
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004098PyDoc_STRVAR(posix_rmdir__doc__,
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004099"rmdir(path, *, dir_fd=None)\n\n\
4100Remove a directory.\n\
4101\n\
4102If dir_fd is not None, it should be a file descriptor open to a directory,\n\
4103 and path should be relative; path will then be relative to that directory.\n\
4104dir_fd may not be implemented on your platform.\n\
4105 If it is unavailable, using it will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004106
Barry Warsaw53699e91996-12-10 23:23:01 +00004107static PyObject *
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004108posix_rmdir(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004109{
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004110 path_t path;
4111 int dir_fd = DEFAULT_DIR_FD;
4112 static char *keywords[] = {"path", "dir_fd", NULL};
4113 int result;
4114 PyObject *return_value = NULL;
4115
4116 memset(&path, 0, sizeof(path));
4117 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:rmdir", keywords,
4118 path_converter, &path,
4119#ifdef HAVE_UNLINKAT
4120 dir_fd_converter, &dir_fd
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004121#else
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004122 dir_fd_unavailable, &dir_fd
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004123#endif
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004124 ))
4125 return NULL;
4126
4127 Py_BEGIN_ALLOW_THREADS
4128#ifdef MS_WINDOWS
4129 if (path.wide)
4130 result = RemoveDirectoryW(path.wide);
4131 else
4132 result = RemoveDirectoryA(path.narrow);
4133 result = !result; /* Windows, success=1, UNIX, success=0 */
4134#else
4135#ifdef HAVE_UNLINKAT
4136 if (dir_fd != DEFAULT_DIR_FD)
4137 result = unlinkat(dir_fd, path.narrow, AT_REMOVEDIR);
4138 else
4139#endif
4140 result = rmdir(path.narrow);
4141#endif
4142 Py_END_ALLOW_THREADS
4143
4144 if (result) {
4145 return_value = path_error("rmdir", &path);
4146 goto exit;
4147 }
4148
4149 return_value = Py_None;
4150 Py_INCREF(Py_None);
4151
4152exit:
4153 path_cleanup(&path);
4154 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004155}
4156
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004157
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004158#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004159PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004160"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004161Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004162
Barry Warsaw53699e91996-12-10 23:23:01 +00004163static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004164posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004165{
Victor Stinner8c62be82010-05-06 00:08:46 +00004166 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00004167#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004168 wchar_t *command;
4169 if (!PyArg_ParseTuple(args, "u:system", &command))
4170 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00004171
Victor Stinner8c62be82010-05-06 00:08:46 +00004172 Py_BEGIN_ALLOW_THREADS
4173 sts = _wsystem(command);
4174 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00004175#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004176 PyObject *command_obj;
4177 char *command;
4178 if (!PyArg_ParseTuple(args, "O&:system",
4179 PyUnicode_FSConverter, &command_obj))
4180 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00004181
Victor Stinner8c62be82010-05-06 00:08:46 +00004182 command = PyBytes_AsString(command_obj);
4183 Py_BEGIN_ALLOW_THREADS
4184 sts = system(command);
4185 Py_END_ALLOW_THREADS
4186 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00004187#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004188 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004189}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004190#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004191
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004192
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004193PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004194"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004195Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004196
Barry Warsaw53699e91996-12-10 23:23:01 +00004197static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004198posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004199{
Victor Stinner8c62be82010-05-06 00:08:46 +00004200 int i;
4201 if (!PyArg_ParseTuple(args, "i:umask", &i))
4202 return NULL;
4203 i = (int)umask(i);
4204 if (i < 0)
4205 return posix_error();
4206 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004207}
4208
Brian Curtind40e6f72010-07-08 21:39:08 +00004209#ifdef MS_WINDOWS
4210
4211/* override the default DeleteFileW behavior so that directory
4212symlinks can be removed with this function, the same as with
4213Unix symlinks */
4214BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4215{
4216 WIN32_FILE_ATTRIBUTE_DATA info;
4217 WIN32_FIND_DATAW find_data;
4218 HANDLE find_data_handle;
4219 int is_directory = 0;
4220 int is_link = 0;
4221
4222 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4223 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004224
Brian Curtind40e6f72010-07-08 21:39:08 +00004225 /* Get WIN32_FIND_DATA structure for the path to determine if
4226 it is a symlink */
4227 if(is_directory &&
4228 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4229 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4230
4231 if(find_data_handle != INVALID_HANDLE_VALUE) {
4232 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
4233 FindClose(find_data_handle);
4234 }
4235 }
4236 }
4237
4238 if (is_directory && is_link)
4239 return RemoveDirectoryW(lpFileName);
4240
4241 return DeleteFileW(lpFileName);
4242}
4243#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004244
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004245PyDoc_STRVAR(posix_unlink__doc__,
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004246"unlink(path, *, dir_fd=None)\n\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07004247Remove a file (same as remove()).\n\
4248\n\
4249If dir_fd is not None, it should be a file descriptor open to a directory,\n\
4250 and path should be relative; path will then be relative to that directory.\n\
4251dir_fd may not be implemented on your platform.\n\
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004252 If it is unavailable, using it will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004253
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004254PyDoc_STRVAR(posix_remove__doc__,
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004255"remove(path, *, dir_fd=None)\n\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07004256Remove a file (same as unlink()).\n\
4257\n\
4258If dir_fd is not None, it should be a file descriptor open to a directory,\n\
4259 and path should be relative; path will then be relative to that directory.\n\
4260dir_fd may not be implemented on your platform.\n\
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004261 If it is unavailable, using it will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004262
Barry Warsaw53699e91996-12-10 23:23:01 +00004263static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07004264posix_unlink(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004265{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004266 path_t path;
4267 int dir_fd = DEFAULT_DIR_FD;
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004268 static char *keywords[] = {"path", "dir_fd", NULL};
Larry Hastings9cf065c2012-06-22 16:30:09 -07004269 int result;
4270 PyObject *return_value = NULL;
4271
4272 memset(&path, 0, sizeof(path));
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004273 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:unlink", keywords,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004274 path_converter, &path,
4275#ifdef HAVE_UNLINKAT
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004276 dir_fd_converter, &dir_fd
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004277#else
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004278 dir_fd_unavailable, &dir_fd
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004279#endif
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004280 ))
Larry Hastings9cf065c2012-06-22 16:30:09 -07004281 return NULL;
4282
4283 Py_BEGIN_ALLOW_THREADS
4284#ifdef MS_WINDOWS
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004285 if (path.wide)
4286 result = Py_DeleteFileW(path.wide);
4287 else
4288 result = DeleteFileA(path.narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004289 result = !result; /* Windows, success=1, UNIX, success=0 */
4290#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004291#ifdef HAVE_UNLINKAT
4292 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004293 result = unlinkat(dir_fd, path.narrow, 0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004294 else
4295#endif /* HAVE_UNLINKAT */
4296 result = unlink(path.narrow);
4297#endif
4298 Py_END_ALLOW_THREADS
4299
4300 if (result) {
4301 return_value = path_error("unlink", &path);
4302 goto exit;
4303 }
4304
4305 return_value = Py_None;
4306 Py_INCREF(Py_None);
4307
4308exit:
4309 path_cleanup(&path);
4310 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004311}
4312
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004313
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004314PyDoc_STRVAR(posix_uname__doc__,
Larry Hastings605a62d2012-06-24 04:33:36 -07004315"uname() -> uname_result\n\n\
4316Return an object identifying the current operating system.\n\
4317The object behaves like a named tuple with the following fields:\n\
4318 (sysname, nodename, release, version, machine)");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004319
Larry Hastings605a62d2012-06-24 04:33:36 -07004320static PyStructSequence_Field uname_result_fields[] = {
4321 {"sysname", "operating system name"},
4322 {"nodename", "name of machine on network (implementation-defined)"},
4323 {"release", "operating system release"},
4324 {"version", "operating system version"},
4325 {"machine", "hardware identifier"},
4326 {NULL}
4327};
4328
4329PyDoc_STRVAR(uname_result__doc__,
4330"uname_result: Result from os.uname().\n\n\
4331This object may be accessed either as a tuple of\n\
4332 (sysname, nodename, release, version, machine),\n\
4333or via the attributes sysname, nodename, release, version, and machine.\n\
4334\n\
4335See os.uname for more information.");
4336
4337static PyStructSequence_Desc uname_result_desc = {
4338 "uname_result", /* name */
4339 uname_result__doc__, /* doc */
4340 uname_result_fields,
4341 5
4342};
4343
4344static PyTypeObject UnameResultType;
4345
4346
4347#ifdef HAVE_UNAME
Barry Warsaw53699e91996-12-10 23:23:01 +00004348static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004349posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004350{
Victor Stinner8c62be82010-05-06 00:08:46 +00004351 struct utsname u;
4352 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004353 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004354
Victor Stinner8c62be82010-05-06 00:08:46 +00004355 Py_BEGIN_ALLOW_THREADS
4356 res = uname(&u);
4357 Py_END_ALLOW_THREADS
4358 if (res < 0)
4359 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004360
4361 value = PyStructSequence_New(&UnameResultType);
4362 if (value == NULL)
4363 return NULL;
4364
4365#define SET(i, field) \
4366 { \
4367 PyObject *o = PyUnicode_DecodeASCII(field, strlen(field), NULL); \
4368 if (!o) { \
4369 Py_DECREF(value); \
4370 return NULL; \
4371 } \
4372 PyStructSequence_SET_ITEM(value, i, o); \
4373 } \
4374
4375 SET(0, u.sysname);
4376 SET(1, u.nodename);
4377 SET(2, u.release);
4378 SET(3, u.version);
4379 SET(4, u.machine);
4380
4381#undef SET
4382
4383 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004384}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004385#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004386
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004387
Larry Hastings9cf065c2012-06-22 16:30:09 -07004388PyDoc_STRVAR(posix_utime__doc__,
4389"utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True)\n\
4390Set the access and modified time of path.\n\
4391\n\
4392path may always be specified as a string.\n\
4393On some platforms, path may also be specified as an open file descriptor.\n\
4394 If this functionality is unavailable, using it raises an exception.\n\
4395\n\
4396If times is not None, it must be a tuple (atime, mtime);\n\
4397 atime and mtime should be expressed as float seconds since the epoch.\n\
4398If ns is not None, it must be a tuple (atime_ns, mtime_ns);\n\
4399 atime_ns and mtime_ns should be expressed as integer nanoseconds\n\
4400 since the epoch.\n\
4401If both times and ns are None, utime uses the current time.\n\
4402Specifying tuples for both times and ns is an error.\n\
4403\n\
4404If dir_fd is not None, it should be a file descriptor open to a directory,\n\
4405 and path should be relative; path will then be relative to that directory.\n\
4406If follow_symlinks is False, and the last element of the path is a symbolic\n\
4407 link, utime will modify the symbolic link itself instead of the file the\n\
4408 link points to.\n\
4409It is an error to use dir_fd or follow_symlinks when specifying path\n\
4410 as an open file descriptor.\n\
4411dir_fd and follow_symlinks may not be available on your platform.\n\
4412 If they are unavailable, using them will raise a NotImplementedError.");
4413
4414typedef struct {
4415 int now;
4416 time_t atime_s;
4417 long atime_ns;
4418 time_t mtime_s;
4419 long mtime_ns;
4420} utime_t;
4421
4422/*
4423 * these macros assume that "utime" is a pointer to a utime_t
4424 * they also intentionally leak the declaration of a pointer named "time"
4425 */
4426#define UTIME_TO_TIMESPEC \
4427 struct timespec ts[2]; \
4428 struct timespec *time; \
4429 if (utime->now) \
4430 time = NULL; \
4431 else { \
4432 ts[0].tv_sec = utime->atime_s; \
4433 ts[0].tv_nsec = utime->atime_ns; \
4434 ts[1].tv_sec = utime->mtime_s; \
4435 ts[1].tv_nsec = utime->mtime_ns; \
4436 time = ts; \
4437 } \
4438
4439#define UTIME_TO_TIMEVAL \
4440 struct timeval tv[2]; \
4441 struct timeval *time; \
4442 if (utime->now) \
4443 time = NULL; \
4444 else { \
4445 tv[0].tv_sec = utime->atime_s; \
4446 tv[0].tv_usec = utime->atime_ns / 1000; \
4447 tv[1].tv_sec = utime->mtime_s; \
4448 tv[1].tv_usec = utime->mtime_ns / 1000; \
4449 time = tv; \
4450 } \
4451
4452#define UTIME_TO_UTIMBUF \
4453 struct utimbuf u[2]; \
4454 struct utimbuf *time; \
4455 if (utime->now) \
4456 time = NULL; \
4457 else { \
4458 u.actime = utime->atime_s; \
4459 u.modtime = utime->mtime_s; \
4460 time = u; \
4461 }
4462
4463#define UTIME_TO_TIME_T \
4464 time_t timet[2]; \
4465 struct timet time; \
4466 if (utime->now) \
4467 time = NULL; \
4468 else { \
4469 timet[0] = utime->atime_s; \
4470 timet[1] = utime->mtime_s; \
4471 time = &timet; \
4472 } \
4473
4474
4475#define UTIME_HAVE_DIR_FD (defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT))
4476
4477#if UTIME_HAVE_DIR_FD
4478
4479static int
4480utime_dir_fd(utime_t *utime, int dir_fd, char *path, int follow_symlinks)
4481{
4482#ifdef HAVE_UTIMENSAT
4483 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4484 UTIME_TO_TIMESPEC;
4485 return utimensat(dir_fd, path, time, flags);
4486#elif defined(HAVE_FUTIMESAT)
4487 UTIME_TO_TIMEVAL;
4488 /*
4489 * follow_symlinks will never be false here;
4490 * we only allow !follow_symlinks and dir_fd together
4491 * if we have utimensat()
4492 */
4493 assert(follow_symlinks);
4494 return futimesat(dir_fd, path, time);
4495#endif
4496}
4497
4498#endif
4499
4500#define UTIME_HAVE_FD (defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS))
4501
4502#if UTIME_HAVE_FD
4503
4504static int
4505utime_fd(utime_t *utime, int fd)
4506{
4507#ifdef HAVE_FUTIMENS
4508 UTIME_TO_TIMESPEC;
4509 return futimens(fd, time);
4510#else
4511 UTIME_TO_TIMEVAL;
4512 return futimes(fd, time);
4513#endif
4514}
4515
4516#endif
4517
4518
4519#define UTIME_HAVE_NOFOLLOW_SYMLINKS \
4520 (defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES))
4521
4522#if UTIME_HAVE_NOFOLLOW_SYMLINKS
4523
4524static int
4525utime_nofollow_symlinks(utime_t *utime, char *path)
4526{
4527#ifdef HAVE_UTIMENSAT
4528 UTIME_TO_TIMESPEC;
4529 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4530#else
4531 UTIME_TO_TIMEVAL;
4532 return lutimes(path, time);
4533#endif
4534}
4535
4536#endif
4537
4538#ifndef MS_WINDOWS
4539
4540static int
4541utime_default(utime_t *utime, char *path)
4542{
4543#ifdef HAVE_UTIMENSAT
4544 UTIME_TO_TIMESPEC;
4545 return utimensat(DEFAULT_DIR_FD, path, time, 0);
4546#elif defined(HAVE_UTIMES)
4547 UTIME_TO_TIMEVAL;
4548 return utimes(path, time);
4549#elif defined(HAVE_UTIME_H)
4550 UTIME_TO_UTIMBUF;
4551 return utime(path, time);
4552#else
4553 UTIME_TO_TIME_T;
4554 return utime(path, time);
4555#endif
4556}
4557
4558#endif
4559
Larry Hastings76ad59b2012-05-03 00:30:07 -07004560static int
4561split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4562{
4563 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04004564 PyObject *divmod;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004565 divmod = PyNumber_Divmod(py_long, billion);
4566 if (!divmod)
4567 goto exit;
4568 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4569 if ((*s == -1) && PyErr_Occurred())
4570 goto exit;
4571 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04004572 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07004573 goto exit;
4574
4575 result = 1;
4576exit:
4577 Py_XDECREF(divmod);
4578 return result;
4579}
4580
Larry Hastings9cf065c2012-06-22 16:30:09 -07004581static PyObject *
4582posix_utime(PyObject *self, PyObject *args, PyObject *kwargs)
Larry Hastings76ad59b2012-05-03 00:30:07 -07004583{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004584 path_t path;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004585 PyObject *times = NULL;
4586 PyObject *ns = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004587 int dir_fd = DEFAULT_DIR_FD;
4588 int follow_symlinks = 1;
4589 char *keywords[] = {"path", "times", "ns", "dir_fd",
4590 "follow_symlinks", NULL};
Larry Hastings76ad59b2012-05-03 00:30:07 -07004591
Larry Hastings9cf065c2012-06-22 16:30:09 -07004592 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004593
Larry Hastings9cf065c2012-06-22 16:30:09 -07004594#ifdef MS_WINDOWS
4595 HANDLE hFile;
4596 FILETIME atime, mtime;
4597#else
4598 int result;
4599#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004600
Larry Hastings9cf065c2012-06-22 16:30:09 -07004601 PyObject *return_value = NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004602
Larry Hastings9cf065c2012-06-22 16:30:09 -07004603 memset(&path, 0, sizeof(path));
4604#if UTIME_HAVE_FD
4605 path.allow_fd = 1;
4606#endif
4607 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
4608 "O&|O$OO&p:utime", keywords,
4609 path_converter, &path,
4610 &times, &ns,
4611#if UTIME_HAVE_DIR_FD
4612 dir_fd_converter, &dir_fd,
4613#else
4614 dir_fd_unavailable, &dir_fd,
4615#endif
4616 &follow_symlinks
4617 ))
4618 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004619
Larry Hastings9cf065c2012-06-22 16:30:09 -07004620 if (times && (times != Py_None) && ns) {
4621 PyErr_SetString(PyExc_ValueError,
4622 "utime: you may specify either 'times'"
4623 " or 'ns' but not both");
4624 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004625 }
4626
4627 if (times && (times != Py_None)) {
4628 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004629 PyErr_SetString(PyExc_TypeError,
4630 "utime: 'times' must be either"
4631 " a tuple of two ints or None");
4632 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004633 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004634 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004635 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004636 &utime.atime_s, &utime.atime_ns) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004637 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004638 &utime.mtime_s, &utime.mtime_ns) == -1) {
4639 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004640 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004641 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004642 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004643 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004644 PyErr_SetString(PyExc_TypeError,
4645 "utime: 'ns' must be a tuple of two ints");
4646 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004647 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004648 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004649 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004650 &utime.atime_s, &utime.atime_ns) ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004651 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004652 &utime.mtime_s, &utime.mtime_ns)) {
4653 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004654 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004655 }
4656 else {
4657 /* times and ns are both None/unspecified. use "now". */
4658 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004659 }
4660
Larry Hastings9cf065c2012-06-22 16:30:09 -07004661#if !UTIME_HAVE_NOFOLLOW_SYMLINKS
4662 if (follow_symlinks_specified("utime", follow_symlinks))
4663 goto exit;
4664#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004665
Larry Hastings9cf065c2012-06-22 16:30:09 -07004666 if (path_and_dir_fd_invalid("utime", &path, dir_fd) ||
4667 dir_fd_and_fd_invalid("utime", dir_fd, path.fd) ||
4668 fd_and_follow_symlinks_invalid("utime", path.fd, follow_symlinks))
4669 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004670
Larry Hastings9cf065c2012-06-22 16:30:09 -07004671#if !defined(HAVE_UTIMENSAT)
4672 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02004673 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004674 "utime: cannot use dir_fd and follow_symlinks "
4675 "together on this platform");
4676 goto exit;
4677 }
4678#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004679
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004680#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004681 Py_BEGIN_ALLOW_THREADS
4682 if (path.wide)
4683 hFile = CreateFileW(path.wide, FILE_WRITE_ATTRIBUTES, 0,
Victor Stinner8c62be82010-05-06 00:08:46 +00004684 NULL, OPEN_EXISTING,
4685 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004686 else
4687 hFile = CreateFileA(path.narrow, FILE_WRITE_ATTRIBUTES, 0,
Victor Stinner8c62be82010-05-06 00:08:46 +00004688 NULL, OPEN_EXISTING,
4689 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004690 Py_END_ALLOW_THREADS
4691 if (hFile == INVALID_HANDLE_VALUE) {
4692 win32_error_object("utime", path.object);
4693 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004694 }
4695
Larry Hastings9cf065c2012-06-22 16:30:09 -07004696 if (utime.now) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004697 SYSTEMTIME now;
4698 GetSystemTime(&now);
4699 if (!SystemTimeToFileTime(&now, &mtime) ||
4700 !SystemTimeToFileTime(&now, &atime)) {
4701 win32_error("utime", NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004702 goto exit;
Stefan Krah0e803b32010-11-26 16:16:47 +00004703 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004704 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004705 else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004706 time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
4707 time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00004708 }
4709 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
4710 /* Avoid putting the file name into the error here,
4711 as that may confuse the user into believing that
4712 something is wrong with the file, when it also
4713 could be the time stamp that gives a problem. */
4714 win32_error("utime", NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004715 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004716 }
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004717#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004718 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004719
Larry Hastings9cf065c2012-06-22 16:30:09 -07004720#if UTIME_HAVE_NOFOLLOW_SYMLINKS
4721 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
4722 result = utime_nofollow_symlinks(&utime, path.narrow);
4723 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004724#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004725
4726#if UTIME_HAVE_DIR_FD
4727 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
4728 result = utime_dir_fd(&utime, dir_fd, path.narrow, follow_symlinks);
4729 else
4730#endif
4731
4732#if UTIME_HAVE_FD
4733 if (path.fd != -1)
4734 result = utime_fd(&utime, path.fd);
4735 else
4736#endif
4737
4738 result = utime_default(&utime, path.narrow);
4739
4740 Py_END_ALLOW_THREADS
4741
4742 if (result < 0) {
4743 /* see previous comment about not putting filename in error here */
4744 return_value = posix_error();
4745 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004746 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004747
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004748#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004749
4750 Py_INCREF(Py_None);
4751 return_value = Py_None;
4752
4753exit:
4754 path_cleanup(&path);
4755#ifdef MS_WINDOWS
4756 if (hFile != INVALID_HANDLE_VALUE)
4757 CloseHandle(hFile);
4758#endif
4759 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004760}
4761
Guido van Rossum3b066191991-06-04 19:40:25 +00004762/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004763
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004764PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004765"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004766Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004767
Barry Warsaw53699e91996-12-10 23:23:01 +00004768static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004769posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004770{
Victor Stinner8c62be82010-05-06 00:08:46 +00004771 int sts;
4772 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
4773 return NULL;
4774 _exit(sts);
4775 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004776}
4777
Martin v. Löwis114619e2002-10-07 06:44:21 +00004778#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
4779static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00004780free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004781{
Victor Stinner8c62be82010-05-06 00:08:46 +00004782 Py_ssize_t i;
4783 for (i = 0; i < count; i++)
4784 PyMem_Free(array[i]);
4785 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004786}
Martin v. Löwis011e8422009-05-05 04:43:17 +00004787
Antoine Pitrou69f71142009-05-24 21:25:49 +00004788static
Martin v. Löwis011e8422009-05-05 04:43:17 +00004789int fsconvert_strdup(PyObject *o, char**out)
4790{
Victor Stinner8c62be82010-05-06 00:08:46 +00004791 PyObject *bytes;
4792 Py_ssize_t size;
4793 if (!PyUnicode_FSConverter(o, &bytes))
4794 return 0;
4795 size = PyBytes_GET_SIZE(bytes);
4796 *out = PyMem_Malloc(size+1);
4797 if (!*out)
4798 return 0;
4799 memcpy(*out, PyBytes_AsString(bytes), size+1);
4800 Py_DECREF(bytes);
4801 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004802}
Martin v. Löwis114619e2002-10-07 06:44:21 +00004803#endif
4804
Ross Lagerwall7807c352011-03-17 20:20:30 +02004805#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00004806static char**
4807parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
4808{
Victor Stinner8c62be82010-05-06 00:08:46 +00004809 char **envlist;
4810 Py_ssize_t i, pos, envc;
4811 PyObject *keys=NULL, *vals=NULL;
4812 PyObject *key, *val, *key2, *val2;
4813 char *p, *k, *v;
4814 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004815
Victor Stinner8c62be82010-05-06 00:08:46 +00004816 i = PyMapping_Size(env);
4817 if (i < 0)
4818 return NULL;
4819 envlist = PyMem_NEW(char *, i + 1);
4820 if (envlist == NULL) {
4821 PyErr_NoMemory();
4822 return NULL;
4823 }
4824 envc = 0;
4825 keys = PyMapping_Keys(env);
4826 vals = PyMapping_Values(env);
4827 if (!keys || !vals)
4828 goto error;
4829 if (!PyList_Check(keys) || !PyList_Check(vals)) {
4830 PyErr_Format(PyExc_TypeError,
4831 "env.keys() or env.values() is not a list");
4832 goto error;
4833 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004834
Victor Stinner8c62be82010-05-06 00:08:46 +00004835 for (pos = 0; pos < i; pos++) {
4836 key = PyList_GetItem(keys, pos);
4837 val = PyList_GetItem(vals, pos);
4838 if (!key || !val)
4839 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004840
Victor Stinner8c62be82010-05-06 00:08:46 +00004841 if (PyUnicode_FSConverter(key, &key2) == 0)
4842 goto error;
4843 if (PyUnicode_FSConverter(val, &val2) == 0) {
4844 Py_DECREF(key2);
4845 goto error;
4846 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004847
4848#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00004849 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
4850 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00004851#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004852 k = PyBytes_AsString(key2);
4853 v = PyBytes_AsString(val2);
4854 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004855
Victor Stinner8c62be82010-05-06 00:08:46 +00004856 p = PyMem_NEW(char, len);
4857 if (p == NULL) {
4858 PyErr_NoMemory();
4859 Py_DECREF(key2);
4860 Py_DECREF(val2);
4861 goto error;
4862 }
4863 PyOS_snprintf(p, len, "%s=%s", k, v);
4864 envlist[envc++] = p;
4865 Py_DECREF(key2);
4866 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004867#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00004868 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004869#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004870 }
4871 Py_DECREF(vals);
4872 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004873
Victor Stinner8c62be82010-05-06 00:08:46 +00004874 envlist[envc] = 0;
4875 *envc_ptr = envc;
4876 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004877
4878error:
Victor Stinner8c62be82010-05-06 00:08:46 +00004879 Py_XDECREF(keys);
4880 Py_XDECREF(vals);
4881 while (--envc >= 0)
4882 PyMem_DEL(envlist[envc]);
4883 PyMem_DEL(envlist);
4884 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004885}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004886
Ross Lagerwall7807c352011-03-17 20:20:30 +02004887static char**
4888parse_arglist(PyObject* argv, Py_ssize_t *argc)
4889{
4890 int i;
4891 char **argvlist = PyMem_NEW(char *, *argc+1);
4892 if (argvlist == NULL) {
4893 PyErr_NoMemory();
4894 return NULL;
4895 }
4896 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004897 PyObject* item = PySequence_ITEM(argv, i);
4898 if (item == NULL)
4899 goto fail;
4900 if (!fsconvert_strdup(item, &argvlist[i])) {
4901 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004902 goto fail;
4903 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004904 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004905 }
4906 argvlist[*argc] = NULL;
4907 return argvlist;
4908fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004909 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004910 free_string_array(argvlist, *argc);
4911 return NULL;
4912}
4913#endif
4914
4915#ifdef HAVE_EXECV
4916PyDoc_STRVAR(posix_execv__doc__,
4917"execv(path, args)\n\n\
4918Execute an executable path with arguments, replacing current process.\n\
4919\n\
4920 path: path of executable file\n\
4921 args: tuple or list of strings");
4922
4923static PyObject *
4924posix_execv(PyObject *self, PyObject *args)
4925{
4926 PyObject *opath;
4927 char *path;
4928 PyObject *argv;
4929 char **argvlist;
4930 Py_ssize_t argc;
4931
4932 /* execv has two arguments: (path, argv), where
4933 argv is a list or tuple of strings. */
4934
4935 if (!PyArg_ParseTuple(args, "O&O:execv",
4936 PyUnicode_FSConverter,
4937 &opath, &argv))
4938 return NULL;
4939 path = PyBytes_AsString(opath);
4940 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4941 PyErr_SetString(PyExc_TypeError,
4942 "execv() arg 2 must be a tuple or list");
4943 Py_DECREF(opath);
4944 return NULL;
4945 }
4946 argc = PySequence_Size(argv);
4947 if (argc < 1) {
4948 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
4949 Py_DECREF(opath);
4950 return NULL;
4951 }
4952
4953 argvlist = parse_arglist(argv, &argc);
4954 if (argvlist == NULL) {
4955 Py_DECREF(opath);
4956 return NULL;
4957 }
4958
4959 execv(path, argvlist);
4960
4961 /* If we get here it's definitely an error */
4962
4963 free_string_array(argvlist, argc);
4964 Py_DECREF(opath);
4965 return posix_error();
4966}
4967
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004968PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004969"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004970Execute a path with arguments and environment, replacing current process.\n\
4971\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004972 path: path of executable file\n\
4973 args: tuple or list of arguments\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07004974 env: dictionary of strings mapping to strings\n\
4975\n\
4976On some platforms, you may specify an open file descriptor for path;\n\
4977 execve will execute the program the file descriptor is open to.\n\
4978 If this functionality is unavailable, using it raises NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004979
Barry Warsaw53699e91996-12-10 23:23:01 +00004980static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07004981posix_execve(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004982{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004983 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00004984 PyObject *argv, *env;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004985 char **argvlist = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004986 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004987 Py_ssize_t argc, envc;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004988 static char *keywords[] = {"path", "argv", "environment", NULL};
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004989
Victor Stinner8c62be82010-05-06 00:08:46 +00004990 /* execve has three arguments: (path, argv, env), where
4991 argv is a list or tuple of strings and env is a dictionary
4992 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004993
Larry Hastings9cf065c2012-06-22 16:30:09 -07004994 memset(&path, 0, sizeof(path));
4995#ifdef HAVE_FEXECVE
4996 path.allow_fd = 1;
4997#endif
4998 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&OO:execve", keywords,
4999 path_converter, &path,
5000 &argv, &env
5001 ))
Victor Stinner8c62be82010-05-06 00:08:46 +00005002 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07005003
Ross Lagerwall7807c352011-03-17 20:20:30 +02005004 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005005 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005006 "execve: argv must be a tuple or list");
5007 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005008 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005009 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00005010 if (!PyMapping_Check(env)) {
5011 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005012 "execve: environment must be a mapping object");
5013 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005014 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005015
Ross Lagerwall7807c352011-03-17 20:20:30 +02005016 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005017 if (argvlist == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005018 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005019 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005020
Victor Stinner8c62be82010-05-06 00:08:46 +00005021 envlist = parse_envlist(env, &envc);
5022 if (envlist == NULL)
Ross Lagerwall7807c352011-03-17 20:20:30 +02005023 goto fail;
5024
Larry Hastings9cf065c2012-06-22 16:30:09 -07005025#ifdef HAVE_FEXECVE
5026 if (path.fd > -1)
5027 fexecve(path.fd, argvlist, envlist);
5028 else
5029#endif
5030 execve(path.narrow, argvlist, envlist);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005031
5032 /* If we get here it's definitely an error */
5033
Larry Hastings9cf065c2012-06-22 16:30:09 -07005034 path_posix_error("execve", &path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005035
5036 while (--envc >= 0)
5037 PyMem_DEL(envlist[envc]);
5038 PyMem_DEL(envlist);
5039 fail:
Larry Hastings9cf065c2012-06-22 16:30:09 -07005040 if (argvlist)
5041 free_string_array(argvlist, argc);
5042 path_cleanup(&path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005043 return NULL;
5044}
Larry Hastings9cf065c2012-06-22 16:30:09 -07005045#endif /* HAVE_EXECV */
5046
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005047
Guido van Rossuma1065681999-01-25 23:20:23 +00005048#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005049PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005050"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00005051Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00005052\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00005053 mode: mode of process creation\n\
5054 path: path of executable file\n\
5055 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00005056
5057static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005058posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00005059{
Victor Stinner8c62be82010-05-06 00:08:46 +00005060 PyObject *opath;
5061 char *path;
5062 PyObject *argv;
5063 char **argvlist;
5064 int mode, i;
5065 Py_ssize_t argc;
5066 Py_intptr_t spawnval;
5067 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00005068
Victor Stinner8c62be82010-05-06 00:08:46 +00005069 /* spawnv has three arguments: (mode, path, argv), where
5070 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005071
Victor Stinner8c62be82010-05-06 00:08:46 +00005072 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
5073 PyUnicode_FSConverter,
5074 &opath, &argv))
5075 return NULL;
5076 path = PyBytes_AsString(opath);
5077 if (PyList_Check(argv)) {
5078 argc = PyList_Size(argv);
5079 getitem = PyList_GetItem;
5080 }
5081 else if (PyTuple_Check(argv)) {
5082 argc = PyTuple_Size(argv);
5083 getitem = PyTuple_GetItem;
5084 }
5085 else {
5086 PyErr_SetString(PyExc_TypeError,
5087 "spawnv() arg 2 must be a tuple or list");
5088 Py_DECREF(opath);
5089 return NULL;
5090 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005091
Victor Stinner8c62be82010-05-06 00:08:46 +00005092 argvlist = PyMem_NEW(char *, argc+1);
5093 if (argvlist == NULL) {
5094 Py_DECREF(opath);
5095 return PyErr_NoMemory();
5096 }
5097 for (i = 0; i < argc; i++) {
5098 if (!fsconvert_strdup((*getitem)(argv, i),
5099 &argvlist[i])) {
5100 free_string_array(argvlist, i);
5101 PyErr_SetString(
5102 PyExc_TypeError,
5103 "spawnv() arg 2 must contain only strings");
5104 Py_DECREF(opath);
5105 return NULL;
5106 }
5107 }
5108 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005109
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005110#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005111 Py_BEGIN_ALLOW_THREADS
5112 spawnval = spawnv(mode, path, argvlist);
5113 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005114#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005115 if (mode == _OLD_P_OVERLAY)
5116 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00005117
Victor Stinner8c62be82010-05-06 00:08:46 +00005118 Py_BEGIN_ALLOW_THREADS
5119 spawnval = _spawnv(mode, path, argvlist);
5120 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005121#endif
Tim Peters5aa91602002-01-30 05:46:57 +00005122
Victor Stinner8c62be82010-05-06 00:08:46 +00005123 free_string_array(argvlist, argc);
5124 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00005125
Victor Stinner8c62be82010-05-06 00:08:46 +00005126 if (spawnval == -1)
5127 return posix_error();
5128 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00005129#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00005130 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00005131#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005132 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00005133#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00005134}
5135
5136
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005137PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005138"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00005139Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00005140\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00005141 mode: mode of process creation\n\
5142 path: path of executable file\n\
5143 args: tuple or list of arguments\n\
5144 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00005145
5146static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005147posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00005148{
Victor Stinner8c62be82010-05-06 00:08:46 +00005149 PyObject *opath;
5150 char *path;
5151 PyObject *argv, *env;
5152 char **argvlist;
5153 char **envlist;
5154 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00005155 int mode;
5156 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00005157 Py_intptr_t spawnval;
5158 PyObject *(*getitem)(PyObject *, Py_ssize_t);
5159 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00005160
Victor Stinner8c62be82010-05-06 00:08:46 +00005161 /* spawnve has four arguments: (mode, path, argv, env), where
5162 argv is a list or tuple of strings and env is a dictionary
5163 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005164
Victor Stinner8c62be82010-05-06 00:08:46 +00005165 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
5166 PyUnicode_FSConverter,
5167 &opath, &argv, &env))
5168 return NULL;
5169 path = PyBytes_AsString(opath);
5170 if (PyList_Check(argv)) {
5171 argc = PyList_Size(argv);
5172 getitem = PyList_GetItem;
5173 }
5174 else if (PyTuple_Check(argv)) {
5175 argc = PyTuple_Size(argv);
5176 getitem = PyTuple_GetItem;
5177 }
5178 else {
5179 PyErr_SetString(PyExc_TypeError,
5180 "spawnve() arg 2 must be a tuple or list");
5181 goto fail_0;
5182 }
5183 if (!PyMapping_Check(env)) {
5184 PyErr_SetString(PyExc_TypeError,
5185 "spawnve() arg 3 must be a mapping object");
5186 goto fail_0;
5187 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005188
Victor Stinner8c62be82010-05-06 00:08:46 +00005189 argvlist = PyMem_NEW(char *, argc+1);
5190 if (argvlist == NULL) {
5191 PyErr_NoMemory();
5192 goto fail_0;
5193 }
5194 for (i = 0; i < argc; i++) {
5195 if (!fsconvert_strdup((*getitem)(argv, i),
5196 &argvlist[i]))
5197 {
5198 lastarg = i;
5199 goto fail_1;
5200 }
5201 }
5202 lastarg = argc;
5203 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005204
Victor Stinner8c62be82010-05-06 00:08:46 +00005205 envlist = parse_envlist(env, &envc);
5206 if (envlist == NULL)
5207 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00005208
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005209#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005210 Py_BEGIN_ALLOW_THREADS
5211 spawnval = spawnve(mode, path, argvlist, envlist);
5212 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005213#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005214 if (mode == _OLD_P_OVERLAY)
5215 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00005216
Victor Stinner8c62be82010-05-06 00:08:46 +00005217 Py_BEGIN_ALLOW_THREADS
5218 spawnval = _spawnve(mode, path, argvlist, envlist);
5219 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005220#endif
Tim Peters25059d32001-12-07 20:35:43 +00005221
Victor Stinner8c62be82010-05-06 00:08:46 +00005222 if (spawnval == -1)
5223 (void) posix_error();
5224 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00005225#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00005226 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00005227#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005228 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00005229#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00005230
Victor Stinner8c62be82010-05-06 00:08:46 +00005231 while (--envc >= 0)
5232 PyMem_DEL(envlist[envc]);
5233 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00005234 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00005235 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005236 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00005237 Py_DECREF(opath);
5238 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00005239}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005240
5241/* OS/2 supports spawnvp & spawnvpe natively */
5242#if defined(PYOS_OS2)
5243PyDoc_STRVAR(posix_spawnvp__doc__,
5244"spawnvp(mode, file, args)\n\n\
5245Execute the program 'file' in a new process, using the environment\n\
5246search path to find the file.\n\
5247\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00005248 mode: mode of process creation\n\
5249 file: executable file name\n\
5250 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005251
5252static PyObject *
5253posix_spawnvp(PyObject *self, PyObject *args)
5254{
Victor Stinner8c62be82010-05-06 00:08:46 +00005255 PyObject *opath;
5256 char *path;
5257 PyObject *argv;
5258 char **argvlist;
5259 int mode, i, argc;
5260 Py_intptr_t spawnval;
5261 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005262
Victor Stinner8c62be82010-05-06 00:08:46 +00005263 /* spawnvp has three arguments: (mode, path, argv), where
5264 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005265
Victor Stinner8c62be82010-05-06 00:08:46 +00005266 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
5267 PyUnicode_FSConverter,
5268 &opath, &argv))
5269 return NULL;
5270 path = PyBytes_AsString(opath);
5271 if (PyList_Check(argv)) {
5272 argc = PyList_Size(argv);
5273 getitem = PyList_GetItem;
5274 }
5275 else if (PyTuple_Check(argv)) {
5276 argc = PyTuple_Size(argv);
5277 getitem = PyTuple_GetItem;
5278 }
5279 else {
5280 PyErr_SetString(PyExc_TypeError,
5281 "spawnvp() arg 2 must be a tuple or list");
5282 Py_DECREF(opath);
5283 return NULL;
5284 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005285
Victor Stinner8c62be82010-05-06 00:08:46 +00005286 argvlist = PyMem_NEW(char *, argc+1);
5287 if (argvlist == NULL) {
5288 Py_DECREF(opath);
5289 return PyErr_NoMemory();
5290 }
5291 for (i = 0; i < argc; i++) {
5292 if (!fsconvert_strdup((*getitem)(argv, i),
5293 &argvlist[i])) {
5294 free_string_array(argvlist, i);
5295 PyErr_SetString(
5296 PyExc_TypeError,
5297 "spawnvp() arg 2 must contain only strings");
5298 Py_DECREF(opath);
5299 return NULL;
5300 }
5301 }
5302 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005303
Victor Stinner8c62be82010-05-06 00:08:46 +00005304 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005305#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005306 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005307#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005308 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005309#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005310 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005311
Victor Stinner8c62be82010-05-06 00:08:46 +00005312 free_string_array(argvlist, argc);
5313 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005314
Victor Stinner8c62be82010-05-06 00:08:46 +00005315 if (spawnval == -1)
5316 return posix_error();
5317 else
5318 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005319}
5320
5321
5322PyDoc_STRVAR(posix_spawnvpe__doc__,
5323"spawnvpe(mode, file, args, env)\n\n\
5324Execute the program 'file' in a new process, using the environment\n\
5325search path to find the file.\n\
5326\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00005327 mode: mode of process creation\n\
5328 file: executable file name\n\
5329 args: tuple or list of arguments\n\
5330 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005331
5332static PyObject *
5333posix_spawnvpe(PyObject *self, PyObject *args)
5334{
Ross Lagerwalldcfde5a2011-11-04 07:09:14 +02005335 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005336 char *path;
5337 PyObject *argv, *env;
5338 char **argvlist;
5339 char **envlist;
5340 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00005341 int mode;
5342 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00005343 Py_intptr_t spawnval;
5344 PyObject *(*getitem)(PyObject *, Py_ssize_t);
5345 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005346
Victor Stinner8c62be82010-05-06 00:08:46 +00005347 /* spawnvpe has four arguments: (mode, path, argv, env), where
5348 argv is a list or tuple of strings and env is a dictionary
5349 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005350
Victor Stinner8c62be82010-05-06 00:08:46 +00005351 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
5352 PyUnicode_FSConverter,
5353 &opath, &argv, &env))
5354 return NULL;
5355 path = PyBytes_AsString(opath);
5356 if (PyList_Check(argv)) {
5357 argc = PyList_Size(argv);
5358 getitem = PyList_GetItem;
5359 }
5360 else if (PyTuple_Check(argv)) {
5361 argc = PyTuple_Size(argv);
5362 getitem = PyTuple_GetItem;
5363 }
5364 else {
5365 PyErr_SetString(PyExc_TypeError,
5366 "spawnvpe() arg 2 must be a tuple or list");
5367 goto fail_0;
5368 }
5369 if (!PyMapping_Check(env)) {
5370 PyErr_SetString(PyExc_TypeError,
5371 "spawnvpe() arg 3 must be a mapping object");
5372 goto fail_0;
5373 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005374
Victor Stinner8c62be82010-05-06 00:08:46 +00005375 argvlist = PyMem_NEW(char *, argc+1);
5376 if (argvlist == NULL) {
5377 PyErr_NoMemory();
5378 goto fail_0;
5379 }
5380 for (i = 0; i < argc; i++) {
5381 if (!fsconvert_strdup((*getitem)(argv, i),
5382 &argvlist[i]))
5383 {
5384 lastarg = i;
5385 goto fail_1;
5386 }
5387 }
5388 lastarg = argc;
5389 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005390
Victor Stinner8c62be82010-05-06 00:08:46 +00005391 envlist = parse_envlist(env, &envc);
5392 if (envlist == NULL)
5393 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005394
Victor Stinner8c62be82010-05-06 00:08:46 +00005395 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005396#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005397 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005398#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005399 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005400#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005401 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005402
Victor Stinner8c62be82010-05-06 00:08:46 +00005403 if (spawnval == -1)
5404 (void) posix_error();
5405 else
5406 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005407
Victor Stinner8c62be82010-05-06 00:08:46 +00005408 while (--envc >= 0)
5409 PyMem_DEL(envlist[envc]);
5410 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005411 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00005412 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005413 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00005414 Py_DECREF(opath);
5415 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005416}
5417#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00005418#endif /* HAVE_SPAWNV */
5419
5420
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005421#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005422PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005423"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005424Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
5425\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005426Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005427
5428static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005429posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005430{
Victor Stinner8c62be82010-05-06 00:08:46 +00005431 pid_t pid;
5432 int result = 0;
5433 _PyImport_AcquireLock();
5434 pid = fork1();
5435 if (pid == 0) {
5436 /* child: this clobbers and resets the import lock. */
5437 PyOS_AfterFork();
5438 } else {
5439 /* parent: release the import lock. */
5440 result = _PyImport_ReleaseLock();
5441 }
5442 if (pid == -1)
5443 return posix_error();
5444 if (result < 0) {
5445 /* Don't clobber the OSError if the fork failed. */
5446 PyErr_SetString(PyExc_RuntimeError,
5447 "not holding the import lock");
5448 return NULL;
5449 }
5450 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005451}
5452#endif
5453
5454
Guido van Rossumad0ee831995-03-01 10:34:45 +00005455#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005456PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005457"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005458Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005459Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005460
Barry Warsaw53699e91996-12-10 23:23:01 +00005461static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005462posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005463{
Victor Stinner8c62be82010-05-06 00:08:46 +00005464 pid_t pid;
5465 int result = 0;
5466 _PyImport_AcquireLock();
5467 pid = fork();
5468 if (pid == 0) {
5469 /* child: this clobbers and resets the import lock. */
5470 PyOS_AfterFork();
5471 } else {
5472 /* parent: release the import lock. */
5473 result = _PyImport_ReleaseLock();
5474 }
5475 if (pid == -1)
5476 return posix_error();
5477 if (result < 0) {
5478 /* Don't clobber the OSError if the fork failed. */
5479 PyErr_SetString(PyExc_RuntimeError,
5480 "not holding the import lock");
5481 return NULL;
5482 }
5483 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00005484}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005485#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005486
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005487#ifdef HAVE_SCHED_H
5488
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005489#ifdef HAVE_SCHED_GET_PRIORITY_MAX
5490
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005491PyDoc_STRVAR(posix_sched_get_priority_max__doc__,
5492"sched_get_priority_max(policy)\n\n\
5493Get the maximum scheduling priority for *policy*.");
5494
5495static PyObject *
5496posix_sched_get_priority_max(PyObject *self, PyObject *args)
5497{
5498 int policy, max;
5499
5500 if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy))
5501 return NULL;
5502 max = sched_get_priority_max(policy);
5503 if (max < 0)
5504 return posix_error();
5505 return PyLong_FromLong(max);
5506}
5507
5508PyDoc_STRVAR(posix_sched_get_priority_min__doc__,
5509"sched_get_priority_min(policy)\n\n\
5510Get the minimum scheduling priority for *policy*.");
5511
5512static PyObject *
5513posix_sched_get_priority_min(PyObject *self, PyObject *args)
5514{
5515 int policy, min;
5516
5517 if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy))
5518 return NULL;
5519 min = sched_get_priority_min(policy);
5520 if (min < 0)
5521 return posix_error();
5522 return PyLong_FromLong(min);
5523}
5524
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005525#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
5526
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005527#ifdef HAVE_SCHED_SETSCHEDULER
5528
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005529PyDoc_STRVAR(posix_sched_getscheduler__doc__,
5530"sched_getscheduler(pid)\n\n\
5531Get the scheduling policy for the process with a PID of *pid*.\n\
5532Passing a PID of 0 returns the scheduling policy for the calling process.");
5533
5534static PyObject *
5535posix_sched_getscheduler(PyObject *self, PyObject *args)
5536{
5537 pid_t pid;
5538 int policy;
5539
5540 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid))
5541 return NULL;
5542 policy = sched_getscheduler(pid);
5543 if (policy < 0)
5544 return posix_error();
5545 return PyLong_FromLong(policy);
5546}
5547
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005548#endif
5549
5550#if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM)
5551
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005552static PyObject *
5553sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
5554{
5555 PyObject *res, *priority;
Benjamin Peterson4e36d5a2011-08-02 19:56:11 -05005556 static char *kwlist[] = {"sched_priority", NULL};
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005557
5558 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority))
5559 return NULL;
5560 res = PyStructSequence_New(type);
5561 if (!res)
5562 return NULL;
5563 Py_INCREF(priority);
5564 PyStructSequence_SET_ITEM(res, 0, priority);
5565 return res;
5566}
5567
5568PyDoc_STRVAR(sched_param__doc__,
5569"sched_param(sched_priority): A scheduling parameter.\n\n\
5570Current has only one field: sched_priority");
5571
5572static PyStructSequence_Field sched_param_fields[] = {
5573 {"sched_priority", "the scheduling priority"},
5574 {0}
5575};
5576
5577static PyStructSequence_Desc sched_param_desc = {
5578 "sched_param", /* name */
5579 sched_param__doc__, /* doc */
5580 sched_param_fields,
5581 1
5582};
5583
5584static int
5585convert_sched_param(PyObject *param, struct sched_param *res)
5586{
5587 long priority;
5588
5589 if (Py_TYPE(param) != &SchedParamType) {
5590 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
5591 return 0;
5592 }
5593 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
5594 if (priority == -1 && PyErr_Occurred())
5595 return 0;
5596 if (priority > INT_MAX || priority < INT_MIN) {
5597 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
5598 return 0;
5599 }
5600 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
5601 return 1;
5602}
5603
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005604#endif
5605
5606#ifdef HAVE_SCHED_SETSCHEDULER
5607
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005608PyDoc_STRVAR(posix_sched_setscheduler__doc__,
5609"sched_setscheduler(pid, policy, param)\n\n\
5610Set the scheduling policy, *policy*, for *pid*.\n\
5611If *pid* is 0, the calling process is changed.\n\
5612*param* is an instance of sched_param.");
5613
5614static PyObject *
5615posix_sched_setscheduler(PyObject *self, PyObject *args)
5616{
5617 pid_t pid;
5618 int policy;
5619 struct sched_param param;
5620
5621 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler",
5622 &pid, &policy, &convert_sched_param, &param))
5623 return NULL;
Jesus Cea9c822272011-09-10 01:40:52 +02005624
5625 /*
Jesus Cea54b01492011-09-10 01:53:19 +02005626 ** sched_setscheduler() returns 0 in Linux, but the previous
5627 ** scheduling policy under Solaris/Illumos, and others.
5628 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02005629 */
5630 if (sched_setscheduler(pid, policy, &param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005631 return posix_error();
5632 Py_RETURN_NONE;
5633}
5634
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005635#endif
5636
5637#ifdef HAVE_SCHED_SETPARAM
5638
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005639PyDoc_STRVAR(posix_sched_getparam__doc__,
5640"sched_getparam(pid) -> sched_param\n\n\
5641Returns scheduling parameters for the process with *pid* as an instance of the\n\
5642sched_param class. A PID of 0 means the calling process.");
5643
5644static PyObject *
5645posix_sched_getparam(PyObject *self, PyObject *args)
5646{
5647 pid_t pid;
5648 struct sched_param param;
5649 PyObject *res, *priority;
5650
5651 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid))
5652 return NULL;
5653 if (sched_getparam(pid, &param))
5654 return posix_error();
5655 res = PyStructSequence_New(&SchedParamType);
5656 if (!res)
5657 return NULL;
5658 priority = PyLong_FromLong(param.sched_priority);
5659 if (!priority) {
5660 Py_DECREF(res);
5661 return NULL;
5662 }
5663 PyStructSequence_SET_ITEM(res, 0, priority);
5664 return res;
5665}
5666
5667PyDoc_STRVAR(posix_sched_setparam__doc__,
5668"sched_setparam(pid, param)\n\n\
5669Set scheduling parameters for a process with PID *pid*.\n\
5670A PID of 0 means the calling process.");
5671
5672static PyObject *
5673posix_sched_setparam(PyObject *self, PyObject *args)
5674{
5675 pid_t pid;
5676 struct sched_param param;
5677
5678 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam",
5679 &pid, &convert_sched_param, &param))
5680 return NULL;
5681 if (sched_setparam(pid, &param))
5682 return posix_error();
5683 Py_RETURN_NONE;
5684}
5685
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005686#endif
5687
5688#ifdef HAVE_SCHED_RR_GET_INTERVAL
5689
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005690PyDoc_STRVAR(posix_sched_rr_get_interval__doc__,
5691"sched_rr_get_interval(pid) -> float\n\n\
5692Return the round-robin quantum for the process with PID *pid* in seconds.");
5693
5694static PyObject *
5695posix_sched_rr_get_interval(PyObject *self, PyObject *args)
5696{
5697 pid_t pid;
5698 struct timespec interval;
5699
5700 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid))
5701 return NULL;
5702 if (sched_rr_get_interval(pid, &interval))
5703 return posix_error();
5704 return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec);
5705}
5706
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005707#endif
5708
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005709PyDoc_STRVAR(posix_sched_yield__doc__,
5710"sched_yield()\n\n\
5711Voluntarily relinquish the CPU.");
5712
5713static PyObject *
5714posix_sched_yield(PyObject *self, PyObject *noargs)
5715{
5716 if (sched_yield())
5717 return posix_error();
5718 Py_RETURN_NONE;
5719}
5720
Benjamin Peterson2740af82011-08-02 17:41:34 -05005721#ifdef HAVE_SCHED_SETAFFINITY
5722
Antoine Pitrou84869872012-08-04 16:16:35 +02005723/* The minimum number of CPUs allocated in a cpu_set_t */
5724static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005725
5726PyDoc_STRVAR(posix_sched_setaffinity__doc__,
5727"sched_setaffinity(pid, cpu_set)\n\n\
5728Set the affinity of the process with PID *pid* to *cpu_set*.");
5729
5730static PyObject *
5731posix_sched_setaffinity(PyObject *self, PyObject *args)
5732{
5733 pid_t pid;
Antoine Pitrou84869872012-08-04 16:16:35 +02005734 int ncpus;
5735 size_t setsize;
5736 cpu_set_t *mask = NULL;
5737 PyObject *iterable, *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005738
Antoine Pitrou84869872012-08-04 16:16:35 +02005739 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O:sched_setaffinity",
5740 &pid, &iterable))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005741 return NULL;
Antoine Pitrou84869872012-08-04 16:16:35 +02005742
5743 iterator = PyObject_GetIter(iterable);
5744 if (iterator == NULL)
5745 return NULL;
5746
5747 ncpus = NCPUS_START;
5748 setsize = CPU_ALLOC_SIZE(ncpus);
5749 mask = CPU_ALLOC(ncpus);
5750 if (mask == NULL) {
5751 PyErr_NoMemory();
5752 goto error;
5753 }
5754 CPU_ZERO_S(setsize, mask);
5755
5756 while ((item = PyIter_Next(iterator))) {
5757 long cpu;
5758 if (!PyLong_Check(item)) {
5759 PyErr_Format(PyExc_TypeError,
5760 "expected an iterator of ints, "
5761 "but iterator yielded %R",
5762 Py_TYPE(item));
5763 Py_DECREF(item);
5764 goto error;
5765 }
5766 cpu = PyLong_AsLong(item);
5767 Py_DECREF(item);
5768 if (cpu < 0) {
5769 if (!PyErr_Occurred())
5770 PyErr_SetString(PyExc_ValueError, "negative CPU number");
5771 goto error;
5772 }
5773 if (cpu > INT_MAX - 1) {
5774 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
5775 goto error;
5776 }
5777 if (cpu >= ncpus) {
5778 /* Grow CPU mask to fit the CPU number */
5779 int newncpus = ncpus;
5780 cpu_set_t *newmask;
5781 size_t newsetsize;
5782 while (newncpus <= cpu) {
5783 if (newncpus > INT_MAX / 2)
5784 newncpus = cpu + 1;
5785 else
5786 newncpus = newncpus * 2;
5787 }
5788 newmask = CPU_ALLOC(newncpus);
5789 if (newmask == NULL) {
5790 PyErr_NoMemory();
5791 goto error;
5792 }
5793 newsetsize = CPU_ALLOC_SIZE(newncpus);
5794 CPU_ZERO_S(newsetsize, newmask);
5795 memcpy(newmask, mask, setsize);
5796 CPU_FREE(mask);
5797 setsize = newsetsize;
5798 mask = newmask;
5799 ncpus = newncpus;
5800 }
5801 CPU_SET_S(cpu, setsize, mask);
5802 }
5803 Py_CLEAR(iterator);
5804
5805 if (sched_setaffinity(pid, setsize, mask)) {
5806 posix_error();
5807 goto error;
5808 }
5809 CPU_FREE(mask);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005810 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02005811
5812error:
5813 if (mask)
5814 CPU_FREE(mask);
5815 Py_XDECREF(iterator);
5816 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005817}
5818
5819PyDoc_STRVAR(posix_sched_getaffinity__doc__,
5820"sched_getaffinity(pid, ncpus) -> cpu_set\n\n\
5821Return the affinity of the process with PID *pid*.\n\
5822The returned cpu_set will be of size *ncpus*.");
5823
5824static PyObject *
5825posix_sched_getaffinity(PyObject *self, PyObject *args)
5826{
5827 pid_t pid;
Antoine Pitrou84869872012-08-04 16:16:35 +02005828 int cpu, ncpus, count;
5829 size_t setsize;
5830 cpu_set_t *mask = NULL;
5831 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005832
Antoine Pitrou84869872012-08-04 16:16:35 +02005833 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getaffinity",
5834 &pid))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005835 return NULL;
Antoine Pitrou84869872012-08-04 16:16:35 +02005836
5837 ncpus = NCPUS_START;
5838 while (1) {
5839 setsize = CPU_ALLOC_SIZE(ncpus);
5840 mask = CPU_ALLOC(ncpus);
5841 if (mask == NULL)
5842 return PyErr_NoMemory();
5843 if (sched_getaffinity(pid, setsize, mask) == 0)
5844 break;
5845 CPU_FREE(mask);
5846 if (errno != EINVAL)
5847 return posix_error();
5848 if (ncpus > INT_MAX / 2) {
5849 PyErr_SetString(PyExc_OverflowError, "could not allocate "
5850 "a large enough CPU set");
5851 return NULL;
5852 }
5853 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005854 }
Antoine Pitrou84869872012-08-04 16:16:35 +02005855
5856 res = PySet_New(NULL);
5857 if (res == NULL)
5858 goto error;
5859 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
5860 if (CPU_ISSET_S(cpu, setsize, mask)) {
5861 PyObject *cpu_num = PyLong_FromLong(cpu);
5862 --count;
5863 if (cpu_num == NULL)
5864 goto error;
5865 if (PySet_Add(res, cpu_num)) {
5866 Py_DECREF(cpu_num);
5867 goto error;
5868 }
5869 Py_DECREF(cpu_num);
5870 }
5871 }
5872 CPU_FREE(mask);
5873 return res;
5874
5875error:
5876 if (mask)
5877 CPU_FREE(mask);
5878 Py_XDECREF(res);
5879 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005880}
5881
Benjamin Peterson2740af82011-08-02 17:41:34 -05005882#endif /* HAVE_SCHED_SETAFFINITY */
5883
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005884#endif /* HAVE_SCHED_H */
5885
Neal Norwitzb59798b2003-03-21 01:43:31 +00005886/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005887/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5888#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005889#define DEV_PTY_FILE "/dev/ptc"
5890#define HAVE_DEV_PTMX
5891#else
5892#define DEV_PTY_FILE "/dev/ptmx"
5893#endif
5894
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005895#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005896#ifdef HAVE_PTY_H
5897#include <pty.h>
5898#else
5899#ifdef HAVE_LIBUTIL_H
5900#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005901#else
5902#ifdef HAVE_UTIL_H
5903#include <util.h>
5904#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005905#endif /* HAVE_LIBUTIL_H */
5906#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005907#ifdef HAVE_STROPTS_H
5908#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005909#endif
5910#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005911
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005912#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005913PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005914"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005915Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005916
5917static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005918posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005919{
Victor Stinner8c62be82010-05-06 00:08:46 +00005920 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005921#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005922 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005923#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005924#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005925 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005926#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00005927 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005928#endif
5929#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005930
Thomas Wouters70c21a12000-07-14 14:28:33 +00005931#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005932 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
5933 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005934#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005935 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5936 if (slave_name == NULL)
5937 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00005938
Victor Stinner8c62be82010-05-06 00:08:46 +00005939 slave_fd = open(slave_name, O_RDWR);
5940 if (slave_fd < 0)
5941 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005942#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005943 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
5944 if (master_fd < 0)
5945 return posix_error();
5946 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
5947 /* change permission of slave */
5948 if (grantpt(master_fd) < 0) {
5949 PyOS_setsig(SIGCHLD, sig_saved);
5950 return posix_error();
5951 }
5952 /* unlock slave */
5953 if (unlockpt(master_fd) < 0) {
5954 PyOS_setsig(SIGCHLD, sig_saved);
5955 return posix_error();
5956 }
5957 PyOS_setsig(SIGCHLD, sig_saved);
5958 slave_name = ptsname(master_fd); /* get name of slave */
5959 if (slave_name == NULL)
5960 return posix_error();
5961 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
5962 if (slave_fd < 0)
5963 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005964#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005965 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
5966 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00005967#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00005968 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00005969#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005970#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00005971#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005972
Victor Stinner8c62be82010-05-06 00:08:46 +00005973 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00005974
Fred Drake8cef4cf2000-06-28 16:40:38 +00005975}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005976#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005977
5978#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005979PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005980"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00005981Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
5982Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005983To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005984
5985static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005986posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005987{
Victor Stinner8c62be82010-05-06 00:08:46 +00005988 int master_fd = -1, result = 0;
5989 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00005990
Victor Stinner8c62be82010-05-06 00:08:46 +00005991 _PyImport_AcquireLock();
5992 pid = forkpty(&master_fd, NULL, NULL, NULL);
5993 if (pid == 0) {
5994 /* child: this clobbers and resets the import lock. */
5995 PyOS_AfterFork();
5996 } else {
5997 /* parent: release the import lock. */
5998 result = _PyImport_ReleaseLock();
5999 }
6000 if (pid == -1)
6001 return posix_error();
6002 if (result < 0) {
6003 /* Don't clobber the OSError if the fork failed. */
6004 PyErr_SetString(PyExc_RuntimeError,
6005 "not holding the import lock");
6006 return NULL;
6007 }
6008 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00006009}
6010#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006011
Ross Lagerwall7807c352011-03-17 20:20:30 +02006012
Guido van Rossumad0ee831995-03-01 10:34:45 +00006013#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006014PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006015"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006016Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006017
Barry Warsaw53699e91996-12-10 23:23:01 +00006018static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006019posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00006020{
Victor Stinner8c62be82010-05-06 00:08:46 +00006021 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006022}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006023#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00006024
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006025
Guido van Rossumad0ee831995-03-01 10:34:45 +00006026#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006027PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006028"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006029Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006030
Barry Warsaw53699e91996-12-10 23:23:01 +00006031static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006032posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00006033{
Victor Stinner8c62be82010-05-06 00:08:46 +00006034 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006035}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006036#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00006037
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006038
Guido van Rossumad0ee831995-03-01 10:34:45 +00006039#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006040PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006041"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006042Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006043
Barry Warsaw53699e91996-12-10 23:23:01 +00006044static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006045posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00006046{
Victor Stinner8c62be82010-05-06 00:08:46 +00006047 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006048}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006049#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00006050
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006051
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006052PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006053"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006054Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006055
Barry Warsaw53699e91996-12-10 23:23:01 +00006056static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006057posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006058{
Victor Stinner8c62be82010-05-06 00:08:46 +00006059 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00006060}
6061
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006062#ifdef HAVE_GETGROUPLIST
6063PyDoc_STRVAR(posix_getgrouplist__doc__,
6064"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
6065Returns a list of groups to which a user belongs.\n\n\
6066 user: username to lookup\n\
6067 group: base group id of the user");
6068
6069static PyObject *
6070posix_getgrouplist(PyObject *self, PyObject *args)
6071{
6072#ifdef NGROUPS_MAX
6073#define MAX_GROUPS NGROUPS_MAX
6074#else
6075 /* defined to be 16 on Solaris7, so this should be a small number */
6076#define MAX_GROUPS 64
6077#endif
6078
6079 const char *user;
6080 int i, ngroups;
6081 PyObject *list;
6082#ifdef __APPLE__
6083 int *groups, basegid;
6084#else
6085 gid_t *groups, basegid;
6086#endif
6087 ngroups = MAX_GROUPS;
6088
6089 if (!PyArg_ParseTuple(args, "si", &user, &basegid))
6090 return NULL;
6091
6092#ifdef __APPLE__
6093 groups = PyMem_Malloc(ngroups * sizeof(int));
6094#else
6095 groups = PyMem_Malloc(ngroups * sizeof(gid_t));
6096#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++) {
6112 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
6113 if (o == NULL) {
6114 Py_DECREF(list);
6115 PyMem_Del(groups);
6116 return NULL;
6117 }
6118 PyList_SET_ITEM(list, i, o);
6119 }
6120
6121 PyMem_Del(groups);
6122
6123 return list;
6124}
6125#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006126
Fred Drakec9680921999-12-13 16:37:25 +00006127#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006128PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006129"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006130Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00006131
6132static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006133posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00006134{
6135 PyObject *result = NULL;
6136
Fred Drakec9680921999-12-13 16:37:25 +00006137#ifdef NGROUPS_MAX
6138#define MAX_GROUPS NGROUPS_MAX
6139#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006140 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00006141#define MAX_GROUPS 64
6142#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006143 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006144
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006145 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006146 * This is a helper variable to store the intermediate result when
6147 * that happens.
6148 *
6149 * To keep the code readable the OSX behaviour is unconditional,
6150 * according to the POSIX spec this should be safe on all unix-y
6151 * systems.
6152 */
6153 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006154 int n;
Fred Drakec9680921999-12-13 16:37:25 +00006155
Victor Stinner8c62be82010-05-06 00:08:46 +00006156 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006157 if (n < 0) {
6158 if (errno == EINVAL) {
6159 n = getgroups(0, NULL);
6160 if (n == -1) {
6161 return posix_error();
6162 }
6163 if (n == 0) {
6164 /* Avoid malloc(0) */
6165 alt_grouplist = grouplist;
6166 } else {
6167 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
6168 if (alt_grouplist == NULL) {
6169 errno = EINVAL;
6170 return posix_error();
6171 }
6172 n = getgroups(n, alt_grouplist);
6173 if (n == -1) {
6174 PyMem_Free(alt_grouplist);
6175 return posix_error();
6176 }
6177 }
6178 } else {
6179 return posix_error();
6180 }
6181 }
6182 result = PyList_New(n);
6183 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006184 int i;
6185 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006186 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00006187 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006188 Py_DECREF(result);
6189 result = NULL;
6190 break;
Fred Drakec9680921999-12-13 16:37:25 +00006191 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006192 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00006193 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006194 }
6195
6196 if (alt_grouplist != grouplist) {
6197 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00006198 }
Neal Norwitze241ce82003-02-17 18:17:05 +00006199
Fred Drakec9680921999-12-13 16:37:25 +00006200 return result;
6201}
6202#endif
6203
Antoine Pitroub7572f02009-12-02 20:46:48 +00006204#ifdef HAVE_INITGROUPS
6205PyDoc_STRVAR(posix_initgroups__doc__,
6206"initgroups(username, gid) -> None\n\n\
6207Call the system initgroups() to initialize the group access list with all of\n\
6208the groups of which the specified username is a member, plus the specified\n\
6209group id.");
6210
6211static PyObject *
6212posix_initgroups(PyObject *self, PyObject *args)
6213{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006214 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00006215 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006216 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00006217 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00006218
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006219 if (!PyArg_ParseTuple(args, "O&l:initgroups",
6220 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00006221 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006222 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006223
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006224 res = initgroups(username, (gid_t) gid);
6225 Py_DECREF(oname);
6226 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00006227 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006228
Victor Stinner8c62be82010-05-06 00:08:46 +00006229 Py_INCREF(Py_None);
6230 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00006231}
6232#endif
6233
Martin v. Löwis606edc12002-06-13 21:09:11 +00006234#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00006235PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006236"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00006237Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00006238
6239static PyObject *
6240posix_getpgid(PyObject *self, PyObject *args)
6241{
Victor Stinner8c62be82010-05-06 00:08:46 +00006242 pid_t pid, pgid;
6243 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
6244 return NULL;
6245 pgid = getpgid(pid);
6246 if (pgid < 0)
6247 return posix_error();
6248 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00006249}
6250#endif /* HAVE_GETPGID */
6251
6252
Guido van Rossumb6775db1994-08-01 11:34:53 +00006253#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006254PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006255"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006256Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006257
Barry Warsaw53699e91996-12-10 23:23:01 +00006258static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006259posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00006260{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006261#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006262 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006263#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006264 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006265#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00006266}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006267#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00006268
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006269
Guido van Rossumb6775db1994-08-01 11:34:53 +00006270#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006271PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006272"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00006273Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006274
Barry Warsaw53699e91996-12-10 23:23:01 +00006275static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006276posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006277{
Guido van Rossum64933891994-10-20 21:56:42 +00006278#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006279 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006280#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006281 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006282#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006283 return posix_error();
6284 Py_INCREF(Py_None);
6285 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006286}
6287
Guido van Rossumb6775db1994-08-01 11:34:53 +00006288#endif /* HAVE_SETPGRP */
6289
Guido van Rossumad0ee831995-03-01 10:34:45 +00006290#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006291
6292#ifdef MS_WINDOWS
6293#include <tlhelp32.h>
6294
6295static PyObject*
6296win32_getppid()
6297{
6298 HANDLE snapshot;
6299 pid_t mypid;
6300 PyObject* result = NULL;
6301 BOOL have_record;
6302 PROCESSENTRY32 pe;
6303
6304 mypid = getpid(); /* This function never fails */
6305
6306 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
6307 if (snapshot == INVALID_HANDLE_VALUE)
6308 return PyErr_SetFromWindowsErr(GetLastError());
6309
6310 pe.dwSize = sizeof(pe);
6311 have_record = Process32First(snapshot, &pe);
6312 while (have_record) {
6313 if (mypid == (pid_t)pe.th32ProcessID) {
6314 /* We could cache the ulong value in a static variable. */
6315 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
6316 break;
6317 }
6318
6319 have_record = Process32Next(snapshot, &pe);
6320 }
6321
6322 /* If our loop exits and our pid was not found (result will be NULL)
6323 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
6324 * error anyway, so let's raise it. */
6325 if (!result)
6326 result = PyErr_SetFromWindowsErr(GetLastError());
6327
6328 CloseHandle(snapshot);
6329
6330 return result;
6331}
6332#endif /*MS_WINDOWS*/
6333
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006334PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006335"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006336Return the parent's process id. If the parent process has already exited,\n\
6337Windows machines will still return its id; others systems will return the id\n\
6338of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006339
Barry Warsaw53699e91996-12-10 23:23:01 +00006340static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006341posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006342{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006343#ifdef MS_WINDOWS
6344 return win32_getppid();
6345#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006346 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00006347#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006348}
6349#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006350
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006351
Fred Drake12c6e2d1999-12-14 21:25:03 +00006352#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006353PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006354"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006355Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00006356
6357static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006358posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006359{
Victor Stinner8c62be82010-05-06 00:08:46 +00006360 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006361#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006362 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02006363 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006364
6365 if (GetUserNameW(user_name, &num_chars)) {
6366 /* num_chars is the number of unicode chars plus null terminator */
6367 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006368 }
6369 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006370 result = PyErr_SetFromWindowsErr(GetLastError());
6371#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006372 char *name;
6373 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006374
Victor Stinner8c62be82010-05-06 00:08:46 +00006375 errno = 0;
6376 name = getlogin();
6377 if (name == NULL) {
6378 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00006379 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00006380 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00006381 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00006382 }
6383 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00006384 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00006385 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006386#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00006387 return result;
6388}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006389#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006390
Guido van Rossumad0ee831995-03-01 10:34:45 +00006391#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006392PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006393"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006394Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006395
Barry Warsaw53699e91996-12-10 23:23:01 +00006396static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006397posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00006398{
Victor Stinner8c62be82010-05-06 00:08:46 +00006399 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006400}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006401#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00006402
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006403
Guido van Rossumad0ee831995-03-01 10:34:45 +00006404#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006405PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006406"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006407Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006408
Barry Warsaw53699e91996-12-10 23:23:01 +00006409static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006410posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006411{
Victor Stinner8c62be82010-05-06 00:08:46 +00006412 pid_t pid;
6413 int sig;
6414 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
6415 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00006416#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006417 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
6418 APIRET rc;
6419 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006420 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006421
6422 } else if (sig == XCPT_SIGNAL_KILLPROC) {
6423 APIRET rc;
6424 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006425 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006426
6427 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00006428 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006429#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006430 if (kill(pid, sig) == -1)
6431 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006432#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006433 Py_INCREF(Py_None);
6434 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00006435}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006436#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006437
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006438#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006439PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006440"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006441Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006442
6443static PyObject *
6444posix_killpg(PyObject *self, PyObject *args)
6445{
Victor Stinner8c62be82010-05-06 00:08:46 +00006446 int sig;
6447 pid_t pgid;
6448 /* XXX some man pages make the `pgid` parameter an int, others
6449 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
6450 take the same type. Moreover, pid_t is always at least as wide as
6451 int (else compilation of this module fails), which is safe. */
6452 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
6453 return NULL;
6454 if (killpg(pgid, sig) == -1)
6455 return posix_error();
6456 Py_INCREF(Py_None);
6457 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006458}
6459#endif
6460
Brian Curtineb24d742010-04-12 17:16:38 +00006461#ifdef MS_WINDOWS
6462PyDoc_STRVAR(win32_kill__doc__,
6463"kill(pid, sig)\n\n\
6464Kill a process with a signal.");
6465
6466static PyObject *
6467win32_kill(PyObject *self, PyObject *args)
6468{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00006469 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00006470 DWORD pid, sig, err;
6471 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00006472
Victor Stinner8c62be82010-05-06 00:08:46 +00006473 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
6474 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00006475
Victor Stinner8c62be82010-05-06 00:08:46 +00006476 /* Console processes which share a common console can be sent CTRL+C or
6477 CTRL+BREAK events, provided they handle said events. */
6478 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
6479 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
6480 err = GetLastError();
6481 PyErr_SetFromWindowsErr(err);
6482 }
6483 else
6484 Py_RETURN_NONE;
6485 }
Brian Curtineb24d742010-04-12 17:16:38 +00006486
Victor Stinner8c62be82010-05-06 00:08:46 +00006487 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
6488 attempt to open and terminate the process. */
6489 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
6490 if (handle == NULL) {
6491 err = GetLastError();
6492 return PyErr_SetFromWindowsErr(err);
6493 }
Brian Curtineb24d742010-04-12 17:16:38 +00006494
Victor Stinner8c62be82010-05-06 00:08:46 +00006495 if (TerminateProcess(handle, sig) == 0) {
6496 err = GetLastError();
6497 result = PyErr_SetFromWindowsErr(err);
6498 } else {
6499 Py_INCREF(Py_None);
6500 result = Py_None;
6501 }
Brian Curtineb24d742010-04-12 17:16:38 +00006502
Victor Stinner8c62be82010-05-06 00:08:46 +00006503 CloseHandle(handle);
6504 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00006505}
6506#endif /* MS_WINDOWS */
6507
Guido van Rossumc0125471996-06-28 18:55:32 +00006508#ifdef HAVE_PLOCK
6509
6510#ifdef HAVE_SYS_LOCK_H
6511#include <sys/lock.h>
6512#endif
6513
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006514PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006515"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006516Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006517
Barry Warsaw53699e91996-12-10 23:23:01 +00006518static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006519posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00006520{
Victor Stinner8c62be82010-05-06 00:08:46 +00006521 int op;
6522 if (!PyArg_ParseTuple(args, "i:plock", &op))
6523 return NULL;
6524 if (plock(op) == -1)
6525 return posix_error();
6526 Py_INCREF(Py_None);
6527 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00006528}
6529#endif
6530
Guido van Rossumb6775db1994-08-01 11:34:53 +00006531#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006532PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006533"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006534Set the current process's user id.");
6535
Barry Warsaw53699e91996-12-10 23:23:01 +00006536static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006537posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006538{
Victor Stinner8c62be82010-05-06 00:08:46 +00006539 long uid_arg;
6540 uid_t uid;
6541 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
6542 return NULL;
6543 uid = uid_arg;
6544 if (uid != uid_arg) {
6545 PyErr_SetString(PyExc_OverflowError, "user id too big");
6546 return NULL;
6547 }
6548 if (setuid(uid) < 0)
6549 return posix_error();
6550 Py_INCREF(Py_None);
6551 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006552}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006553#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006554
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006555
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006556#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006557PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006558"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006559Set the current process's effective user id.");
6560
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006561static PyObject *
6562posix_seteuid (PyObject *self, PyObject *args)
6563{
Victor Stinner8c62be82010-05-06 00:08:46 +00006564 long euid_arg;
6565 uid_t euid;
6566 if (!PyArg_ParseTuple(args, "l", &euid_arg))
6567 return NULL;
6568 euid = euid_arg;
6569 if (euid != euid_arg) {
6570 PyErr_SetString(PyExc_OverflowError, "user id too big");
6571 return NULL;
6572 }
6573 if (seteuid(euid) < 0) {
6574 return posix_error();
6575 } else {
6576 Py_INCREF(Py_None);
6577 return Py_None;
6578 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006579}
6580#endif /* HAVE_SETEUID */
6581
6582#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006583PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006584"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006585Set the current process's effective group id.");
6586
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006587static PyObject *
6588posix_setegid (PyObject *self, PyObject *args)
6589{
Victor Stinner8c62be82010-05-06 00:08:46 +00006590 long egid_arg;
6591 gid_t egid;
6592 if (!PyArg_ParseTuple(args, "l", &egid_arg))
6593 return NULL;
6594 egid = egid_arg;
6595 if (egid != egid_arg) {
6596 PyErr_SetString(PyExc_OverflowError, "group id too big");
6597 return NULL;
6598 }
6599 if (setegid(egid) < 0) {
6600 return posix_error();
6601 } else {
6602 Py_INCREF(Py_None);
6603 return Py_None;
6604 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006605}
6606#endif /* HAVE_SETEGID */
6607
6608#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006609PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00006610"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006611Set the current process's real and effective user ids.");
6612
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006613static PyObject *
6614posix_setreuid (PyObject *self, PyObject *args)
6615{
Victor Stinner8c62be82010-05-06 00:08:46 +00006616 long ruid_arg, euid_arg;
6617 uid_t ruid, euid;
6618 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
6619 return NULL;
6620 if (ruid_arg == -1)
6621 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
6622 else
6623 ruid = ruid_arg; /* otherwise, assign from our long */
6624 if (euid_arg == -1)
6625 euid = (uid_t)-1;
6626 else
6627 euid = euid_arg;
6628 if ((euid_arg != -1 && euid != euid_arg) ||
6629 (ruid_arg != -1 && ruid != ruid_arg)) {
6630 PyErr_SetString(PyExc_OverflowError, "user id too big");
6631 return NULL;
6632 }
6633 if (setreuid(ruid, euid) < 0) {
6634 return posix_error();
6635 } else {
6636 Py_INCREF(Py_None);
6637 return Py_None;
6638 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006639}
6640#endif /* HAVE_SETREUID */
6641
6642#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006643PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00006644"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006645Set the current process's real and effective group ids.");
6646
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006647static PyObject *
6648posix_setregid (PyObject *self, PyObject *args)
6649{
Victor Stinner8c62be82010-05-06 00:08:46 +00006650 long rgid_arg, egid_arg;
6651 gid_t rgid, egid;
6652 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
6653 return NULL;
6654 if (rgid_arg == -1)
6655 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
6656 else
6657 rgid = rgid_arg; /* otherwise, assign from our long */
6658 if (egid_arg == -1)
6659 egid = (gid_t)-1;
6660 else
6661 egid = egid_arg;
6662 if ((egid_arg != -1 && egid != egid_arg) ||
6663 (rgid_arg != -1 && rgid != rgid_arg)) {
6664 PyErr_SetString(PyExc_OverflowError, "group id too big");
6665 return NULL;
6666 }
6667 if (setregid(rgid, egid) < 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_SETREGID */
6675
Guido van Rossumb6775db1994-08-01 11:34:53 +00006676#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006677PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006678"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006679Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006680
Barry Warsaw53699e91996-12-10 23:23:01 +00006681static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006682posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006683{
Victor Stinner8c62be82010-05-06 00:08:46 +00006684 long gid_arg;
6685 gid_t gid;
6686 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
6687 return NULL;
6688 gid = gid_arg;
6689 if (gid != gid_arg) {
6690 PyErr_SetString(PyExc_OverflowError, "group id too big");
6691 return NULL;
6692 }
6693 if (setgid(gid) < 0)
6694 return posix_error();
6695 Py_INCREF(Py_None);
6696 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006697}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006698#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006699
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006700#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006701PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006702"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006703Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006704
6705static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00006706posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006707{
Victor Stinner8c62be82010-05-06 00:08:46 +00006708 int i, len;
6709 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00006710
Victor Stinner8c62be82010-05-06 00:08:46 +00006711 if (!PySequence_Check(groups)) {
6712 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
6713 return NULL;
6714 }
6715 len = PySequence_Size(groups);
6716 if (len > MAX_GROUPS) {
6717 PyErr_SetString(PyExc_ValueError, "too many groups");
6718 return NULL;
6719 }
6720 for(i = 0; i < len; i++) {
6721 PyObject *elem;
6722 elem = PySequence_GetItem(groups, i);
6723 if (!elem)
6724 return NULL;
6725 if (!PyLong_Check(elem)) {
6726 PyErr_SetString(PyExc_TypeError,
6727 "groups must be integers");
6728 Py_DECREF(elem);
6729 return NULL;
6730 } else {
6731 unsigned long x = PyLong_AsUnsignedLong(elem);
6732 if (PyErr_Occurred()) {
6733 PyErr_SetString(PyExc_TypeError,
6734 "group id too big");
6735 Py_DECREF(elem);
6736 return NULL;
6737 }
6738 grouplist[i] = x;
6739 /* read back the value to see if it fitted in gid_t */
6740 if (grouplist[i] != x) {
6741 PyErr_SetString(PyExc_TypeError,
6742 "group id too big");
6743 Py_DECREF(elem);
6744 return NULL;
6745 }
6746 }
6747 Py_DECREF(elem);
6748 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006749
Victor Stinner8c62be82010-05-06 00:08:46 +00006750 if (setgroups(len, grouplist) < 0)
6751 return posix_error();
6752 Py_INCREF(Py_None);
6753 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006754}
6755#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006756
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006757#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
6758static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006759wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006760{
Victor Stinner8c62be82010-05-06 00:08:46 +00006761 PyObject *result;
6762 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006763 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006764
Victor Stinner8c62be82010-05-06 00:08:46 +00006765 if (pid == -1)
6766 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006767
Victor Stinner8c62be82010-05-06 00:08:46 +00006768 if (struct_rusage == NULL) {
6769 PyObject *m = PyImport_ImportModuleNoBlock("resource");
6770 if (m == NULL)
6771 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006772 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00006773 Py_DECREF(m);
6774 if (struct_rusage == NULL)
6775 return NULL;
6776 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006777
Victor Stinner8c62be82010-05-06 00:08:46 +00006778 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6779 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6780 if (!result)
6781 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006782
6783#ifndef doubletime
6784#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6785#endif
6786
Victor Stinner8c62be82010-05-06 00:08:46 +00006787 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006788 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00006789 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006790 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006791#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00006792 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
6793 SET_INT(result, 2, ru->ru_maxrss);
6794 SET_INT(result, 3, ru->ru_ixrss);
6795 SET_INT(result, 4, ru->ru_idrss);
6796 SET_INT(result, 5, ru->ru_isrss);
6797 SET_INT(result, 6, ru->ru_minflt);
6798 SET_INT(result, 7, ru->ru_majflt);
6799 SET_INT(result, 8, ru->ru_nswap);
6800 SET_INT(result, 9, ru->ru_inblock);
6801 SET_INT(result, 10, ru->ru_oublock);
6802 SET_INT(result, 11, ru->ru_msgsnd);
6803 SET_INT(result, 12, ru->ru_msgrcv);
6804 SET_INT(result, 13, ru->ru_nsignals);
6805 SET_INT(result, 14, ru->ru_nvcsw);
6806 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006807#undef SET_INT
6808
Victor Stinner8c62be82010-05-06 00:08:46 +00006809 if (PyErr_Occurred()) {
6810 Py_DECREF(result);
6811 return NULL;
6812 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006813
Victor Stinner8c62be82010-05-06 00:08:46 +00006814 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006815}
6816#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6817
6818#ifdef HAVE_WAIT3
6819PyDoc_STRVAR(posix_wait3__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006820"wait3(options) -> (pid, status, rusage)\n\n\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006821Wait for completion of a child process.");
6822
6823static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006824posix_wait3(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006825{
Victor Stinner8c62be82010-05-06 00:08:46 +00006826 pid_t pid;
6827 int options;
6828 struct rusage ru;
6829 WAIT_TYPE status;
6830 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006831
Victor Stinner4195b5c2012-02-08 23:03:19 +01006832 if (!PyArg_ParseTuple(args, "i:wait3", &options))
Victor Stinner8c62be82010-05-06 00:08:46 +00006833 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006834
Victor Stinner8c62be82010-05-06 00:08:46 +00006835 Py_BEGIN_ALLOW_THREADS
6836 pid = wait3(&status, options, &ru);
6837 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006838
Victor Stinner4195b5c2012-02-08 23:03:19 +01006839 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006840}
6841#endif /* HAVE_WAIT3 */
6842
6843#ifdef HAVE_WAIT4
6844PyDoc_STRVAR(posix_wait4__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006845"wait4(pid, options) -> (pid, status, rusage)\n\n\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006846Wait for completion of a given child process.");
6847
6848static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006849posix_wait4(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006850{
Victor Stinner8c62be82010-05-06 00:08:46 +00006851 pid_t pid;
6852 int options;
6853 struct rusage ru;
6854 WAIT_TYPE status;
6855 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006856
Victor Stinner4195b5c2012-02-08 23:03:19 +01006857 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
Victor Stinner8c62be82010-05-06 00:08:46 +00006858 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006859
Victor Stinner8c62be82010-05-06 00:08:46 +00006860 Py_BEGIN_ALLOW_THREADS
6861 pid = wait4(pid, &status, options, &ru);
6862 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006863
Victor Stinner4195b5c2012-02-08 23:03:19 +01006864 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006865}
6866#endif /* HAVE_WAIT4 */
6867
Ross Lagerwall7807c352011-03-17 20:20:30 +02006868#if defined(HAVE_WAITID) && !defined(__APPLE__)
6869PyDoc_STRVAR(posix_waitid__doc__,
6870"waitid(idtype, id, options) -> waitid_result\n\n\
6871Wait for the completion of one or more child processes.\n\n\
6872idtype can be P_PID, P_PGID or P_ALL.\n\
6873id specifies the pid to wait on.\n\
6874options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
6875or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
6876Returns either waitid_result or None if WNOHANG is specified and there are\n\
6877no children in a waitable state.");
6878
6879static PyObject *
6880posix_waitid(PyObject *self, PyObject *args)
6881{
6882 PyObject *result;
6883 idtype_t idtype;
6884 id_t id;
6885 int options, res;
6886 siginfo_t si;
6887 si.si_pid = 0;
6888 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
6889 return NULL;
6890 Py_BEGIN_ALLOW_THREADS
6891 res = waitid(idtype, id, &si, options);
6892 Py_END_ALLOW_THREADS
6893 if (res == -1)
6894 return posix_error();
6895
6896 if (si.si_pid == 0)
6897 Py_RETURN_NONE;
6898
6899 result = PyStructSequence_New(&WaitidResultType);
6900 if (!result)
6901 return NULL;
6902
6903 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
6904 PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid));
6905 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
6906 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
6907 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
6908 if (PyErr_Occurred()) {
6909 Py_DECREF(result);
6910 return NULL;
6911 }
6912
6913 return result;
6914}
6915#endif
6916
Guido van Rossumb6775db1994-08-01 11:34:53 +00006917#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006918PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006919"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006920Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006921
Barry Warsaw53699e91996-12-10 23:23:01 +00006922static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006923posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006924{
Victor Stinner8c62be82010-05-06 00:08:46 +00006925 pid_t pid;
6926 int options;
6927 WAIT_TYPE status;
6928 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006929
Victor Stinner8c62be82010-05-06 00:08:46 +00006930 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6931 return NULL;
6932 Py_BEGIN_ALLOW_THREADS
6933 pid = waitpid(pid, &status, options);
6934 Py_END_ALLOW_THREADS
6935 if (pid == -1)
6936 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006937
Victor Stinner8c62be82010-05-06 00:08:46 +00006938 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006939}
6940
Tim Petersab034fa2002-02-01 11:27:43 +00006941#elif defined(HAVE_CWAIT)
6942
6943/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006944PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006945"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006946"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00006947
6948static PyObject *
6949posix_waitpid(PyObject *self, PyObject *args)
6950{
Victor Stinner8c62be82010-05-06 00:08:46 +00006951 Py_intptr_t pid;
6952 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006953
Victor Stinner8c62be82010-05-06 00:08:46 +00006954 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6955 return NULL;
6956 Py_BEGIN_ALLOW_THREADS
6957 pid = _cwait(&status, pid, options);
6958 Py_END_ALLOW_THREADS
6959 if (pid == -1)
6960 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006961
Victor Stinner8c62be82010-05-06 00:08:46 +00006962 /* shift the status left a byte so this is more like the POSIX waitpid */
6963 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006964}
6965#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006966
Guido van Rossumad0ee831995-03-01 10:34:45 +00006967#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006968PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006969"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006970Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006971
Barry Warsaw53699e91996-12-10 23:23:01 +00006972static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006973posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006974{
Victor Stinner8c62be82010-05-06 00:08:46 +00006975 pid_t pid;
6976 WAIT_TYPE status;
6977 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006978
Victor Stinner8c62be82010-05-06 00:08:46 +00006979 Py_BEGIN_ALLOW_THREADS
6980 pid = wait(&status);
6981 Py_END_ALLOW_THREADS
6982 if (pid == -1)
6983 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006984
Victor Stinner8c62be82010-05-06 00:08:46 +00006985 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006986}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006987#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006988
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006989
Larry Hastings9cf065c2012-06-22 16:30:09 -07006990#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
6991PyDoc_STRVAR(readlink__doc__,
6992"readlink(path, *, dir_fd=None) -> path\n\n\
6993Return a string representing the path to which the symbolic link points.\n\
6994\n\
6995If dir_fd is not None, it should be a file descriptor open to a directory,\n\
6996 and path should be relative; path will then be relative to that directory.\n\
6997dir_fd may not be implemented on your platform.\n\
6998 If it is unavailable, using it will raise a NotImplementedError.");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006999#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007000
Guido van Rossumb6775db1994-08-01 11:34:53 +00007001#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007002
Barry Warsaw53699e91996-12-10 23:23:01 +00007003static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07007004posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007005{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007006 path_t path;
7007 int dir_fd = DEFAULT_DIR_FD;
7008 char buffer[MAXPATHLEN];
7009 ssize_t length;
7010 PyObject *return_value = NULL;
7011 static char *keywords[] = {"path", "dir_fd", NULL};
Thomas Wouters89f507f2006-12-13 04:49:30 +00007012
Larry Hastings9cf065c2012-06-22 16:30:09 -07007013 memset(&path, 0, sizeof(path));
7014 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords,
7015 path_converter, &path,
7016#ifdef HAVE_READLINKAT
7017 dir_fd_converter, &dir_fd
7018#else
7019 dir_fd_unavailable, &dir_fd
7020#endif
7021 ))
Victor Stinner8c62be82010-05-06 00:08:46 +00007022 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00007023
Victor Stinner8c62be82010-05-06 00:08:46 +00007024 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007025#ifdef HAVE_READLINKAT
7026 if (dir_fd != DEFAULT_DIR_FD)
7027 length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer));
Victor Stinnera45598a2010-05-14 16:35:39 +00007028 else
Larry Hastings9cf065c2012-06-22 16:30:09 -07007029#endif
7030 length = readlink(path.narrow, buffer, sizeof(buffer));
7031 Py_END_ALLOW_THREADS
7032
7033 if (length < 0) {
7034 return_value = path_posix_error("readlink", &path);
7035 goto exit;
7036 }
7037
7038 if (PyUnicode_Check(path.object))
7039 return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length);
7040 else
7041 return_value = PyBytes_FromStringAndSize(buffer, length);
7042exit:
7043 path_cleanup(&path);
7044 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007045}
Larry Hastings9cf065c2012-06-22 16:30:09 -07007046
7047
Guido van Rossumb6775db1994-08-01 11:34:53 +00007048#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007049
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007050
Larry Hastings9cf065c2012-06-22 16:30:09 -07007051#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007052PyDoc_STRVAR(posix_symlink__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07007053"symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
7054Create a symbolic link pointing to src named dst.\n\n\
7055target_is_directory is required on Windows if the target is to be\n\
7056 interpreted as a directory. (On Windows, symlink requires\n\
7057 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)\n\
7058 target_is_directory is ignored on non-Windows platforms.\n\
7059\n\
7060If dir_fd is not None, it should be a file descriptor open to a directory,\n\
7061 and path should be relative; path will then be relative to that directory.\n\
7062dir_fd may not be implemented on your platform.\n\
7063 If it is unavailable, using it will raise a NotImplementedError.");
7064
7065#if defined(MS_WINDOWS)
7066
7067/* Grab CreateSymbolicLinkW dynamically from kernel32 */
7068static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD) = NULL;
7069static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPSTR, LPSTR, DWORD) = NULL;
7070static int
7071check_CreateSymbolicLink()
7072{
7073 HINSTANCE hKernel32;
7074 /* only recheck */
7075 if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA)
7076 return 1;
7077 hKernel32 = GetModuleHandleW(L"KERNEL32");
7078 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
7079 "CreateSymbolicLinkW");
7080 *(FARPROC*)&Py_CreateSymbolicLinkA = GetProcAddress(hKernel32,
7081 "CreateSymbolicLinkA");
7082 return (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA);
7083}
7084
7085#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007086
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007087static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07007088posix_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007089{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007090 path_t src;
7091 path_t dst;
7092 int dir_fd = DEFAULT_DIR_FD;
7093 int target_is_directory = 0;
7094 static char *keywords[] = {"src", "dst", "target_is_directory",
7095 "dir_fd", NULL};
7096 PyObject *return_value;
7097#ifdef MS_WINDOWS
7098 DWORD result;
7099#else
7100 int result;
7101#endif
7102
7103 memset(&src, 0, sizeof(src));
7104 src.argument_name = "src";
7105 memset(&dst, 0, sizeof(dst));
7106 dst.argument_name = "dst";
7107
7108#ifdef MS_WINDOWS
7109 if (!check_CreateSymbolicLink()) {
7110 PyErr_SetString(PyExc_NotImplementedError,
7111 "CreateSymbolicLink functions not found");
7112 return NULL;
7113 }
7114 if (!win32_can_symlink) {
7115 PyErr_SetString(PyExc_OSError, "symbolic link privilege not held");
7116 return NULL;
7117 }
7118#endif
7119
7120 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|i$O&:symlink",
7121 keywords,
7122 path_converter, &src,
7123 path_converter, &dst,
7124 &target_is_directory,
7125#ifdef HAVE_SYMLINKAT
7126 dir_fd_converter, &dir_fd
7127#else
7128 dir_fd_unavailable, &dir_fd
7129#endif
7130 ))
7131 return NULL;
7132
7133 if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) {
7134 PyErr_SetString(PyExc_ValueError,
7135 "symlink: src and dst must be the same type");
7136 return_value = NULL;
7137 goto exit;
7138 }
7139
7140#ifdef MS_WINDOWS
7141 Py_BEGIN_ALLOW_THREADS
7142 if (dst.wide)
7143 result = Py_CreateSymbolicLinkW(dst.wide, src.wide,
7144 target_is_directory);
7145 else
7146 result = Py_CreateSymbolicLinkA(dst.narrow, src.narrow,
7147 target_is_directory);
7148 Py_END_ALLOW_THREADS
7149
7150 if (!result) {
7151 return_value = win32_error_object("symlink", src.object);
7152 goto exit;
7153 }
7154
7155#else
7156
7157 Py_BEGIN_ALLOW_THREADS
7158#if HAVE_SYMLINKAT
7159 if (dir_fd != DEFAULT_DIR_FD)
7160 result = symlinkat(src.narrow, dir_fd, dst.narrow);
7161 else
7162#endif
7163 result = symlink(src.narrow, dst.narrow);
7164 Py_END_ALLOW_THREADS
7165
7166 if (result) {
7167 return_value = path_error("symlink", &dst);
7168 goto exit;
7169 }
7170#endif
7171
7172 return_value = Py_None;
7173 Py_INCREF(Py_None);
7174 goto exit; /* silence "unused label" warning */
7175exit:
7176 path_cleanup(&src);
7177 path_cleanup(&dst);
7178 return return_value;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007179}
Larry Hastings9cf065c2012-06-22 16:30:09 -07007180
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007181#endif /* HAVE_SYMLINK */
7182
Larry Hastings9cf065c2012-06-22 16:30:09 -07007183
Brian Curtind40e6f72010-07-08 21:39:08 +00007184#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
7185
Brian Curtind40e6f72010-07-08 21:39:08 +00007186static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07007187win_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
Brian Curtind40e6f72010-07-08 21:39:08 +00007188{
7189 wchar_t *path;
7190 DWORD n_bytes_returned;
7191 DWORD io_result;
Victor Stinnereb5657a2011-09-30 01:44:27 +02007192 PyObject *po, *result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007193 int dir_fd;
Brian Curtind40e6f72010-07-08 21:39:08 +00007194 HANDLE reparse_point_handle;
7195
7196 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
7197 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
7198 wchar_t *print_name;
7199
Larry Hastings9cf065c2012-06-22 16:30:09 -07007200 static char *keywords[] = {"path", "dir_fd", NULL};
7201
7202 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords,
7203 &po,
7204 dir_fd_unavailable, &dir_fd
7205 ))
Victor Stinnereb5657a2011-09-30 01:44:27 +02007206 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007207
Victor Stinnereb5657a2011-09-30 01:44:27 +02007208 path = PyUnicode_AsUnicode(po);
7209 if (path == NULL)
Brian Curtind40e6f72010-07-08 21:39:08 +00007210 return NULL;
7211
7212 /* First get a handle to the reparse point */
7213 Py_BEGIN_ALLOW_THREADS
7214 reparse_point_handle = CreateFileW(
7215 path,
7216 0,
7217 0,
7218 0,
7219 OPEN_EXISTING,
7220 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
7221 0);
7222 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007223
Brian Curtind40e6f72010-07-08 21:39:08 +00007224 if (reparse_point_handle==INVALID_HANDLE_VALUE)
Victor Stinnereb5657a2011-09-30 01:44:27 +02007225 return win32_error_object("readlink", po);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007226
Brian Curtind40e6f72010-07-08 21:39:08 +00007227 Py_BEGIN_ALLOW_THREADS
7228 /* New call DeviceIoControl to read the reparse point */
7229 io_result = DeviceIoControl(
7230 reparse_point_handle,
7231 FSCTL_GET_REPARSE_POINT,
7232 0, 0, /* in buffer */
7233 target_buffer, sizeof(target_buffer),
7234 &n_bytes_returned,
7235 0 /* we're not using OVERLAPPED_IO */
7236 );
7237 CloseHandle(reparse_point_handle);
7238 Py_END_ALLOW_THREADS
7239
7240 if (io_result==0)
Victor Stinnereb5657a2011-09-30 01:44:27 +02007241 return win32_error_object("readlink", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00007242
7243 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
7244 {
7245 PyErr_SetString(PyExc_ValueError,
7246 "not a symbolic link");
7247 return NULL;
7248 }
Brian Curtin74e45612010-07-09 15:58:59 +00007249 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
7250 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
7251
7252 result = PyUnicode_FromWideChar(print_name,
7253 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00007254 return result;
7255}
7256
7257#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
7258
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007259
Larry Hastings605a62d2012-06-24 04:33:36 -07007260static PyStructSequence_Field times_result_fields[] = {
7261 {"user", "user time"},
7262 {"system", "system time"},
7263 {"children_user", "user time of children"},
7264 {"children_system", "system time of children"},
7265 {"elapsed", "elapsed time since an arbitrary point in the past"},
7266 {NULL}
7267};
7268
7269PyDoc_STRVAR(times_result__doc__,
7270"times_result: Result from os.times().\n\n\
7271This object may be accessed either as a tuple of\n\
7272 (user, system, children_user, children_system, elapsed),\n\
7273or via the attributes user, system, children_user, children_system,\n\
7274and elapsed.\n\
7275\n\
7276See os.times for more information.");
7277
7278static PyStructSequence_Desc times_result_desc = {
7279 "times_result", /* name */
7280 times_result__doc__, /* doc */
7281 times_result_fields,
7282 5
7283};
7284
7285static PyTypeObject TimesResultType;
7286
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007287#ifdef MS_WINDOWS
7288#define HAVE_TIMES /* mandatory, for the method table */
7289#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07007290
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007291#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07007292
7293static PyObject *
7294build_times_result(double user, double system,
7295 double children_user, double children_system,
7296 double elapsed)
7297{
7298 PyObject *value = PyStructSequence_New(&TimesResultType);
7299 if (value == NULL)
7300 return NULL;
7301
7302#define SET(i, field) \
7303 { \
7304 PyObject *o = PyFloat_FromDouble(field); \
7305 if (!o) { \
7306 Py_DECREF(value); \
7307 return NULL; \
7308 } \
7309 PyStructSequence_SET_ITEM(value, i, o); \
7310 } \
7311
7312 SET(0, user);
7313 SET(1, system);
7314 SET(2, children_user);
7315 SET(3, children_system);
7316 SET(4, elapsed);
7317
7318#undef SET
7319
7320 return value;
7321}
7322
7323PyDoc_STRVAR(posix_times__doc__,
7324"times() -> times_result\n\n\
7325Return an object containing floating point numbers indicating process\n\
7326times. The object behaves like a named tuple with these fields:\n\
7327 (utime, stime, cutime, cstime, elapsed_time)");
7328
Guido van Rossumd48f2521997-12-05 22:19:34 +00007329#if defined(PYCC_VACPP) && defined(PYOS_OS2)
7330static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007331system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007332{
7333 ULONG value = 0;
7334
7335 Py_BEGIN_ALLOW_THREADS
7336 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
7337 Py_END_ALLOW_THREADS
7338
7339 return value;
7340}
7341
7342static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007343posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007344{
Guido van Rossumd48f2521997-12-05 22:19:34 +00007345 /* Currently Only Uptime is Provided -- Others Later */
Larry Hastings605a62d2012-06-24 04:33:36 -07007346 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00007347 (double)0 /* t.tms_utime / HZ */,
7348 (double)0 /* t.tms_stime / HZ */,
7349 (double)0 /* t.tms_cutime / HZ */,
7350 (double)0 /* t.tms_cstime / HZ */,
7351 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007352}
Larry Hastings605a62d2012-06-24 04:33:36 -07007353#elif defined(MS_WINDOWS)
Barry Warsaw53699e91996-12-10 23:23:01 +00007354static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007355posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00007356{
Victor Stinner8c62be82010-05-06 00:08:46 +00007357 FILETIME create, exit, kernel, user;
7358 HANDLE hProc;
7359 hProc = GetCurrentProcess();
7360 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
7361 /* The fields of a FILETIME structure are the hi and lo part
7362 of a 64-bit value expressed in 100 nanosecond units.
7363 1e7 is one second in such units; 1e-7 the inverse.
7364 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
7365 */
Larry Hastings605a62d2012-06-24 04:33:36 -07007366 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00007367 (double)(user.dwHighDateTime*429.4967296 +
7368 user.dwLowDateTime*1e-7),
7369 (double)(kernel.dwHighDateTime*429.4967296 +
7370 kernel.dwLowDateTime*1e-7),
7371 (double)0,
7372 (double)0,
7373 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00007374}
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007375#else /* Neither Windows nor OS/2 */
7376#define NEED_TICKS_PER_SECOND
7377static long ticks_per_second = -1;
7378static PyObject *
7379posix_times(PyObject *self, PyObject *noargs)
7380{
7381 struct tms t;
7382 clock_t c;
7383 errno = 0;
7384 c = times(&t);
7385 if (c == (clock_t) -1)
7386 return posix_error();
7387 return build_times_result(
7388 (double)t.tms_utime / ticks_per_second,
7389 (double)t.tms_stime / ticks_per_second,
7390 (double)t.tms_cutime / ticks_per_second,
7391 (double)t.tms_cstime / ticks_per_second,
7392 (double)c / ticks_per_second);
7393}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007394#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00007395
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007396#endif /* HAVE_TIMES */
7397
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007398
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007399#ifdef HAVE_GETSID
7400PyDoc_STRVAR(posix_getsid__doc__,
7401"getsid(pid) -> sid\n\n\
7402Call the system call getsid().");
7403
7404static PyObject *
7405posix_getsid(PyObject *self, PyObject *args)
7406{
Victor Stinner8c62be82010-05-06 00:08:46 +00007407 pid_t pid;
7408 int sid;
7409 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
7410 return NULL;
7411 sid = getsid(pid);
7412 if (sid < 0)
7413 return posix_error();
7414 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007415}
7416#endif /* HAVE_GETSID */
7417
7418
Guido van Rossumb6775db1994-08-01 11:34:53 +00007419#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007420PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007421"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007422Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007423
Barry Warsaw53699e91996-12-10 23:23:01 +00007424static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007425posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00007426{
Victor Stinner8c62be82010-05-06 00:08:46 +00007427 if (setsid() < 0)
7428 return posix_error();
7429 Py_INCREF(Py_None);
7430 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007431}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007432#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007433
Guido van Rossumb6775db1994-08-01 11:34:53 +00007434#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007435PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007436"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007437Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007438
Barry Warsaw53699e91996-12-10 23:23:01 +00007439static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007440posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00007441{
Victor Stinner8c62be82010-05-06 00:08:46 +00007442 pid_t pid;
7443 int pgrp;
7444 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
7445 return NULL;
7446 if (setpgid(pid, pgrp) < 0)
7447 return posix_error();
7448 Py_INCREF(Py_None);
7449 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007450}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007451#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007452
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007453
Guido van Rossumb6775db1994-08-01 11:34:53 +00007454#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007455PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007456"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007457Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007458
Barry Warsaw53699e91996-12-10 23:23:01 +00007459static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007460posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00007461{
Victor Stinner8c62be82010-05-06 00:08:46 +00007462 int fd;
7463 pid_t pgid;
7464 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
7465 return NULL;
7466 pgid = tcgetpgrp(fd);
7467 if (pgid < 0)
7468 return posix_error();
7469 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00007470}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007471#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00007472
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007473
Guido van Rossumb6775db1994-08-01 11:34:53 +00007474#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007475PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007476"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007477Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007478
Barry Warsaw53699e91996-12-10 23:23:01 +00007479static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007480posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00007481{
Victor Stinner8c62be82010-05-06 00:08:46 +00007482 int fd;
7483 pid_t pgid;
7484 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
7485 return NULL;
7486 if (tcsetpgrp(fd, pgid) < 0)
7487 return posix_error();
7488 Py_INCREF(Py_None);
7489 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00007490}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007491#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00007492
Guido van Rossum687dd131993-05-17 08:34:16 +00007493/* Functions acting on file descriptors */
7494
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007495PyDoc_STRVAR(posix_open__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07007496"open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
7497Open a file for low level IO. Returns a file handle (integer).\n\
7498\n\
7499If dir_fd is not None, it should be a file descriptor open to a directory,\n\
7500 and path should be relative; path will then be relative to that directory.\n\
7501dir_fd may not be implemented on your platform.\n\
7502 If it is unavailable, using it will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007503
Barry Warsaw53699e91996-12-10 23:23:01 +00007504static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07007505posix_open(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00007506{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007507 path_t path;
7508 int flags;
Victor Stinner8c62be82010-05-06 00:08:46 +00007509 int mode = 0777;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007510 int dir_fd = DEFAULT_DIR_FD;
Victor Stinner8c62be82010-05-06 00:08:46 +00007511 int fd;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007512 PyObject *return_value = NULL;
7513 static char *keywords[] = {"path", "flags", "mode", "dir_fd", NULL};
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007514
Larry Hastings9cf065c2012-06-22 16:30:09 -07007515 memset(&path, 0, sizeof(path));
7516 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|i$O&:open", keywords,
7517 path_converter, &path,
7518 &flags, &mode,
7519#ifdef HAVE_OPENAT
7520 dir_fd_converter, &dir_fd
7521#else
7522 dir_fd_unavailable, &dir_fd
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007523#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07007524 ))
7525 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007526
Victor Stinner8c62be82010-05-06 00:08:46 +00007527 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007528#ifdef MS_WINDOWS
7529 if (path.wide)
7530 fd = _wopen(path.wide, flags, mode);
7531 else
7532#endif
7533#ifdef HAVE_OPENAT
7534 if (dir_fd != DEFAULT_DIR_FD)
7535 fd = openat(dir_fd, path.narrow, flags, mode);
7536 else
7537#endif
7538 fd = open(path.narrow, flags, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00007539 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00007540
Larry Hastings9cf065c2012-06-22 16:30:09 -07007541 if (fd == -1) {
7542#ifdef MS_WINDOWS
7543 /* force use of posix_error here for exact backwards compatibility */
7544 if (path.wide)
7545 return_value = posix_error();
7546 else
7547#endif
7548 return_value = path_error("open", &path);
7549 goto exit;
7550 }
7551
7552 return_value = PyLong_FromLong((long)fd);
7553
7554exit:
7555 path_cleanup(&path);
7556 return return_value;
7557}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007558
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007559PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007560"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007561Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007562
Barry Warsaw53699e91996-12-10 23:23:01 +00007563static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007564posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007565{
Victor Stinner8c62be82010-05-06 00:08:46 +00007566 int fd, res;
7567 if (!PyArg_ParseTuple(args, "i:close", &fd))
7568 return NULL;
7569 if (!_PyVerify_fd(fd))
7570 return posix_error();
7571 Py_BEGIN_ALLOW_THREADS
7572 res = close(fd);
7573 Py_END_ALLOW_THREADS
7574 if (res < 0)
7575 return posix_error();
7576 Py_INCREF(Py_None);
7577 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00007578}
7579
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007580
Victor Stinner8c62be82010-05-06 00:08:46 +00007581PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00007582"closerange(fd_low, fd_high)\n\n\
7583Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
7584
7585static PyObject *
7586posix_closerange(PyObject *self, PyObject *args)
7587{
Victor Stinner8c62be82010-05-06 00:08:46 +00007588 int fd_from, fd_to, i;
7589 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
7590 return NULL;
7591 Py_BEGIN_ALLOW_THREADS
7592 for (i = fd_from; i < fd_to; i++)
7593 if (_PyVerify_fd(i))
7594 close(i);
7595 Py_END_ALLOW_THREADS
7596 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00007597}
7598
7599
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007600PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007601"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007602Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007603
Barry Warsaw53699e91996-12-10 23:23:01 +00007604static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007605posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007606{
Victor Stinner8c62be82010-05-06 00:08:46 +00007607 int fd;
7608 if (!PyArg_ParseTuple(args, "i:dup", &fd))
7609 return NULL;
7610 if (!_PyVerify_fd(fd))
7611 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00007612 fd = dup(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00007613 if (fd < 0)
7614 return posix_error();
7615 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00007616}
7617
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007618
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007619PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00007620"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007621Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007622
Barry Warsaw53699e91996-12-10 23:23:01 +00007623static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007624posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007625{
Victor Stinner8c62be82010-05-06 00:08:46 +00007626 int fd, fd2, res;
7627 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
7628 return NULL;
7629 if (!_PyVerify_fd_dup2(fd, fd2))
7630 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00007631 res = dup2(fd, fd2);
Victor Stinner8c62be82010-05-06 00:08:46 +00007632 if (res < 0)
7633 return posix_error();
7634 Py_INCREF(Py_None);
7635 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00007636}
7637
Ross Lagerwall7807c352011-03-17 20:20:30 +02007638#ifdef HAVE_LOCKF
7639PyDoc_STRVAR(posix_lockf__doc__,
7640"lockf(fd, cmd, len)\n\n\
7641Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
7642fd is an open file descriptor.\n\
7643cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
7644F_TEST.\n\
7645len specifies the section of the file to lock.");
7646
7647static PyObject *
7648posix_lockf(PyObject *self, PyObject *args)
7649{
7650 int fd, cmd, res;
7651 off_t len;
7652 if (!PyArg_ParseTuple(args, "iiO&:lockf",
7653 &fd, &cmd, _parse_off_t, &len))
7654 return NULL;
7655
7656 Py_BEGIN_ALLOW_THREADS
7657 res = lockf(fd, cmd, len);
7658 Py_END_ALLOW_THREADS
7659
7660 if (res < 0)
7661 return posix_error();
7662
7663 Py_RETURN_NONE;
7664}
7665#endif
7666
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007667
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007668PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007669"lseek(fd, pos, how) -> newpos\n\n\
Victor Stinnere83f8992011-12-17 23:15:09 +01007670Set the current position of a file descriptor.\n\
7671Return the new cursor position in bytes, starting from the beginning.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007672
Barry Warsaw53699e91996-12-10 23:23:01 +00007673static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007674posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007675{
Victor Stinner8c62be82010-05-06 00:08:46 +00007676 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007677#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007678 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00007679#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007680 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00007681#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02007682 PyObject *posobj;
7683 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00007684 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00007685#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00007686 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
7687 switch (how) {
7688 case 0: how = SEEK_SET; break;
7689 case 1: how = SEEK_CUR; break;
7690 case 2: how = SEEK_END; break;
7691 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007692#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007693
Ross Lagerwall8e749672011-03-17 21:54:07 +02007694#if !defined(HAVE_LARGEFILE_SUPPORT)
7695 pos = PyLong_AsLong(posobj);
7696#else
7697 pos = PyLong_AsLongLong(posobj);
7698#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007699 if (PyErr_Occurred())
7700 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00007701
Victor Stinner8c62be82010-05-06 00:08:46 +00007702 if (!_PyVerify_fd(fd))
7703 return posix_error();
7704 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007705#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007706 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00007707#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007708 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00007709#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007710 Py_END_ALLOW_THREADS
7711 if (res < 0)
7712 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00007713
7714#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007715 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007716#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007717 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007718#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00007719}
7720
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007721
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007722PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007723"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007724Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007725
Barry Warsaw53699e91996-12-10 23:23:01 +00007726static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007727posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007728{
Victor Stinner8c62be82010-05-06 00:08:46 +00007729 int fd, size;
7730 Py_ssize_t n;
7731 PyObject *buffer;
7732 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
7733 return NULL;
7734 if (size < 0) {
7735 errno = EINVAL;
7736 return posix_error();
7737 }
7738 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
7739 if (buffer == NULL)
7740 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00007741 if (!_PyVerify_fd(fd)) {
7742 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00007743 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00007744 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007745 Py_BEGIN_ALLOW_THREADS
7746 n = read(fd, PyBytes_AS_STRING(buffer), size);
7747 Py_END_ALLOW_THREADS
7748 if (n < 0) {
7749 Py_DECREF(buffer);
7750 return posix_error();
7751 }
7752 if (n != size)
7753 _PyBytes_Resize(&buffer, n);
7754 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00007755}
7756
Ross Lagerwall7807c352011-03-17 20:20:30 +02007757#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
7758 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007759static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007760iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
7761{
7762 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007763 Py_ssize_t blen, total = 0;
7764
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007765 *iov = PyMem_New(struct iovec, cnt);
7766 if (*iov == NULL) {
7767 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007768 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007769 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007770
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007771 *buf = PyMem_New(Py_buffer, cnt);
7772 if (*buf == NULL) {
7773 PyMem_Del(*iov);
7774 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007775 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007776 }
7777
7778 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007779 PyObject *item = PySequence_GetItem(seq, i);
7780 if (item == NULL)
7781 goto fail;
7782 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
7783 Py_DECREF(item);
7784 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007785 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007786 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007787 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007788 blen = (*buf)[i].len;
7789 (*iov)[i].iov_len = blen;
7790 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007791 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007792 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007793
7794fail:
7795 PyMem_Del(*iov);
7796 for (j = 0; j < i; j++) {
7797 PyBuffer_Release(&(*buf)[j]);
7798 }
7799 PyMem_Del(*buf);
7800 return 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007801}
7802
7803static void
7804iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
7805{
7806 int i;
7807 PyMem_Del(iov);
7808 for (i = 0; i < cnt; i++) {
7809 PyBuffer_Release(&buf[i]);
7810 }
7811 PyMem_Del(buf);
7812}
7813#endif
7814
Ross Lagerwall7807c352011-03-17 20:20:30 +02007815#ifdef HAVE_READV
7816PyDoc_STRVAR(posix_readv__doc__,
7817"readv(fd, buffers) -> bytesread\n\n\
7818Read from a file descriptor into a number of writable buffers. buffers\n\
7819is an arbitrary sequence of writable buffers.\n\
7820Returns the total number of bytes read.");
7821
7822static PyObject *
7823posix_readv(PyObject *self, PyObject *args)
7824{
7825 int fd, cnt;
7826 Py_ssize_t n;
7827 PyObject *seq;
7828 struct iovec *iov;
7829 Py_buffer *buf;
7830
7831 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
7832 return NULL;
7833 if (!PySequence_Check(seq)) {
7834 PyErr_SetString(PyExc_TypeError,
7835 "readv() arg 2 must be a sequence");
7836 return NULL;
7837 }
7838 cnt = PySequence_Size(seq);
7839
7840 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
7841 return NULL;
7842
7843 Py_BEGIN_ALLOW_THREADS
7844 n = readv(fd, iov, cnt);
7845 Py_END_ALLOW_THREADS
7846
7847 iov_cleanup(iov, buf, cnt);
7848 return PyLong_FromSsize_t(n);
7849}
7850#endif
7851
7852#ifdef HAVE_PREAD
7853PyDoc_STRVAR(posix_pread__doc__,
7854"pread(fd, buffersize, offset) -> string\n\n\
7855Read from a file descriptor, fd, at a position of offset. It will read up\n\
7856to buffersize number of bytes. The file offset remains unchanged.");
7857
7858static PyObject *
7859posix_pread(PyObject *self, PyObject *args)
7860{
7861 int fd, size;
7862 off_t offset;
7863 Py_ssize_t n;
7864 PyObject *buffer;
7865 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
7866 return NULL;
7867
7868 if (size < 0) {
7869 errno = EINVAL;
7870 return posix_error();
7871 }
7872 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
7873 if (buffer == NULL)
7874 return NULL;
7875 if (!_PyVerify_fd(fd)) {
7876 Py_DECREF(buffer);
7877 return posix_error();
7878 }
7879 Py_BEGIN_ALLOW_THREADS
7880 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
7881 Py_END_ALLOW_THREADS
7882 if (n < 0) {
7883 Py_DECREF(buffer);
7884 return posix_error();
7885 }
7886 if (n != size)
7887 _PyBytes_Resize(&buffer, n);
7888 return buffer;
7889}
7890#endif
7891
7892PyDoc_STRVAR(posix_write__doc__,
7893"write(fd, string) -> byteswritten\n\n\
7894Write a string to a file descriptor.");
7895
7896static PyObject *
7897posix_write(PyObject *self, PyObject *args)
7898{
7899 Py_buffer pbuf;
7900 int fd;
7901 Py_ssize_t size, len;
7902
7903 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
7904 return NULL;
7905 if (!_PyVerify_fd(fd)) {
7906 PyBuffer_Release(&pbuf);
7907 return posix_error();
7908 }
7909 len = pbuf.len;
7910 Py_BEGIN_ALLOW_THREADS
7911#if defined(MS_WIN64) || defined(MS_WINDOWS)
7912 if (len > INT_MAX)
7913 len = INT_MAX;
7914 size = write(fd, pbuf.buf, (int)len);
7915#else
7916 size = write(fd, pbuf.buf, len);
7917#endif
7918 Py_END_ALLOW_THREADS
7919 PyBuffer_Release(&pbuf);
7920 if (size < 0)
7921 return posix_error();
7922 return PyLong_FromSsize_t(size);
7923}
7924
7925#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007926PyDoc_STRVAR(posix_sendfile__doc__,
7927"sendfile(out, in, offset, nbytes) -> byteswritten\n\
7928sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
7929 -> byteswritten\n\
7930Copy nbytes bytes from file descriptor in to file descriptor out.");
7931
7932static PyObject *
7933posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
7934{
7935 int in, out;
7936 Py_ssize_t ret;
7937 off_t offset;
7938
7939#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
7940#ifndef __APPLE__
7941 Py_ssize_t len;
7942#endif
7943 PyObject *headers = NULL, *trailers = NULL;
7944 Py_buffer *hbuf, *tbuf;
7945 off_t sbytes;
7946 struct sf_hdtr sf;
7947 int flags = 0;
7948 sf.headers = NULL;
7949 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00007950 static char *keywords[] = {"out", "in",
7951 "offset", "count",
7952 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007953
7954#ifdef __APPLE__
7955 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007956 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007957#else
7958 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007959 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007960#endif
7961 &headers, &trailers, &flags))
7962 return NULL;
7963 if (headers != NULL) {
7964 if (!PySequence_Check(headers)) {
7965 PyErr_SetString(PyExc_TypeError,
7966 "sendfile() headers must be a sequence or None");
7967 return NULL;
7968 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007969 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007970 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007971 if (sf.hdr_cnt > 0 &&
7972 !(i = iov_setup(&(sf.headers), &hbuf,
7973 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007974 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007975#ifdef __APPLE__
7976 sbytes += i;
7977#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007978 }
7979 }
7980 if (trailers != NULL) {
7981 if (!PySequence_Check(trailers)) {
7982 PyErr_SetString(PyExc_TypeError,
7983 "sendfile() trailers must be a sequence or None");
7984 return NULL;
7985 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007986 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007987 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007988 if (sf.trl_cnt > 0 &&
7989 !(i = iov_setup(&(sf.trailers), &tbuf,
7990 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007991 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007992#ifdef __APPLE__
7993 sbytes += i;
7994#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007995 }
7996 }
7997
7998 Py_BEGIN_ALLOW_THREADS
7999#ifdef __APPLE__
8000 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
8001#else
8002 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
8003#endif
8004 Py_END_ALLOW_THREADS
8005
8006 if (sf.headers != NULL)
8007 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
8008 if (sf.trailers != NULL)
8009 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
8010
8011 if (ret < 0) {
8012 if ((errno == EAGAIN) || (errno == EBUSY)) {
8013 if (sbytes != 0) {
8014 // some data has been sent
8015 goto done;
8016 }
8017 else {
8018 // no data has been sent; upper application is supposed
8019 // to retry on EAGAIN or EBUSY
8020 return posix_error();
8021 }
8022 }
8023 return posix_error();
8024 }
8025 goto done;
8026
8027done:
8028 #if !defined(HAVE_LARGEFILE_SUPPORT)
8029 return Py_BuildValue("l", sbytes);
8030 #else
8031 return Py_BuildValue("L", sbytes);
8032 #endif
8033
8034#else
8035 Py_ssize_t count;
8036 PyObject *offobj;
8037 static char *keywords[] = {"out", "in",
8038 "offset", "count", NULL};
8039 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
8040 keywords, &out, &in, &offobj, &count))
8041 return NULL;
8042#ifdef linux
8043 if (offobj == Py_None) {
8044 Py_BEGIN_ALLOW_THREADS
8045 ret = sendfile(out, in, NULL, count);
8046 Py_END_ALLOW_THREADS
8047 if (ret < 0)
8048 return posix_error();
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02008049 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008050 }
8051#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00008052 if (!_parse_off_t(offobj, &offset))
8053 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008054 Py_BEGIN_ALLOW_THREADS
8055 ret = sendfile(out, in, &offset, count);
8056 Py_END_ALLOW_THREADS
8057 if (ret < 0)
8058 return posix_error();
8059 return Py_BuildValue("n", ret);
8060#endif
8061}
8062#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008063
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008064PyDoc_STRVAR(posix_fstat__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01008065"fstat(fd) -> stat result\n\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07008066Like stat(), but for an open file descriptor.\n\
8067Equivalent to stat(fd=fd).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008068
Barry Warsaw53699e91996-12-10 23:23:01 +00008069static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01008070posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00008071{
Victor Stinner8c62be82010-05-06 00:08:46 +00008072 int fd;
8073 STRUCT_STAT st;
8074 int res;
Victor Stinner4195b5c2012-02-08 23:03:19 +01008075 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Victor Stinner8c62be82010-05-06 00:08:46 +00008076 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00008077#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00008078 /* on OpenVMS we must ensure that all bytes are written to the file */
8079 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00008080#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008081 Py_BEGIN_ALLOW_THREADS
8082 res = FSTAT(fd, &st);
8083 Py_END_ALLOW_THREADS
8084 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00008085#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008086 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00008087#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008088 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00008089#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008090 }
Tim Peters5aa91602002-01-30 05:46:57 +00008091
Victor Stinner4195b5c2012-02-08 23:03:19 +01008092 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00008093}
8094
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008095PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008096"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00008097Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008098connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00008099
8100static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00008101posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00008102{
Victor Stinner8c62be82010-05-06 00:08:46 +00008103 int fd;
8104 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
8105 return NULL;
8106 if (!_PyVerify_fd(fd))
8107 return PyBool_FromLong(0);
8108 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00008109}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008110
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008111#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008112PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008113"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008114Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008115
Barry Warsaw53699e91996-12-10 23:23:01 +00008116static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008117posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00008118{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008119#if defined(PYOS_OS2)
8120 HFILE read, write;
8121 APIRET rc;
8122
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008123 rc = DosCreatePipe( &read, &write, 4096);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008124 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008125 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008126
8127 return Py_BuildValue("(ii)", read, write);
8128#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008129#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00008130 int fds[2];
8131 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00008132 res = pipe(fds);
Victor Stinner8c62be82010-05-06 00:08:46 +00008133 if (res != 0)
8134 return posix_error();
8135 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008136#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00008137 HANDLE read, write;
8138 int read_fd, write_fd;
8139 BOOL ok;
Victor Stinner8c62be82010-05-06 00:08:46 +00008140 ok = CreatePipe(&read, &write, NULL, 0);
Victor Stinner8c62be82010-05-06 00:08:46 +00008141 if (!ok)
8142 return win32_error("CreatePipe", NULL);
8143 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
8144 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
8145 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008146#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008147#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00008148}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008149#endif /* HAVE_PIPE */
8150
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008151#ifdef HAVE_PIPE2
8152PyDoc_STRVAR(posix_pipe2__doc__,
Charles-François Natali368f34b2011-06-06 19:49:47 +02008153"pipe2(flags) -> (read_end, write_end)\n\n\
8154Create a pipe with flags set atomically.\n\
8155flags can be constructed by ORing together one or more of these values:\n\
8156O_NONBLOCK, O_CLOEXEC.\n\
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008157");
8158
8159static PyObject *
Charles-François Natali368f34b2011-06-06 19:49:47 +02008160posix_pipe2(PyObject *self, PyObject *arg)
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008161{
Charles-François Natali368f34b2011-06-06 19:49:47 +02008162 int flags;
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008163 int fds[2];
8164 int res;
8165
Charles-François Natali368f34b2011-06-06 19:49:47 +02008166 flags = PyLong_AsLong(arg);
8167 if (flags == -1 && PyErr_Occurred())
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008168 return NULL;
8169
8170 res = pipe2(fds, flags);
8171 if (res != 0)
8172 return posix_error();
8173 return Py_BuildValue("(ii)", fds[0], fds[1]);
8174}
8175#endif /* HAVE_PIPE2 */
8176
Ross Lagerwall7807c352011-03-17 20:20:30 +02008177#ifdef HAVE_WRITEV
8178PyDoc_STRVAR(posix_writev__doc__,
8179"writev(fd, buffers) -> byteswritten\n\n\
8180Write the contents of buffers to a file descriptor, where buffers is an\n\
8181arbitrary sequence of buffers.\n\
8182Returns the total bytes written.");
8183
8184static PyObject *
8185posix_writev(PyObject *self, PyObject *args)
8186{
8187 int fd, cnt;
8188 Py_ssize_t res;
8189 PyObject *seq;
8190 struct iovec *iov;
8191 Py_buffer *buf;
8192 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
8193 return NULL;
8194 if (!PySequence_Check(seq)) {
8195 PyErr_SetString(PyExc_TypeError,
8196 "writev() arg 2 must be a sequence");
8197 return NULL;
8198 }
8199 cnt = PySequence_Size(seq);
8200
8201 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
8202 return NULL;
8203 }
8204
8205 Py_BEGIN_ALLOW_THREADS
8206 res = writev(fd, iov, cnt);
8207 Py_END_ALLOW_THREADS
8208
8209 iov_cleanup(iov, buf, cnt);
8210 return PyLong_FromSsize_t(res);
8211}
8212#endif
8213
8214#ifdef HAVE_PWRITE
8215PyDoc_STRVAR(posix_pwrite__doc__,
8216"pwrite(fd, string, offset) -> byteswritten\n\n\
8217Write string to a file descriptor, fd, from offset, leaving the file\n\
8218offset unchanged.");
8219
8220static PyObject *
8221posix_pwrite(PyObject *self, PyObject *args)
8222{
8223 Py_buffer pbuf;
8224 int fd;
8225 off_t offset;
8226 Py_ssize_t size;
8227
8228 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
8229 return NULL;
8230
8231 if (!_PyVerify_fd(fd)) {
8232 PyBuffer_Release(&pbuf);
8233 return posix_error();
8234 }
8235 Py_BEGIN_ALLOW_THREADS
8236 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
8237 Py_END_ALLOW_THREADS
8238 PyBuffer_Release(&pbuf);
8239 if (size < 0)
8240 return posix_error();
8241 return PyLong_FromSsize_t(size);
8242}
8243#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008244
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008245#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008246PyDoc_STRVAR(posix_mkfifo__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07008247"mkfifo(path, mode=0o666, *, dir_fd=None)\n\n\
8248Create a FIFO (a POSIX named pipe).\n\
8249\n\
8250If dir_fd is not None, it should be a file descriptor open to a directory,\n\
8251 and path should be relative; path will then be relative to that directory.\n\
8252dir_fd may not be implemented on your platform.\n\
8253 If it is unavailable, using it will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008254
Barry Warsaw53699e91996-12-10 23:23:01 +00008255static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07008256posix_mkfifo(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008257{
Larry Hastings9cf065c2012-06-22 16:30:09 -07008258 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00008259 int mode = 0666;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008260 int dir_fd = DEFAULT_DIR_FD;
8261 int result;
8262 PyObject *return_value = NULL;
8263 static char *keywords[] = {"path", "mode", "dir_fd", NULL};
8264
8265 memset(&path, 0, sizeof(path));
8266 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkfifo", keywords,
8267 path_converter, &path,
8268 &mode,
8269#ifdef HAVE_MKFIFOAT
8270 dir_fd_converter, &dir_fd
8271#else
8272 dir_fd_unavailable, &dir_fd
8273#endif
8274 ))
Victor Stinner8c62be82010-05-06 00:08:46 +00008275 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008276
Victor Stinner8c62be82010-05-06 00:08:46 +00008277 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008278#ifdef HAVE_MKFIFOAT
8279 if (dir_fd != DEFAULT_DIR_FD)
8280 result = mkfifoat(dir_fd, path.narrow, mode);
8281 else
8282#endif
8283 result = mkfifo(path.narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00008284 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008285
8286 if (result < 0) {
8287 return_value = posix_error();
8288 goto exit;
8289 }
8290
8291 return_value = Py_None;
Victor Stinner8c62be82010-05-06 00:08:46 +00008292 Py_INCREF(Py_None);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008293
8294exit:
8295 path_cleanup(&path);
8296 return return_value;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008297}
8298#endif
8299
Neal Norwitz11690112002-07-30 01:08:28 +00008300#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008301PyDoc_STRVAR(posix_mknod__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07008302"mknod(filename, mode=0o600, device=0, *, dir_fd=None)\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008303Create a filesystem node (file, device special file or named pipe)\n\
8304named filename. mode specifies both the permissions to use and the\n\
8305type of node to be created, being combined (bitwise OR) with one of\n\
8306S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008307device defines the newly created device special file (probably using\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07008308os.makedev()), otherwise it is ignored.\n\
8309\n\
8310If dir_fd is not None, it should be a file descriptor open to a directory,\n\
8311 and path should be relative; path will then be relative to that directory.\n\
8312dir_fd may not be implemented on your platform.\n\
8313 If it is unavailable, using it will raise a NotImplementedError.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008314
8315
8316static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07008317posix_mknod(PyObject *self, PyObject *args, PyObject *kwargs)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008318{
Larry Hastings9cf065c2012-06-22 16:30:09 -07008319 path_t path;
8320 int mode = 0666;
Victor Stinner8c62be82010-05-06 00:08:46 +00008321 int device = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008322 int dir_fd = DEFAULT_DIR_FD;
8323 int result;
8324 PyObject *return_value = NULL;
8325 static char *keywords[] = {"path", "mode", "device", "dir_fd", NULL};
8326
8327 memset(&path, 0, sizeof(path));
8328 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|ii$O&:mknod", keywords,
8329 path_converter, &path,
8330 &mode, &device,
8331#ifdef HAVE_MKNODAT
8332 dir_fd_converter, &dir_fd
8333#else
8334 dir_fd_unavailable, &dir_fd
8335#endif
8336 ))
Victor Stinner8c62be82010-05-06 00:08:46 +00008337 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008338
Victor Stinner8c62be82010-05-06 00:08:46 +00008339 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008340#ifdef HAVE_MKNODAT
8341 if (dir_fd != DEFAULT_DIR_FD)
8342 result = mknodat(dir_fd, path.narrow, mode, device);
8343 else
8344#endif
8345 result = mknod(path.narrow, mode, device);
Victor Stinner8c62be82010-05-06 00:08:46 +00008346 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008347
8348 if (result < 0) {
8349 return_value = posix_error();
8350 goto exit;
8351 }
8352
8353 return_value = Py_None;
Victor Stinner8c62be82010-05-06 00:08:46 +00008354 Py_INCREF(Py_None);
Georg Brandlf7875592012-06-24 13:58:31 +02008355
Larry Hastings9cf065c2012-06-22 16:30:09 -07008356exit:
8357 path_cleanup(&path);
8358 return return_value;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008359}
8360#endif
8361
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008362#ifdef HAVE_DEVICE_MACROS
8363PyDoc_STRVAR(posix_major__doc__,
8364"major(device) -> major number\n\
8365Extracts a device major number from a raw device number.");
8366
8367static PyObject *
8368posix_major(PyObject *self, PyObject *args)
8369{
Victor Stinner8c62be82010-05-06 00:08:46 +00008370 int device;
8371 if (!PyArg_ParseTuple(args, "i:major", &device))
8372 return NULL;
8373 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008374}
8375
8376PyDoc_STRVAR(posix_minor__doc__,
8377"minor(device) -> minor number\n\
8378Extracts a device minor number from a raw device number.");
8379
8380static PyObject *
8381posix_minor(PyObject *self, PyObject *args)
8382{
Victor Stinner8c62be82010-05-06 00:08:46 +00008383 int device;
8384 if (!PyArg_ParseTuple(args, "i:minor", &device))
8385 return NULL;
8386 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008387}
8388
8389PyDoc_STRVAR(posix_makedev__doc__,
8390"makedev(major, minor) -> device number\n\
8391Composes a raw device number from the major and minor device numbers.");
8392
8393static PyObject *
8394posix_makedev(PyObject *self, PyObject *args)
8395{
Victor Stinner8c62be82010-05-06 00:08:46 +00008396 int major, minor;
8397 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
8398 return NULL;
8399 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008400}
8401#endif /* device macros */
8402
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008403
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008404#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008405PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008406"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008407Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008408
Barry Warsaw53699e91996-12-10 23:23:01 +00008409static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008410posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008411{
Victor Stinner8c62be82010-05-06 00:08:46 +00008412 int fd;
8413 off_t length;
8414 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008415
Ross Lagerwall7807c352011-03-17 20:20:30 +02008416 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00008417 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008418
Victor Stinner8c62be82010-05-06 00:08:46 +00008419 Py_BEGIN_ALLOW_THREADS
8420 res = ftruncate(fd, length);
8421 Py_END_ALLOW_THREADS
8422 if (res < 0)
8423 return posix_error();
8424 Py_INCREF(Py_None);
8425 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008426}
8427#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00008428
Ross Lagerwall7807c352011-03-17 20:20:30 +02008429#ifdef HAVE_TRUNCATE
8430PyDoc_STRVAR(posix_truncate__doc__,
8431"truncate(path, length)\n\n\
Georg Brandl306336b2012-06-24 12:55:33 +02008432Truncate the file given by path to length bytes.\n\
8433On some platforms, path may also be specified as an open file descriptor.\n\
8434 If this functionality is unavailable, using it raises an exception.");
Ross Lagerwall7807c352011-03-17 20:20:30 +02008435
8436static PyObject *
Georg Brandl306336b2012-06-24 12:55:33 +02008437posix_truncate(PyObject *self, PyObject *args, PyObject *kwargs)
Ross Lagerwall7807c352011-03-17 20:20:30 +02008438{
Georg Brandl306336b2012-06-24 12:55:33 +02008439 path_t path;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008440 off_t length;
8441 int res;
Georg Brandl306336b2012-06-24 12:55:33 +02008442 PyObject *result = NULL;
8443 static char *keywords[] = {"path", "length", NULL};
Ross Lagerwall7807c352011-03-17 20:20:30 +02008444
Georg Brandl306336b2012-06-24 12:55:33 +02008445 memset(&path, 0, sizeof(path));
8446#ifdef HAVE_FTRUNCATE
8447 path.allow_fd = 1;
8448#endif
8449 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:truncate", keywords,
8450 path_converter, &path,
8451 _parse_off_t, &length))
Ross Lagerwall7807c352011-03-17 20:20:30 +02008452 return NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008453
8454 Py_BEGIN_ALLOW_THREADS
Georg Brandl306336b2012-06-24 12:55:33 +02008455#ifdef HAVE_FTRUNCATE
8456 if (path.fd != -1)
8457 res = ftruncate(path.fd, length);
8458 else
8459#endif
8460 res = truncate(path.narrow, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008461 Py_END_ALLOW_THREADS
Ross Lagerwall7807c352011-03-17 20:20:30 +02008462 if (res < 0)
Georg Brandl306336b2012-06-24 12:55:33 +02008463 result = path_posix_error("truncate", &path);
8464 else {
8465 Py_INCREF(Py_None);
8466 result = Py_None;
8467 }
8468 path_cleanup(&path);
8469 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008470}
8471#endif
8472
8473#ifdef HAVE_POSIX_FALLOCATE
8474PyDoc_STRVAR(posix_posix_fallocate__doc__,
8475"posix_fallocate(fd, offset, len)\n\n\
8476Ensures that enough disk space is allocated for the file specified by fd\n\
8477starting from offset and continuing for len bytes.");
8478
8479static PyObject *
8480posix_posix_fallocate(PyObject *self, PyObject *args)
8481{
8482 off_t len, offset;
8483 int res, fd;
8484
8485 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
8486 &fd, _parse_off_t, &offset, _parse_off_t, &len))
8487 return NULL;
8488
8489 Py_BEGIN_ALLOW_THREADS
8490 res = posix_fallocate(fd, offset, len);
8491 Py_END_ALLOW_THREADS
8492 if (res != 0) {
8493 errno = res;
8494 return posix_error();
8495 }
8496 Py_RETURN_NONE;
8497}
8498#endif
8499
8500#ifdef HAVE_POSIX_FADVISE
8501PyDoc_STRVAR(posix_posix_fadvise__doc__,
8502"posix_fadvise(fd, offset, len, advice)\n\n\
8503Announces an intention to access data in a specific pattern thus allowing\n\
8504the kernel to make optimizations.\n\
8505The advice applies to the region of the file specified by fd starting at\n\
8506offset and continuing for len bytes.\n\
8507advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
8508POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
8509POSIX_FADV_DONTNEED.");
8510
8511static PyObject *
8512posix_posix_fadvise(PyObject *self, PyObject *args)
8513{
8514 off_t len, offset;
8515 int res, fd, advice;
8516
8517 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
8518 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
8519 return NULL;
8520
8521 Py_BEGIN_ALLOW_THREADS
8522 res = posix_fadvise(fd, offset, len, advice);
8523 Py_END_ALLOW_THREADS
8524 if (res != 0) {
8525 errno = res;
8526 return posix_error();
8527 }
8528 Py_RETURN_NONE;
8529}
8530#endif
8531
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008532#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008533PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008534"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008535Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008536
Fred Drake762e2061999-08-26 17:23:54 +00008537/* Save putenv() parameters as values here, so we can collect them when they
8538 * get re-set with another call for the same key. */
8539static PyObject *posix_putenv_garbage;
8540
Tim Peters5aa91602002-01-30 05:46:57 +00008541static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008542posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008543{
Victor Stinner84ae1182010-05-06 22:05:07 +00008544 PyObject *newstr = NULL;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00008545#ifdef MS_WINDOWS
Victor Stinner65170952011-11-22 22:16:17 +01008546 PyObject *os1, *os2;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008547 wchar_t *newenv;
Victor Stinner65170952011-11-22 22:16:17 +01008548
Victor Stinner8c62be82010-05-06 00:08:46 +00008549 if (!PyArg_ParseTuple(args,
Victor Stinner9d3b93b2011-11-22 02:27:30 +01008550 "UU:putenv",
Victor Stinner65170952011-11-22 22:16:17 +01008551 &os1, &os2))
Victor Stinner8c62be82010-05-06 00:08:46 +00008552 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008553
Victor Stinner65170952011-11-22 22:16:17 +01008554 newstr = PyUnicode_FromFormat("%U=%U", os1, os2);
Victor Stinner84ae1182010-05-06 22:05:07 +00008555 if (newstr == NULL) {
8556 PyErr_NoMemory();
8557 goto error;
8558 }
Victor Stinner65170952011-11-22 22:16:17 +01008559 if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) {
8560 PyErr_Format(PyExc_ValueError,
8561 "the environment variable is longer than %u characters",
8562 _MAX_ENV);
8563 goto error;
8564 }
8565
Victor Stinner8c62be82010-05-06 00:08:46 +00008566 newenv = PyUnicode_AsUnicode(newstr);
Victor Stinnereb5657a2011-09-30 01:44:27 +02008567 if (newenv == NULL)
8568 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00008569 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008570 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00008571 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00008572 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00008573#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008574 PyObject *os1, *os2;
8575 char *s1, *s2;
8576 char *newenv;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008577
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008578 if (!PyArg_ParseTuple(args,
8579 "O&O&:putenv",
8580 PyUnicode_FSConverter, &os1,
8581 PyUnicode_FSConverter, &os2))
8582 return NULL;
8583 s1 = PyBytes_AsString(os1);
8584 s2 = PyBytes_AsString(os2);
Guido van Rossumad0ee831995-03-01 10:34:45 +00008585
Victor Stinner65170952011-11-22 22:16:17 +01008586 newstr = PyBytes_FromFormat("%s=%s", s1, s2);
Victor Stinner9d3b93b2011-11-22 02:27:30 +01008587 if (newstr == NULL) {
8588 PyErr_NoMemory();
8589 goto error;
8590 }
8591
Victor Stinner8c62be82010-05-06 00:08:46 +00008592 newenv = PyBytes_AS_STRING(newstr);
Victor Stinner8c62be82010-05-06 00:08:46 +00008593 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008594 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00008595 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00008596 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00008597#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00008598
Victor Stinner8c62be82010-05-06 00:08:46 +00008599 /* Install the first arg and newstr in posix_putenv_garbage;
8600 * this will cause previous value to be collected. This has to
8601 * happen after the real putenv() call because the old value
8602 * was still accessible until then. */
Victor Stinner65170952011-11-22 22:16:17 +01008603 if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008604 /* really not much we can do; just leak */
8605 PyErr_Clear();
8606 }
8607 else {
8608 Py_DECREF(newstr);
8609 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00008610
Martin v. Löwis011e8422009-05-05 04:43:17 +00008611#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008612 Py_DECREF(os1);
8613 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00008614#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00008615 Py_RETURN_NONE;
8616
8617error:
8618#ifndef MS_WINDOWS
8619 Py_DECREF(os1);
8620 Py_DECREF(os2);
8621#endif
8622 Py_XDECREF(newstr);
8623 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00008624}
Guido van Rossumb6a47161997-09-15 22:54:34 +00008625#endif /* putenv */
8626
Guido van Rossumc524d952001-10-19 01:31:59 +00008627#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008628PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008629"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008630Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00008631
8632static PyObject *
8633posix_unsetenv(PyObject *self, PyObject *args)
8634{
Victor Stinner65170952011-11-22 22:16:17 +01008635 PyObject *name;
Victor Stinner984890f2011-11-24 13:53:38 +01008636#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +01008637 int err;
Victor Stinner984890f2011-11-24 13:53:38 +01008638#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00008639
8640 if (!PyArg_ParseTuple(args, "O&:unsetenv",
Guido van Rossumc524d952001-10-19 01:31:59 +00008641
Victor Stinner65170952011-11-22 22:16:17 +01008642 PyUnicode_FSConverter, &name))
Guido van Rossumc524d952001-10-19 01:31:59 +00008643 return NULL;
Guido van Rossumc524d952001-10-19 01:31:59 +00008644
Victor Stinner984890f2011-11-24 13:53:38 +01008645#ifdef HAVE_BROKEN_UNSETENV
8646 unsetenv(PyBytes_AS_STRING(name));
8647#else
Victor Stinner65170952011-11-22 22:16:17 +01008648 err = unsetenv(PyBytes_AS_STRING(name));
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06008649 if (err) {
Benjamin Petersone8eb0e82011-11-22 23:14:47 -06008650 Py_DECREF(name);
Victor Stinner60b385e2011-11-22 22:01:28 +01008651 return posix_error();
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06008652 }
Victor Stinner984890f2011-11-24 13:53:38 +01008653#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008654
Victor Stinner8c62be82010-05-06 00:08:46 +00008655 /* Remove the key from posix_putenv_garbage;
8656 * this will cause it to be collected. This has to
8657 * happen after the real unsetenv() call because the
8658 * old value was still accessible until then.
8659 */
Victor Stinner65170952011-11-22 22:16:17 +01008660 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008661 /* really not much we can do; just leak */
8662 PyErr_Clear();
8663 }
Victor Stinner65170952011-11-22 22:16:17 +01008664 Py_DECREF(name);
Victor Stinner84ae1182010-05-06 22:05:07 +00008665 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00008666}
8667#endif /* unsetenv */
8668
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008669PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008670"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008671Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00008672
Guido van Rossumf68d8e52001-04-14 17:55:09 +00008673static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008674posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00008675{
Victor Stinner8c62be82010-05-06 00:08:46 +00008676 int code;
8677 char *message;
8678 if (!PyArg_ParseTuple(args, "i:strerror", &code))
8679 return NULL;
8680 message = strerror(code);
8681 if (message == NULL) {
8682 PyErr_SetString(PyExc_ValueError,
8683 "strerror() argument out of range");
8684 return NULL;
8685 }
Victor Stinner1b579672011-12-17 05:47:23 +01008686 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +00008687}
Guido van Rossumb6a47161997-09-15 22:54:34 +00008688
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008689
Guido van Rossumc9641791998-08-04 15:26:23 +00008690#ifdef HAVE_SYS_WAIT_H
8691
Fred Drake106c1a02002-04-23 15:58:02 +00008692#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008693PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008694"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008695Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00008696
8697static PyObject *
8698posix_WCOREDUMP(PyObject *self, PyObject *args)
8699{
Victor Stinner8c62be82010-05-06 00:08:46 +00008700 WAIT_TYPE status;
8701 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00008702
Victor Stinner8c62be82010-05-06 00:08:46 +00008703 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
8704 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00008705
Victor Stinner8c62be82010-05-06 00:08:46 +00008706 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00008707}
8708#endif /* WCOREDUMP */
8709
8710#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008711PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008712"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00008713Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008714job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00008715
8716static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008717posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00008718{
Victor Stinner8c62be82010-05-06 00:08:46 +00008719 WAIT_TYPE status;
8720 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00008721
Victor Stinner8c62be82010-05-06 00:08:46 +00008722 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
8723 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00008724
Victor Stinner8c62be82010-05-06 00:08:46 +00008725 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00008726}
8727#endif /* WIFCONTINUED */
8728
Guido van Rossumc9641791998-08-04 15:26:23 +00008729#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008730PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008731"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008732Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008733
8734static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008735posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008736{
Victor Stinner8c62be82010-05-06 00:08:46 +00008737 WAIT_TYPE status;
8738 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008739
Victor Stinner8c62be82010-05-06 00:08:46 +00008740 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
8741 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008742
Victor Stinner8c62be82010-05-06 00:08:46 +00008743 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008744}
8745#endif /* WIFSTOPPED */
8746
8747#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008748PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008749"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008750Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008751
8752static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008753posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008754{
Victor Stinner8c62be82010-05-06 00:08:46 +00008755 WAIT_TYPE status;
8756 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008757
Victor Stinner8c62be82010-05-06 00:08:46 +00008758 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
8759 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008760
Victor Stinner8c62be82010-05-06 00:08:46 +00008761 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008762}
8763#endif /* WIFSIGNALED */
8764
8765#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008766PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008767"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00008768Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008769system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008770
8771static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008772posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008773{
Victor Stinner8c62be82010-05-06 00:08:46 +00008774 WAIT_TYPE status;
8775 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008776
Victor Stinner8c62be82010-05-06 00:08:46 +00008777 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
8778 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008779
Victor Stinner8c62be82010-05-06 00:08:46 +00008780 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008781}
8782#endif /* WIFEXITED */
8783
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00008784#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008785PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008786"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008787Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008788
8789static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008790posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008791{
Victor Stinner8c62be82010-05-06 00:08:46 +00008792 WAIT_TYPE status;
8793 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008794
Victor Stinner8c62be82010-05-06 00:08:46 +00008795 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
8796 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008797
Victor Stinner8c62be82010-05-06 00:08:46 +00008798 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008799}
8800#endif /* WEXITSTATUS */
8801
8802#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008803PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008804"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00008805Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008806value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008807
8808static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008809posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008810{
Victor Stinner8c62be82010-05-06 00:08:46 +00008811 WAIT_TYPE status;
8812 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008813
Victor Stinner8c62be82010-05-06 00:08:46 +00008814 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
8815 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008816
Victor Stinner8c62be82010-05-06 00:08:46 +00008817 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008818}
8819#endif /* WTERMSIG */
8820
8821#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008822PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008823"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008824Return the signal that stopped the process that provided\n\
8825the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008826
8827static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008828posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008829{
Victor Stinner8c62be82010-05-06 00:08:46 +00008830 WAIT_TYPE status;
8831 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008832
Victor Stinner8c62be82010-05-06 00:08:46 +00008833 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
8834 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008835
Victor Stinner8c62be82010-05-06 00:08:46 +00008836 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008837}
8838#endif /* WSTOPSIG */
8839
8840#endif /* HAVE_SYS_WAIT_H */
8841
8842
Thomas Wouters477c8d52006-05-27 19:21:47 +00008843#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00008844#ifdef _SCO_DS
8845/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
8846 needed definitions in sys/statvfs.h */
8847#define _SVID3
8848#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008849#include <sys/statvfs.h>
8850
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008851static PyObject*
8852_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008853 PyObject *v = PyStructSequence_New(&StatVFSResultType);
8854 if (v == NULL)
8855 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008856
8857#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00008858 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8859 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8860 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
8861 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
8862 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
8863 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
8864 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
8865 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
8866 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8867 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008868#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008869 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8870 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8871 PyStructSequence_SET_ITEM(v, 2,
8872 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
8873 PyStructSequence_SET_ITEM(v, 3,
8874 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
8875 PyStructSequence_SET_ITEM(v, 4,
8876 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
8877 PyStructSequence_SET_ITEM(v, 5,
8878 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
8879 PyStructSequence_SET_ITEM(v, 6,
8880 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
8881 PyStructSequence_SET_ITEM(v, 7,
8882 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
8883 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8884 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008885#endif
8886
Victor Stinner8c62be82010-05-06 00:08:46 +00008887 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008888}
8889
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008890PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008891"fstatvfs(fd) -> statvfs result\n\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07008892Perform an fstatvfs system call on the given fd.\n\
8893Equivalent to statvfs(fd).");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008894
8895static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008896posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008897{
Victor Stinner8c62be82010-05-06 00:08:46 +00008898 int fd, res;
8899 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008900
Victor Stinner8c62be82010-05-06 00:08:46 +00008901 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
8902 return NULL;
8903 Py_BEGIN_ALLOW_THREADS
8904 res = fstatvfs(fd, &st);
8905 Py_END_ALLOW_THREADS
8906 if (res != 0)
8907 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008908
Victor Stinner8c62be82010-05-06 00:08:46 +00008909 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008910}
Thomas Wouters477c8d52006-05-27 19:21:47 +00008911#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008912
8913
Thomas Wouters477c8d52006-05-27 19:21:47 +00008914#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008915#include <sys/statvfs.h>
8916
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008917PyDoc_STRVAR(posix_statvfs__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07008918"statvfs(path)\n\n\
8919Perform a statvfs system call on the given path.\n\
8920\n\
8921path may always be specified as a string.\n\
8922On some platforms, path may also be specified as an open file descriptor.\n\
8923 If this functionality is unavailable, using it raises an exception.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008924
8925static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07008926posix_statvfs(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008927{
Larry Hastings9cf065c2012-06-22 16:30:09 -07008928 static char *keywords[] = {"path", NULL};
8929 path_t path;
8930 int result;
8931 PyObject *return_value = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008932 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008933
Larry Hastings9cf065c2012-06-22 16:30:09 -07008934 memset(&path, 0, sizeof(path));
8935#ifdef HAVE_FSTATVFS
8936 path.allow_fd = 1;
8937#endif
8938 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:statvfs", keywords,
8939 path_converter, &path
8940 ))
8941 return NULL;
8942
8943 Py_BEGIN_ALLOW_THREADS
8944#ifdef HAVE_FSTATVFS
8945 if (path.fd != -1) {
8946#ifdef __APPLE__
8947 /* handle weak-linking on Mac OS X 10.3 */
8948 if (fstatvfs == NULL) {
8949 fd_specified("statvfs", path.fd);
8950 goto exit;
8951 }
8952#endif
8953 result = fstatvfs(path.fd, &st);
8954 }
8955 else
8956#endif
8957 result = statvfs(path.narrow, &st);
8958 Py_END_ALLOW_THREADS
8959
8960 if (result) {
8961 return_value = path_posix_error("statvfs", &path);
8962 goto exit;
8963 }
8964
8965 return_value = _pystatvfs_fromstructstatvfs(st);
8966
8967exit:
8968 path_cleanup(&path);
8969 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00008970}
8971#endif /* HAVE_STATVFS */
8972
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008973#ifdef MS_WINDOWS
8974PyDoc_STRVAR(win32__getdiskusage__doc__,
8975"_getdiskusage(path) -> (total, free)\n\n\
8976Return disk usage statistics about the given path as (total, free) tuple.");
8977
8978static PyObject *
8979win32__getdiskusage(PyObject *self, PyObject *args)
8980{
8981 BOOL retval;
8982 ULARGE_INTEGER _, total, free;
Victor Stinner6139c1b2011-11-09 22:14:14 +01008983 const wchar_t *path;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008984
Victor Stinner6139c1b2011-11-09 22:14:14 +01008985 if (! PyArg_ParseTuple(args, "u", &path))
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008986 return NULL;
8987
8988 Py_BEGIN_ALLOW_THREADS
Victor Stinner6139c1b2011-11-09 22:14:14 +01008989 retval = GetDiskFreeSpaceExW(path, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008990 Py_END_ALLOW_THREADS
8991 if (retval == 0)
8992 return PyErr_SetFromWindowsErr(0);
8993
8994 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
8995}
8996#endif
8997
8998
Fred Drakec9680921999-12-13 16:37:25 +00008999/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
9000 * It maps strings representing configuration variable names to
9001 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00009002 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00009003 * rarely-used constants. There are three separate tables that use
9004 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00009005 *
9006 * This code is always included, even if none of the interfaces that
9007 * need it are included. The #if hackery needed to avoid it would be
9008 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00009009 */
9010struct constdef {
9011 char *name;
9012 long value;
9013};
9014
Fred Drake12c6e2d1999-12-14 21:25:03 +00009015static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009016conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00009017 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00009018{
Christian Heimes217cfd12007-12-02 14:31:20 +00009019 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00009020 *valuep = PyLong_AS_LONG(arg);
9021 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00009022 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00009023 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00009024 /* look up the value in the table using a binary search */
9025 size_t lo = 0;
9026 size_t mid;
9027 size_t hi = tablesize;
9028 int cmp;
9029 const char *confname;
9030 if (!PyUnicode_Check(arg)) {
9031 PyErr_SetString(PyExc_TypeError,
9032 "configuration names must be strings or integers");
9033 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00009034 }
Stefan Krah0e803b32010-11-26 16:16:47 +00009035 confname = _PyUnicode_AsString(arg);
9036 if (confname == NULL)
9037 return 0;
9038 while (lo < hi) {
9039 mid = (lo + hi) / 2;
9040 cmp = strcmp(confname, table[mid].name);
9041 if (cmp < 0)
9042 hi = mid;
9043 else if (cmp > 0)
9044 lo = mid + 1;
9045 else {
9046 *valuep = table[mid].value;
9047 return 1;
9048 }
9049 }
9050 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
9051 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00009052 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00009053}
9054
9055
9056#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
9057static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00009058#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009059 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00009060#endif
9061#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009062 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00009063#endif
Fred Drakec9680921999-12-13 16:37:25 +00009064#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009065 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009066#endif
9067#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00009068 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00009069#endif
9070#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00009071 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00009072#endif
9073#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00009074 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00009075#endif
9076#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009077 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009078#endif
9079#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00009080 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00009081#endif
9082#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00009083 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00009084#endif
9085#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009086 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009087#endif
9088#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009089 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00009090#endif
9091#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009092 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009093#endif
9094#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00009095 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00009096#endif
9097#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009098 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009099#endif
9100#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00009101 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00009102#endif
9103#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009104 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009105#endif
9106#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00009107 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00009108#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00009109#ifdef _PC_ACL_ENABLED
9110 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
9111#endif
9112#ifdef _PC_MIN_HOLE_SIZE
9113 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
9114#endif
9115#ifdef _PC_ALLOC_SIZE_MIN
9116 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
9117#endif
9118#ifdef _PC_REC_INCR_XFER_SIZE
9119 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
9120#endif
9121#ifdef _PC_REC_MAX_XFER_SIZE
9122 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
9123#endif
9124#ifdef _PC_REC_MIN_XFER_SIZE
9125 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
9126#endif
9127#ifdef _PC_REC_XFER_ALIGN
9128 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
9129#endif
9130#ifdef _PC_SYMLINK_MAX
9131 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
9132#endif
9133#ifdef _PC_XATTR_ENABLED
9134 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
9135#endif
9136#ifdef _PC_XATTR_EXISTS
9137 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
9138#endif
9139#ifdef _PC_TIMESTAMP_RESOLUTION
9140 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
9141#endif
Fred Drakec9680921999-12-13 16:37:25 +00009142};
9143
Fred Drakec9680921999-12-13 16:37:25 +00009144static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009145conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009146{
9147 return conv_confname(arg, valuep, posix_constants_pathconf,
9148 sizeof(posix_constants_pathconf)
9149 / sizeof(struct constdef));
9150}
9151#endif
9152
9153#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009154PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009155"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00009156Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009157If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00009158
9159static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009160posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00009161{
9162 PyObject *result = NULL;
9163 int name, fd;
9164
Fred Drake12c6e2d1999-12-14 21:25:03 +00009165 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
9166 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00009167 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00009168
Stefan Krah0e803b32010-11-26 16:16:47 +00009169 errno = 0;
9170 limit = fpathconf(fd, name);
9171 if (limit == -1 && errno != 0)
9172 posix_error();
9173 else
9174 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00009175 }
9176 return result;
9177}
9178#endif
9179
9180
9181#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009182PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009183"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00009184Return the configuration limit name for the file or directory path.\n\
Georg Brandl306336b2012-06-24 12:55:33 +02009185If there is no limit, return -1.\n\
9186On some platforms, path may also be specified as an open file descriptor.\n\
9187 If this functionality is unavailable, using it raises an exception.");
Fred Drakec9680921999-12-13 16:37:25 +00009188
9189static PyObject *
Georg Brandl306336b2012-06-24 12:55:33 +02009190posix_pathconf(PyObject *self, PyObject *args, PyObject *kwargs)
Fred Drakec9680921999-12-13 16:37:25 +00009191{
Georg Brandl306336b2012-06-24 12:55:33 +02009192 path_t path;
Fred Drakec9680921999-12-13 16:37:25 +00009193 PyObject *result = NULL;
9194 int name;
Georg Brandl306336b2012-06-24 12:55:33 +02009195 static char *keywords[] = {"path", "name", NULL};
Fred Drakec9680921999-12-13 16:37:25 +00009196
Georg Brandl306336b2012-06-24 12:55:33 +02009197 memset(&path, 0, sizeof(path));
9198#ifdef HAVE_FPATHCONF
9199 path.allow_fd = 1;
9200#endif
9201 if (PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:pathconf", keywords,
9202 path_converter, &path,
9203 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009204 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00009205
Victor Stinner8c62be82010-05-06 00:08:46 +00009206 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +02009207#ifdef HAVE_FPATHCONF
9208 if (path.fd != -1)
9209 limit = fpathconf(path.fd, name);
9210 else
9211#endif
9212 limit = pathconf(path.narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +00009213 if (limit == -1 && errno != 0) {
9214 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00009215 /* could be a path or name problem */
9216 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00009217 else
Georg Brandl306336b2012-06-24 12:55:33 +02009218 result = path_posix_error("pathconf", &path);
Victor Stinner8c62be82010-05-06 00:08:46 +00009219 }
9220 else
9221 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00009222 }
Georg Brandl306336b2012-06-24 12:55:33 +02009223 path_cleanup(&path);
Fred Drakec9680921999-12-13 16:37:25 +00009224 return result;
9225}
9226#endif
9227
9228#ifdef HAVE_CONFSTR
9229static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00009230#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00009231 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00009232#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00009233#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009234 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00009235#endif
9236#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009237 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00009238#endif
Fred Draked86ed291999-12-15 15:34:33 +00009239#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009240 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00009241#endif
9242#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00009243 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00009244#endif
9245#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00009246 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00009247#endif
9248#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009249 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00009250#endif
Fred Drakec9680921999-12-13 16:37:25 +00009251#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009252 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009253#endif
9254#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009255 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009256#endif
9257#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009258 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009259#endif
9260#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009261 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009262#endif
9263#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009264 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009265#endif
9266#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009267 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009268#endif
9269#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009270 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009271#endif
9272#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009273 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009274#endif
Fred Draked86ed291999-12-15 15:34:33 +00009275#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00009276 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00009277#endif
Fred Drakec9680921999-12-13 16:37:25 +00009278#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00009279 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00009280#endif
Fred Draked86ed291999-12-15 15:34:33 +00009281#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00009282 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00009283#endif
9284#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009285 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00009286#endif
9287#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009288 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00009289#endif
9290#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009291 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00009292#endif
Fred Drakec9680921999-12-13 16:37:25 +00009293#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009294 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009295#endif
9296#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009297 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009298#endif
9299#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009300 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009301#endif
9302#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009303 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009304#endif
9305#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009306 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009307#endif
9308#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009309 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009310#endif
9311#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009312 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009313#endif
9314#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009315 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009316#endif
9317#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009318 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009319#endif
9320#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009321 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009322#endif
9323#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009324 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009325#endif
9326#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009327 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009328#endif
9329#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009330 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009331#endif
9332#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009333 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009334#endif
9335#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009336 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009337#endif
9338#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009339 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009340#endif
Fred Draked86ed291999-12-15 15:34:33 +00009341#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00009342 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00009343#endif
9344#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00009345 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00009346#endif
9347#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00009348 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00009349#endif
9350#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009351 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00009352#endif
9353#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00009354 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00009355#endif
9356#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00009357 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00009358#endif
9359#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009360 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00009361#endif
9362#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00009363 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00009364#endif
9365#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009366 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00009367#endif
9368#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00009369 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00009370#endif
9371#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00009372 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00009373#endif
9374#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00009375 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00009376#endif
9377#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00009378 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00009379#endif
Fred Drakec9680921999-12-13 16:37:25 +00009380};
9381
9382static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009383conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009384{
9385 return conv_confname(arg, valuep, posix_constants_confstr,
9386 sizeof(posix_constants_confstr)
9387 / sizeof(struct constdef));
9388}
9389
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009390PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009391"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009392Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00009393
9394static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009395posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00009396{
9397 PyObject *result = NULL;
9398 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00009399 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00009400 int len;
Fred Drakec9680921999-12-13 16:37:25 +00009401
Victor Stinnercb043522010-09-10 23:49:04 +00009402 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
9403 return NULL;
9404
9405 errno = 0;
9406 len = confstr(name, buffer, sizeof(buffer));
9407 if (len == 0) {
9408 if (errno) {
9409 posix_error();
9410 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00009411 }
9412 else {
Victor Stinnercb043522010-09-10 23:49:04 +00009413 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00009414 }
9415 }
Victor Stinnercb043522010-09-10 23:49:04 +00009416
9417 if ((unsigned int)len >= sizeof(buffer)) {
9418 char *buf = PyMem_Malloc(len);
9419 if (buf == NULL)
9420 return PyErr_NoMemory();
9421 confstr(name, buf, len);
9422 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
9423 PyMem_Free(buf);
9424 }
9425 else
9426 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00009427 return result;
9428}
9429#endif
9430
9431
9432#ifdef HAVE_SYSCONF
9433static struct constdef posix_constants_sysconf[] = {
9434#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00009435 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00009436#endif
9437#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00009438 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00009439#endif
9440#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009441 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009442#endif
9443#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009444 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009445#endif
9446#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009447 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009448#endif
9449#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00009450 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00009451#endif
9452#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00009453 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00009454#endif
9455#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009456 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009457#endif
9458#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00009459 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00009460#endif
9461#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009462 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009463#endif
Fred Draked86ed291999-12-15 15:34:33 +00009464#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009465 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00009466#endif
9467#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00009468 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00009469#endif
Fred Drakec9680921999-12-13 16:37:25 +00009470#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009471 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009472#endif
Fred Drakec9680921999-12-13 16:37:25 +00009473#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009474 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009475#endif
9476#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009477 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009478#endif
9479#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009480 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009481#endif
9482#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009483 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009484#endif
9485#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009486 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009487#endif
Fred Draked86ed291999-12-15 15:34:33 +00009488#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009489 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00009490#endif
Fred Drakec9680921999-12-13 16:37:25 +00009491#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00009492 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00009493#endif
9494#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009495 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009496#endif
9497#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009498 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009499#endif
9500#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009501 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009502#endif
9503#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009504 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009505#endif
Fred Draked86ed291999-12-15 15:34:33 +00009506#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00009507 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00009508#endif
Fred Drakec9680921999-12-13 16:37:25 +00009509#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009510 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009511#endif
9512#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009513 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009514#endif
9515#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009516 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009517#endif
9518#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009519 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009520#endif
9521#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009522 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009523#endif
9524#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009525 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00009526#endif
9527#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009528 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009529#endif
9530#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009531 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009532#endif
9533#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00009534 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00009535#endif
9536#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009537 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009538#endif
9539#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009540 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00009541#endif
9542#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009543 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00009544#endif
9545#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009546 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009547#endif
9548#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009549 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009550#endif
9551#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009552 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009553#endif
9554#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009555 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009556#endif
9557#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009558 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00009559#endif
9560#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009561 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009562#endif
9563#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009564 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009565#endif
9566#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00009567 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00009568#endif
9569#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009570 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009571#endif
9572#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009573 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00009574#endif
9575#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009576 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00009577#endif
Fred Draked86ed291999-12-15 15:34:33 +00009578#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00009579 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00009580#endif
Fred Drakec9680921999-12-13 16:37:25 +00009581#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009582 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009583#endif
9584#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009585 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009586#endif
9587#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009588 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009589#endif
Fred Draked86ed291999-12-15 15:34:33 +00009590#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009591 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00009592#endif
Fred Drakec9680921999-12-13 16:37:25 +00009593#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00009594 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00009595#endif
Fred Draked86ed291999-12-15 15:34:33 +00009596#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00009597 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00009598#endif
9599#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00009600 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00009601#endif
Fred Drakec9680921999-12-13 16:37:25 +00009602#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009603 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009604#endif
9605#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009606 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009607#endif
9608#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009609 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009610#endif
9611#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009612 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009613#endif
Fred Draked86ed291999-12-15 15:34:33 +00009614#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00009615 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00009616#endif
Fred Drakec9680921999-12-13 16:37:25 +00009617#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00009618 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00009619#endif
9620#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00009621 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00009622#endif
9623#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009624 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009625#endif
9626#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009627 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00009628#endif
9629#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00009630 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00009631#endif
9632#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00009633 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00009634#endif
9635#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00009636 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00009637#endif
Fred Draked86ed291999-12-15 15:34:33 +00009638#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00009639 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00009640#endif
Fred Drakec9680921999-12-13 16:37:25 +00009641#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009642 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009643#endif
9644#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009645 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009646#endif
Fred Draked86ed291999-12-15 15:34:33 +00009647#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009648 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00009649#endif
Fred Drakec9680921999-12-13 16:37:25 +00009650#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009651 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009652#endif
9653#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009654 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009655#endif
9656#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009657 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009658#endif
9659#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009660 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009661#endif
9662#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009663 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009664#endif
9665#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009666 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009667#endif
9668#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009669 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009670#endif
9671#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009672 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00009673#endif
9674#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00009675 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00009676#endif
Fred Draked86ed291999-12-15 15:34:33 +00009677#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009678 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00009679#endif
9680#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00009681 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00009682#endif
Fred Drakec9680921999-12-13 16:37:25 +00009683#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00009684 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00009685#endif
9686#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009687 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009688#endif
9689#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00009690 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00009691#endif
9692#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00009693 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00009694#endif
9695#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009696 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009697#endif
9698#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00009699 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00009700#endif
9701#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00009702 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00009703#endif
9704#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00009705 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00009706#endif
9707#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00009708 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00009709#endif
9710#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00009711 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00009712#endif
9713#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00009714 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00009715#endif
9716#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009717 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00009718#endif
9719#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009720 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00009721#endif
9722#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00009723 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00009724#endif
9725#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00009726 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00009727#endif
9728#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00009729 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00009730#endif
9731#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00009732 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00009733#endif
9734#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009735 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009736#endif
9737#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00009738 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00009739#endif
9740#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00009741 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00009742#endif
9743#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009744 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009745#endif
9746#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009747 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009748#endif
9749#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00009750 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00009751#endif
9752#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009753 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009754#endif
9755#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009756 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009757#endif
9758#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00009759 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00009760#endif
9761#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00009762 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00009763#endif
9764#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009765 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009766#endif
9767#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009768 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009769#endif
9770#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009771 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00009772#endif
9773#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009774 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009775#endif
9776#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009777 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009778#endif
9779#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009780 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009781#endif
9782#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009783 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009784#endif
9785#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009786 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009787#endif
Fred Draked86ed291999-12-15 15:34:33 +00009788#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00009789 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00009790#endif
Fred Drakec9680921999-12-13 16:37:25 +00009791#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00009792 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00009793#endif
9794#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009795 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009796#endif
9797#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00009798 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00009799#endif
9800#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009801 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009802#endif
9803#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009804 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009805#endif
9806#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00009807 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00009808#endif
9809#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00009810 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00009811#endif
9812#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00009813 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00009814#endif
9815#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00009816 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00009817#endif
9818#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009819 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009820#endif
9821#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00009822 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00009823#endif
9824#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009825 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00009826#endif
9827#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00009828 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00009829#endif
9830#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00009831 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00009832#endif
9833#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00009834 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00009835#endif
9836#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009837 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009838#endif
9839#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009840 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009841#endif
9842#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00009843 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00009844#endif
9845#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009846 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009847#endif
9848#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009849 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009850#endif
9851#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009852 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009853#endif
9854#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009855 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009856#endif
9857#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009858 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009859#endif
9860#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009861 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009862#endif
9863#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00009864 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00009865#endif
9866#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009867 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009868#endif
9869#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009870 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009871#endif
9872#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009873 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009874#endif
9875#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009876 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009877#endif
9878#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00009879 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00009880#endif
9881#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009882 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00009883#endif
9884#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00009885 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00009886#endif
9887#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009888 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00009889#endif
9890#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00009891 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00009892#endif
9893#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00009894 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00009895#endif
9896#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00009897 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00009898#endif
9899#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00009900 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00009901#endif
9902#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00009903 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00009904#endif
9905#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00009906 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00009907#endif
9908#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00009909 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00009910#endif
9911#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009912 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009913#endif
9914#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009915 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009916#endif
9917#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00009918 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00009919#endif
9920#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00009921 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00009922#endif
9923#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00009924 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00009925#endif
9926};
9927
9928static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009929conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009930{
9931 return conv_confname(arg, valuep, posix_constants_sysconf,
9932 sizeof(posix_constants_sysconf)
9933 / sizeof(struct constdef));
9934}
9935
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009936PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009937"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009938Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00009939
9940static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009941posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00009942{
9943 PyObject *result = NULL;
9944 int name;
9945
9946 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
9947 int value;
9948
9949 errno = 0;
9950 value = sysconf(name);
9951 if (value == -1 && errno != 0)
9952 posix_error();
9953 else
Christian Heimes217cfd12007-12-02 14:31:20 +00009954 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00009955 }
9956 return result;
9957}
9958#endif
9959
9960
Fred Drakebec628d1999-12-15 18:31:10 +00009961/* This code is used to ensure that the tables of configuration value names
9962 * are in sorted order as required by conv_confname(), and also to build the
9963 * the exported dictionaries that are used to publish information about the
9964 * names available on the host platform.
9965 *
9966 * Sorting the table at runtime ensures that the table is properly ordered
9967 * when used, even for platforms we're not able to test on. It also makes
9968 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00009969 */
Fred Drakebec628d1999-12-15 18:31:10 +00009970
9971static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009972cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00009973{
9974 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009975 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00009976 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009977 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00009978
9979 return strcmp(c1->name, c2->name);
9980}
9981
9982static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009983setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00009984 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009985{
Fred Drakebec628d1999-12-15 18:31:10 +00009986 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00009987 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00009988
9989 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
9990 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00009991 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00009992 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009993
Barry Warsaw3155db32000-04-13 15:20:40 +00009994 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009995 PyObject *o = PyLong_FromLong(table[i].value);
9996 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
9997 Py_XDECREF(o);
9998 Py_DECREF(d);
9999 return -1;
10000 }
10001 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000010002 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000010003 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000010004}
10005
Fred Drakebec628d1999-12-15 18:31:10 +000010006/* Return -1 on failure, 0 on success. */
10007static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000010008setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000010009{
10010#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000010011 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000010012 sizeof(posix_constants_pathconf)
10013 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000010014 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000010015 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010016#endif
10017#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000010018 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000010019 sizeof(posix_constants_confstr)
10020 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000010021 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000010022 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010023#endif
10024#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000010025 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000010026 sizeof(posix_constants_sysconf)
10027 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000010028 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000010029 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010030#endif
Fred Drakebec628d1999-12-15 18:31:10 +000010031 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000010032}
Fred Draked86ed291999-12-15 15:34:33 +000010033
10034
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000010035PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +000010036"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010037Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000010038in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010039
10040static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +000010041posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010042{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010043 abort();
10044 /*NOTREACHED*/
10045 Py_FatalError("abort() called from Python code didn't abort!");
10046 return NULL;
10047}
Fred Drakebec628d1999-12-15 18:31:10 +000010048
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010049#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000010050PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +000010051"startfile(filepath [, operation]) - Start a file with its associated\n\
10052application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +000010053\n\
Georg Brandlf4f44152006-02-18 22:29:33 +000010054When \"operation\" is not specified or \"open\", this acts like\n\
10055double-clicking the file in Explorer, or giving the file name as an\n\
10056argument to the DOS \"start\" command: the file is opened with whatever\n\
10057application (if any) its extension is associated.\n\
10058When another \"operation\" is given, it specifies what should be done with\n\
10059the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +000010060\n\
10061startfile returns as soon as the associated application is launched.\n\
10062There is no option to wait for the application to close, and no way\n\
10063to retrieve the application's exit status.\n\
10064\n\
10065The filepath is relative to the current directory. If you want to use\n\
10066an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000010067the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +000010068
10069static PyObject *
10070win32_startfile(PyObject *self, PyObject *args)
10071{
Victor Stinner8c62be82010-05-06 00:08:46 +000010072 PyObject *ofilepath;
10073 char *filepath;
10074 char *operation = NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +020010075 wchar_t *wpath, *woperation;
Victor Stinner8c62be82010-05-06 00:08:46 +000010076 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000010077
Victor Stinnereb5657a2011-09-30 01:44:27 +020010078 PyObject *unipath, *uoperation = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000010079 if (!PyArg_ParseTuple(args, "U|s:startfile",
10080 &unipath, &operation)) {
10081 PyErr_Clear();
10082 goto normal;
10083 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +000010084
Victor Stinner8c62be82010-05-06 00:08:46 +000010085 if (operation) {
Victor Stinnereb5657a2011-09-30 01:44:27 +020010086 uoperation = PyUnicode_DecodeASCII(operation,
Victor Stinner8c62be82010-05-06 00:08:46 +000010087 strlen(operation), NULL);
Victor Stinnereb5657a2011-09-30 01:44:27 +020010088 if (!uoperation) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010089 PyErr_Clear();
10090 operation = NULL;
10091 goto normal;
10092 }
10093 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +000010094
Victor Stinnereb5657a2011-09-30 01:44:27 +020010095 wpath = PyUnicode_AsUnicode(unipath);
10096 if (wpath == NULL)
10097 goto normal;
10098 if (uoperation) {
10099 woperation = PyUnicode_AsUnicode(uoperation);
10100 if (woperation == NULL)
10101 goto normal;
10102 }
10103 else
10104 woperation = NULL;
10105
Victor Stinner8c62be82010-05-06 00:08:46 +000010106 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +020010107 rc = ShellExecuteW((HWND)0, woperation, wpath,
10108 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000010109 Py_END_ALLOW_THREADS
10110
Victor Stinnereb5657a2011-09-30 01:44:27 +020010111 Py_XDECREF(uoperation);
Victor Stinner8c62be82010-05-06 00:08:46 +000010112 if (rc <= (HINSTANCE)32) {
Victor Stinnereb5657a2011-09-30 01:44:27 +020010113 win32_error_object("startfile", unipath);
10114 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000010115 }
10116 Py_INCREF(Py_None);
10117 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010118
10119normal:
Victor Stinner8c62be82010-05-06 00:08:46 +000010120 if (!PyArg_ParseTuple(args, "O&|s:startfile",
10121 PyUnicode_FSConverter, &ofilepath,
10122 &operation))
10123 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +010010124 if (win32_warn_bytes_api()) {
10125 Py_DECREF(ofilepath);
10126 return NULL;
10127 }
Victor Stinner8c62be82010-05-06 00:08:46 +000010128 filepath = PyBytes_AsString(ofilepath);
10129 Py_BEGIN_ALLOW_THREADS
10130 rc = ShellExecute((HWND)0, operation, filepath,
10131 NULL, NULL, SW_SHOWNORMAL);
10132 Py_END_ALLOW_THREADS
10133 if (rc <= (HINSTANCE)32) {
10134 PyObject *errval = win32_error("startfile", filepath);
10135 Py_DECREF(ofilepath);
10136 return errval;
10137 }
10138 Py_DECREF(ofilepath);
10139 Py_INCREF(Py_None);
10140 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +000010141}
10142#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010143
Martin v. Löwis438b5342002-12-27 10:16:42 +000010144#ifdef HAVE_GETLOADAVG
10145PyDoc_STRVAR(posix_getloadavg__doc__,
10146"getloadavg() -> (float, float, float)\n\n\
10147Return the number of processes in the system run queue averaged over\n\
10148the last 1, 5, and 15 minutes or raises OSError if the load average\n\
10149was unobtainable");
10150
10151static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +000010152posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +000010153{
10154 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000010155 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000010156 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
10157 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000010158 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000010159 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000010160}
10161#endif
10162
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000010163PyDoc_STRVAR(device_encoding__doc__,
10164"device_encoding(fd) -> str\n\n\
10165Return a string describing the encoding of the device\n\
10166if the output is a terminal; else return None.");
10167
10168static PyObject *
10169device_encoding(PyObject *self, PyObject *args)
10170{
Victor Stinner8c62be82010-05-06 00:08:46 +000010171 int fd;
Brett Cannonefb00c02012-02-29 18:31:31 -050010172
Victor Stinner8c62be82010-05-06 00:08:46 +000010173 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
10174 return NULL;
Brett Cannonefb00c02012-02-29 18:31:31 -050010175
10176 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000010177}
10178
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010179#ifdef HAVE_SETRESUID
10180PyDoc_STRVAR(posix_setresuid__doc__,
10181"setresuid(ruid, euid, suid)\n\n\
10182Set the current process's real, effective, and saved user ids.");
10183
10184static PyObject*
10185posix_setresuid (PyObject *self, PyObject *args)
10186{
Victor Stinner8c62be82010-05-06 00:08:46 +000010187 /* We assume uid_t is no larger than a long. */
10188 long ruid, euid, suid;
10189 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
10190 return NULL;
10191 if (setresuid(ruid, euid, suid) < 0)
10192 return posix_error();
10193 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010194}
10195#endif
10196
10197#ifdef HAVE_SETRESGID
10198PyDoc_STRVAR(posix_setresgid__doc__,
10199"setresgid(rgid, egid, sgid)\n\n\
10200Set the current process's real, effective, and saved group ids.");
10201
10202static PyObject*
10203posix_setresgid (PyObject *self, PyObject *args)
10204{
Victor Stinner8c62be82010-05-06 00:08:46 +000010205 /* We assume uid_t is no larger than a long. */
10206 long rgid, egid, sgid;
10207 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
10208 return NULL;
10209 if (setresgid(rgid, egid, sgid) < 0)
10210 return posix_error();
10211 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010212}
10213#endif
10214
10215#ifdef HAVE_GETRESUID
10216PyDoc_STRVAR(posix_getresuid__doc__,
10217"getresuid() -> (ruid, euid, suid)\n\n\
10218Get tuple of the current process's real, effective, and saved user ids.");
10219
10220static PyObject*
10221posix_getresuid (PyObject *self, PyObject *noargs)
10222{
Victor Stinner8c62be82010-05-06 00:08:46 +000010223 uid_t ruid, euid, suid;
10224 long l_ruid, l_euid, l_suid;
10225 if (getresuid(&ruid, &euid, &suid) < 0)
10226 return posix_error();
10227 /* Force the values into long's as we don't know the size of uid_t. */
10228 l_ruid = ruid;
10229 l_euid = euid;
10230 l_suid = suid;
10231 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010232}
10233#endif
10234
10235#ifdef HAVE_GETRESGID
10236PyDoc_STRVAR(posix_getresgid__doc__,
10237"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +000010238Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010239
10240static PyObject*
10241posix_getresgid (PyObject *self, PyObject *noargs)
10242{
Victor Stinner8c62be82010-05-06 00:08:46 +000010243 uid_t rgid, egid, sgid;
10244 long l_rgid, l_egid, l_sgid;
10245 if (getresgid(&rgid, &egid, &sgid) < 0)
10246 return posix_error();
10247 /* Force the values into long's as we don't know the size of uid_t. */
10248 l_rgid = rgid;
10249 l_egid = egid;
10250 l_sgid = sgid;
10251 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010252}
10253#endif
10254
Benjamin Peterson9428d532011-09-14 11:45:52 -040010255#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040010256
Benjamin Peterson799bd802011-08-31 22:15:17 -040010257PyDoc_STRVAR(posix_getxattr__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -070010258"getxattr(path, attribute, *, follow_symlinks=True) -> value\n\n\
10259Return the value of extended attribute attribute on path.\n\
10260\n\
10261path may be either a string or an open file descriptor.\n\
10262If follow_symlinks is False, and the last element of the path is a symbolic\n\
10263 link, getxattr will examine the symbolic link itself instead of the file\n\
10264 the link points to.");
Benjamin Peterson799bd802011-08-31 22:15:17 -040010265
10266static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -070010267posix_getxattr(PyObject *self, PyObject *args, PyObject *kwargs)
Benjamin Peterson799bd802011-08-31 22:15:17 -040010268{
Larry Hastings9cf065c2012-06-22 16:30:09 -070010269 path_t path;
10270 path_t attribute;
10271 int follow_symlinks = 1;
10272 PyObject *buffer = NULL;
10273 int i;
10274 static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL};
Benjamin Peterson799bd802011-08-31 22:15:17 -040010275
Larry Hastings9cf065c2012-06-22 16:30:09 -070010276 memset(&path, 0, sizeof(path));
10277 memset(&attribute, 0, sizeof(attribute));
10278 path.allow_fd = 1;
10279 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:getxattr", keywords,
10280 path_converter, &path,
10281 path_converter, &attribute,
10282 &follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040010283 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010284
Larry Hastings9cf065c2012-06-22 16:30:09 -070010285 if (fd_and_follow_symlinks_invalid("getxattr", path.fd, follow_symlinks))
10286 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010287
Larry Hastings9cf065c2012-06-22 16:30:09 -070010288 for (i = 0; ; i++) {
10289 void *ptr;
10290 ssize_t result;
10291 static Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
10292 Py_ssize_t buffer_size = buffer_sizes[i];
10293 if (!buffer_size) {
10294 path_error("getxattr", &path);
10295 goto exit;
10296 }
10297 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
10298 if (!buffer)
10299 goto exit;
10300 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010301
Larry Hastings9cf065c2012-06-22 16:30:09 -070010302 Py_BEGIN_ALLOW_THREADS;
10303 if (path.fd >= 0)
10304 result = fgetxattr(path.fd, attribute.narrow, ptr, buffer_size);
10305 else if (follow_symlinks)
10306 result = getxattr(path.narrow, attribute.narrow, ptr, buffer_size);
10307 else
10308 result = lgetxattr(path.narrow, attribute.narrow, ptr, buffer_size);
10309 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010310
Larry Hastings9cf065c2012-06-22 16:30:09 -070010311 if (result < 0) {
10312 Py_DECREF(buffer);
10313 buffer = NULL;
10314 if (errno == ERANGE)
10315 continue;
10316 path_error("getxattr", &path);
10317 goto exit;
10318 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010319
Larry Hastings9cf065c2012-06-22 16:30:09 -070010320 if (result != buffer_size) {
10321 /* Can only shrink. */
10322 _PyBytes_Resize(&buffer, result);
10323 }
10324 break;
10325 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010326
Larry Hastings9cf065c2012-06-22 16:30:09 -070010327exit:
10328 path_cleanup(&path);
10329 path_cleanup(&attribute);
10330 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010331}
10332
10333PyDoc_STRVAR(posix_setxattr__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -070010334"setxattr(path, attribute, value, flags=0, *, follow_symlinks=True)\n\n\
10335Set extended attribute attribute on path to value.\n\
10336path may be either a string or an open file descriptor.\n\
10337If follow_symlinks is False, and the last element of the path is a symbolic\n\
10338 link, setxattr will modify the symbolic link itself instead of the file\n\
10339 the link points to.");
Benjamin Peterson799bd802011-08-31 22:15:17 -040010340
10341static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -070010342posix_setxattr(PyObject *self, PyObject *args, PyObject *kwargs)
Benjamin Peterson799bd802011-08-31 22:15:17 -040010343{
Larry Hastings9cf065c2012-06-22 16:30:09 -070010344 path_t path;
10345 path_t attribute;
10346 Py_buffer value;
10347 int flags = 0;
10348 int follow_symlinks = 1;
10349 int result;
10350 PyObject *return_value = NULL;
10351 static char *keywords[] = {"path", "attribute", "value",
10352 "flags", "follow_symlinks", NULL};
Benjamin Peterson799bd802011-08-31 22:15:17 -040010353
Larry Hastings9cf065c2012-06-22 16:30:09 -070010354 memset(&path, 0, sizeof(path));
10355 path.allow_fd = 1;
10356 memset(&attribute, 0, sizeof(attribute));
10357 memset(&value, 0, sizeof(value));
10358 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&y*|i$p:setxattr",
10359 keywords,
10360 path_converter, &path,
10361 path_converter, &attribute,
10362 &value, &flags,
10363 &follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040010364 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010365
10366 if (fd_and_follow_symlinks_invalid("setxattr", path.fd, follow_symlinks))
10367 goto exit;
10368
Benjamin Peterson799bd802011-08-31 22:15:17 -040010369 Py_BEGIN_ALLOW_THREADS;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010370 if (path.fd > -1)
10371 result = fsetxattr(path.fd, attribute.narrow,
10372 value.buf, value.len, flags);
10373 else if (follow_symlinks)
10374 result = setxattr(path.narrow, attribute.narrow,
10375 value.buf, value.len, flags);
10376 else
10377 result = lsetxattr(path.narrow, attribute.narrow,
10378 value.buf, value.len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010379 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010380
Larry Hastings9cf065c2012-06-22 16:30:09 -070010381 if (result) {
10382 return_value = path_error("setxattr", &path);
10383 goto exit;
10384 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010385
Larry Hastings9cf065c2012-06-22 16:30:09 -070010386 return_value = Py_None;
10387 Py_INCREF(return_value);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010388
Larry Hastings9cf065c2012-06-22 16:30:09 -070010389exit:
10390 path_cleanup(&path);
10391 path_cleanup(&attribute);
10392 PyBuffer_Release(&value);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010393
Larry Hastings9cf065c2012-06-22 16:30:09 -070010394 return return_value;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010395}
10396
10397PyDoc_STRVAR(posix_removexattr__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -070010398"removexattr(path, attribute, *, follow_symlinks=True)\n\n\
10399Remove extended attribute attribute on path.\n\
10400path may be either a string or an open file descriptor.\n\
10401If follow_symlinks is False, and the last element of the path is a symbolic\n\
10402 link, removexattr will modify the symbolic link itself instead of the file\n\
10403 the link points to.");
Benjamin Peterson799bd802011-08-31 22:15:17 -040010404
10405static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -070010406posix_removexattr(PyObject *self, PyObject *args, PyObject *kwargs)
Benjamin Peterson799bd802011-08-31 22:15:17 -040010407{
Larry Hastings9cf065c2012-06-22 16:30:09 -070010408 path_t path;
10409 path_t attribute;
10410 int follow_symlinks = 1;
10411 int result;
10412 PyObject *return_value = NULL;
10413 static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL};
Benjamin Peterson799bd802011-08-31 22:15:17 -040010414
Larry Hastings9cf065c2012-06-22 16:30:09 -070010415 memset(&path, 0, sizeof(path));
10416 memset(&attribute, 0, sizeof(attribute));
10417 path.allow_fd = 1;
10418 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:removexattr",
10419 keywords,
10420 path_converter, &path,
10421 path_converter, &attribute,
10422 &follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040010423 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010424
10425 if (fd_and_follow_symlinks_invalid("removexattr", path.fd, follow_symlinks))
10426 goto exit;
10427
Benjamin Peterson799bd802011-08-31 22:15:17 -040010428 Py_BEGIN_ALLOW_THREADS;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010429 if (path.fd > -1)
10430 result = fremovexattr(path.fd, attribute.narrow);
10431 else if (follow_symlinks)
10432 result = removexattr(path.narrow, attribute.narrow);
10433 else
10434 result = lremovexattr(path.narrow, attribute.narrow);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010435 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010436
Larry Hastings9cf065c2012-06-22 16:30:09 -070010437 if (result) {
10438 return_value = path_error("removexattr", &path);
10439 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010440 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010441
Larry Hastings9cf065c2012-06-22 16:30:09 -070010442 return_value = Py_None;
10443 Py_INCREF(return_value);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010444
Larry Hastings9cf065c2012-06-22 16:30:09 -070010445exit:
10446 path_cleanup(&path);
10447 path_cleanup(&attribute);
10448
10449 return return_value;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010450}
10451
10452PyDoc_STRVAR(posix_listxattr__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -070010453"listxattr(path='.', *, follow_symlinks=True)\n\n\
10454Return a list of extended attributes on path.\n\
10455\n\
10456path may be either None, a string, or an open file descriptor.\n\
10457if path is None, listxattr will examine the current directory.\n\
10458If follow_symlinks is False, and the last element of the path is a symbolic\n\
10459 link, listxattr will examine the symbolic link itself instead of the file\n\
10460 the link points to.");
Benjamin Peterson799bd802011-08-31 22:15:17 -040010461
10462static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -070010463posix_listxattr(PyObject *self, PyObject *args, PyObject *kwargs)
Benjamin Peterson799bd802011-08-31 22:15:17 -040010464{
Larry Hastings9cf065c2012-06-22 16:30:09 -070010465 path_t path;
10466 int follow_symlinks = 1;
10467 Py_ssize_t i;
10468 PyObject *result = NULL;
10469 char *buffer = NULL;
10470 char *name;
10471 static char *keywords[] = {"path", "follow_symlinks", NULL};
Benjamin Peterson799bd802011-08-31 22:15:17 -040010472
Larry Hastings9cf065c2012-06-22 16:30:09 -070010473 memset(&path, 0, sizeof(path));
10474 path.allow_fd = 1;
10475 path.fd = -1;
10476 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&$p:listxattr", keywords,
10477 path_converter, &path,
10478 &follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040010479 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010480
Larry Hastings9cf065c2012-06-22 16:30:09 -070010481 if (fd_and_follow_symlinks_invalid("listxattr", path.fd, follow_symlinks))
10482 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010483
Larry Hastings9cf065c2012-06-22 16:30:09 -070010484 name = path.narrow ? path.narrow : ".";
10485 for (i = 0; ; i++) {
10486 char *start, *trace, *end;
10487 ssize_t length;
10488 static Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
10489 Py_ssize_t buffer_size = buffer_sizes[i];
10490 if (!buffer_size) {
10491 // ERANGE
10492 path_error("listxattr", &path);
10493 break;
10494 }
10495 buffer = PyMem_MALLOC(buffer_size);
10496 if (!buffer) {
10497 PyErr_NoMemory();
10498 break;
10499 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010500
Larry Hastings9cf065c2012-06-22 16:30:09 -070010501 Py_BEGIN_ALLOW_THREADS;
10502 if (path.fd > -1)
10503 length = flistxattr(path.fd, buffer, buffer_size);
10504 else if (follow_symlinks)
10505 length = listxattr(name, buffer, buffer_size);
10506 else
10507 length = llistxattr(name, buffer, buffer_size);
10508 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010509
Larry Hastings9cf065c2012-06-22 16:30:09 -070010510 if (length < 0) {
10511 if (errno == ERANGE)
10512 continue;
10513 path_error("listxattr", &path);
10514 break;
10515 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010516
Larry Hastings9cf065c2012-06-22 16:30:09 -070010517 result = PyList_New(0);
10518 if (!result) {
10519 goto exit;
10520 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010521
Larry Hastings9cf065c2012-06-22 16:30:09 -070010522 end = buffer + length;
10523 for (trace = start = buffer; trace != end; trace++) {
10524 if (!*trace) {
10525 int error;
10526 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
10527 trace - start);
10528 if (!attribute) {
10529 Py_DECREF(result);
10530 result = NULL;
10531 goto exit;
10532 }
10533 error = PyList_Append(result, attribute);
10534 Py_DECREF(attribute);
10535 if (error) {
10536 Py_DECREF(result);
10537 result = NULL;
10538 goto exit;
10539 }
10540 start = trace + 1;
10541 }
10542 }
10543 break;
10544 }
10545exit:
10546 path_cleanup(&path);
10547 if (buffer)
10548 PyMem_FREE(buffer);
10549 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010550}
10551
Benjamin Peterson9428d532011-09-14 11:45:52 -040010552#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040010553
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010554
Georg Brandl2fb477c2012-02-21 00:33:36 +010010555PyDoc_STRVAR(posix_urandom__doc__,
10556"urandom(n) -> str\n\n\
10557Return n random bytes suitable for cryptographic use.");
10558
10559static PyObject *
10560posix_urandom(PyObject *self, PyObject *args)
10561{
10562 Py_ssize_t size;
10563 PyObject *result;
10564 int ret;
10565
10566 /* Read arguments */
10567 if (!PyArg_ParseTuple(args, "n:urandom", &size))
10568 return NULL;
10569 if (size < 0)
10570 return PyErr_Format(PyExc_ValueError,
10571 "negative argument not allowed");
10572 result = PyBytes_FromStringAndSize(NULL, size);
10573 if (result == NULL)
10574 return NULL;
10575
10576 ret = _PyOS_URandom(PyBytes_AS_STRING(result),
10577 PyBytes_GET_SIZE(result));
10578 if (ret == -1) {
10579 Py_DECREF(result);
10580 return NULL;
10581 }
10582 return result;
10583}
10584
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010585/* Terminal size querying */
10586
10587static PyTypeObject TerminalSizeType;
10588
10589PyDoc_STRVAR(TerminalSize_docstring,
10590 "A tuple of (columns, lines) for holding terminal window size");
10591
10592static PyStructSequence_Field TerminalSize_fields[] = {
10593 {"columns", "width of the terminal window in characters"},
10594 {"lines", "height of the terminal window in characters"},
10595 {NULL, NULL}
10596};
10597
10598static PyStructSequence_Desc TerminalSize_desc = {
10599 "os.terminal_size",
10600 TerminalSize_docstring,
10601 TerminalSize_fields,
10602 2,
10603};
10604
10605#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
10606PyDoc_STRVAR(termsize__doc__,
10607 "Return the size of the terminal window as (columns, lines).\n" \
10608 "\n" \
10609 "The optional argument fd (default standard output) specifies\n" \
10610 "which file descriptor should be queried.\n" \
10611 "\n" \
10612 "If the file descriptor is not connected to a terminal, an OSError\n" \
10613 "is thrown.\n" \
10614 "\n" \
10615 "This function will only be defined if an implementation is\n" \
10616 "available for this system.\n" \
10617 "\n" \
10618 "shutil.get_terminal_size is the high-level function which should \n" \
10619 "normally be used, os.get_terminal_size is the low-level implementation.");
10620
10621static PyObject*
10622get_terminal_size(PyObject *self, PyObject *args)
10623{
10624 int columns, lines;
10625 PyObject *termsize;
10626
10627 int fd = fileno(stdout);
10628 /* Under some conditions stdout may not be connected and
10629 * fileno(stdout) may point to an invalid file descriptor. For example
10630 * GUI apps don't have valid standard streams by default.
10631 *
10632 * If this happens, and the optional fd argument is not present,
10633 * the ioctl below will fail returning EBADF. This is what we want.
10634 */
10635
10636 if (!PyArg_ParseTuple(args, "|i", &fd))
10637 return NULL;
10638
10639#ifdef TERMSIZE_USE_IOCTL
10640 {
10641 struct winsize w;
10642 if (ioctl(fd, TIOCGWINSZ, &w))
10643 return PyErr_SetFromErrno(PyExc_OSError);
10644 columns = w.ws_col;
10645 lines = w.ws_row;
10646 }
10647#endif /* TERMSIZE_USE_IOCTL */
10648
10649#ifdef TERMSIZE_USE_CONIO
10650 {
10651 DWORD nhandle;
10652 HANDLE handle;
10653 CONSOLE_SCREEN_BUFFER_INFO csbi;
10654 switch (fd) {
10655 case 0: nhandle = STD_INPUT_HANDLE;
10656 break;
10657 case 1: nhandle = STD_OUTPUT_HANDLE;
10658 break;
10659 case 2: nhandle = STD_ERROR_HANDLE;
10660 break;
10661 default:
10662 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
10663 }
10664 handle = GetStdHandle(nhandle);
10665 if (handle == NULL)
10666 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
10667 if (handle == INVALID_HANDLE_VALUE)
10668 return PyErr_SetFromWindowsErr(0);
10669
10670 if (!GetConsoleScreenBufferInfo(handle, &csbi))
10671 return PyErr_SetFromWindowsErr(0);
10672
10673 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
10674 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
10675 }
10676#endif /* TERMSIZE_USE_CONIO */
10677
10678 termsize = PyStructSequence_New(&TerminalSizeType);
10679 if (termsize == NULL)
10680 return NULL;
10681 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
10682 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
10683 if (PyErr_Occurred()) {
10684 Py_DECREF(termsize);
10685 return NULL;
10686 }
10687 return termsize;
10688}
10689#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
10690
10691
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010692static PyMethodDef posix_methods[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070010693 {"access", (PyCFunction)posix_access,
10694 METH_VARARGS | METH_KEYWORDS,
10695 posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010696#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010697 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010698#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -070010699 {"chdir", (PyCFunction)posix_chdir,
10700 METH_VARARGS | METH_KEYWORDS,
10701 posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010702#ifdef HAVE_CHFLAGS
Larry Hastings9cf065c2012-06-22 16:30:09 -070010703 {"chflags", (PyCFunction)posix_chflags,
10704 METH_VARARGS | METH_KEYWORDS,
10705 posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010706#endif /* HAVE_CHFLAGS */
Larry Hastings9cf065c2012-06-22 16:30:09 -070010707 {"chmod", (PyCFunction)posix_chmod,
10708 METH_VARARGS | METH_KEYWORDS,
10709 posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010710#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010711 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010712#endif /* HAVE_FCHMOD */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010713#ifdef HAVE_CHOWN
Larry Hastings9cf065c2012-06-22 16:30:09 -070010714 {"chown", (PyCFunction)posix_chown,
10715 METH_VARARGS | METH_KEYWORDS,
10716 posix_chown__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010717#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +000010718#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010719 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010720#endif /* HAVE_LCHMOD */
10721#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010722 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010723#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +000010724#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010725 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010726#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010727#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010728 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010729#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +000010730#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +000010731 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +000010732#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010733#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +000010734 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010735#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010736#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +000010737 {"getcwd", (PyCFunction)posix_getcwd_unicode,
10738 METH_NOARGS, posix_getcwd__doc__},
10739 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
10740 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010741#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -070010742#if defined(HAVE_LINK) || defined(MS_WINDOWS)
10743 {"link", (PyCFunction)posix_link,
10744 METH_VARARGS | METH_KEYWORDS,
10745 posix_link__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010746#endif /* HAVE_LINK */
Larry Hastings9cf065c2012-06-22 16:30:09 -070010747 {"listdir", (PyCFunction)posix_listdir,
10748 METH_VARARGS | METH_KEYWORDS,
10749 posix_listdir__doc__},
10750 {"lstat", (PyCFunction)posix_lstat,
10751 METH_VARARGS | METH_KEYWORDS,
10752 posix_lstat__doc__},
10753 {"mkdir", (PyCFunction)posix_mkdir,
10754 METH_VARARGS | METH_KEYWORDS,
10755 posix_mkdir__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010756#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +000010757 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum775f4da1993-01-09 17:18:52 +000010758#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010759#ifdef HAVE_GETPRIORITY
10760 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
10761#endif /* HAVE_GETPRIORITY */
10762#ifdef HAVE_SETPRIORITY
10763 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
10764#endif /* HAVE_SETPRIORITY */
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010765#ifdef HAVE_READLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -070010766 {"readlink", (PyCFunction)posix_readlink,
10767 METH_VARARGS | METH_KEYWORDS,
10768 readlink__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010769#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000010770#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Larry Hastings9cf065c2012-06-22 16:30:09 -070010771 {"readlink", (PyCFunction)win_readlink,
10772 METH_VARARGS | METH_KEYWORDS,
10773 readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000010774#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Larry Hastings9cf065c2012-06-22 16:30:09 -070010775 {"rename", (PyCFunction)posix_rename,
10776 METH_VARARGS | METH_KEYWORDS,
10777 posix_rename__doc__},
10778 {"replace", (PyCFunction)posix_replace,
10779 METH_VARARGS | METH_KEYWORDS,
10780 posix_replace__doc__},
Larry Hastingsb698d8e2012-06-23 16:55:07 -070010781 {"rmdir", (PyCFunction)posix_rmdir,
10782 METH_VARARGS | METH_KEYWORDS,
10783 posix_rmdir__doc__},
Larry Hastings9cf065c2012-06-22 16:30:09 -070010784 {"stat", (PyCFunction)posix_stat,
10785 METH_VARARGS | METH_KEYWORDS,
10786 posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010787 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Larry Hastings9cf065c2012-06-22 16:30:09 -070010788#if defined(HAVE_SYMLINK)
10789 {"symlink", (PyCFunction)posix_symlink,
10790 METH_VARARGS | METH_KEYWORDS,
10791 posix_symlink__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010792#endif /* HAVE_SYMLINK */
Guido van Rossum85e3b011991-06-03 12:42:10 +000010793#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +000010794 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010795#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010796 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010797#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010798 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010799#endif /* HAVE_UNAME */
Larry Hastings9cf065c2012-06-22 16:30:09 -070010800 {"unlink", (PyCFunction)posix_unlink,
10801 METH_VARARGS | METH_KEYWORDS,
10802 posix_unlink__doc__},
10803 {"remove", (PyCFunction)posix_unlink,
10804 METH_VARARGS | METH_KEYWORDS,
10805 posix_remove__doc__},
Larry Hastings76ad59b2012-05-03 00:30:07 -070010806 {"utime", (PyCFunction)posix_utime,
10807 METH_VARARGS | METH_KEYWORDS, posix_utime__doc__},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000010808#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +000010809 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010810#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +000010811 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010812#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +000010813 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
Larry Hastings9cf065c2012-06-22 16:30:09 -070010814 {"execve", (PyCFunction)posix_execve,
10815 METH_VARARGS | METH_KEYWORDS,
10816 posix_execve__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010817#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +000010818#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +000010819 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
10820 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010821#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010822 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
10823 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010824#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +000010825#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010826#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +000010827 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010828#endif /* HAVE_FORK1 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010829#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +000010830 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010831#endif /* HAVE_FORK */
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010832#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010833#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010834 {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__},
10835 {"sched_get_priority_min", posix_sched_get_priority_min, METH_VARARGS, posix_sched_get_priority_min__doc__},
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010836#endif
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010837#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010838 {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010839#endif
10840#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010841 {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010842#endif
10843#ifdef HAVE_SCHED_RR_GET_INTERVAL
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010844 {"sched_rr_get_interval", posix_sched_rr_get_interval, METH_VARARGS, posix_sched_rr_get_interval__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010845#endif
10846#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010847 {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010848#endif
10849#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010850 {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010851#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010852 {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__},
Benjamin Peterson2740af82011-08-02 17:41:34 -050010853#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010854 {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__},
10855 {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__},
10856#endif
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010857#endif /* HAVE_SCHED_H */
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010858#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +000010859 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010860#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +000010861#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010862 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +000010863#endif /* HAVE_FORKPTY */
Guido van Rossum3b066191991-06-04 19:40:25 +000010864#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010865 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +000010866#endif /* HAVE_GETEGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010867#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010868 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossum46003ff1992-05-15 11:05:24 +000010869#endif /* HAVE_GETEUID */
10870#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010871 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010872#endif /* HAVE_GETGID */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020010873#ifdef HAVE_GETGROUPLIST
10874 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
10875#endif
Fred Drakec9680921999-12-13 16:37:25 +000010876#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010877 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010878#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010879 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010880#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010881 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010882#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010883#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010884 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010885#endif /* HAVE_GETPPID */
10886#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010887 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010888#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +000010889#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010890 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +000010891#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +000010892#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +000010893 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010894#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010895#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +000010896 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010897#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +000010898#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010899 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +000010900#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +000010901#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010902 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
10903 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +000010904#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010905#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010906 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010907#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +000010908#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010909 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +000010910#endif /* HAVE_SETEUID */
10911#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010912 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +000010913#endif /* HAVE_SETEGID */
10914#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010915 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +000010916#endif /* HAVE_SETREUID */
10917#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010918 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +000010919#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010920#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010921 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010922#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010923#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010924 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010925#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +000010926#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010927 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000010928#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +000010929#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010930 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +000010931#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010932#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010933 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010934#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010935#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010936 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010937#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010938#ifdef HAVE_WAIT3
Victor Stinner4195b5c2012-02-08 23:03:19 +010010939 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010940#endif /* HAVE_WAIT3 */
10941#ifdef HAVE_WAIT4
Victor Stinner4195b5c2012-02-08 23:03:19 +010010942 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010943#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010944#if defined(HAVE_WAITID) && !defined(__APPLE__)
10945 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
10946#endif
Tim Petersab034fa2002-02-01 11:27:43 +000010947#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010948 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010949#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010950#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010951 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010952#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010953#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010954 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010955#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010956#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010957 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010958#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010959#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010960 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010961#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010962#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010963 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010964#endif /* HAVE_TCSETPGRP */
Larry Hastings9cf065c2012-06-22 16:30:09 -070010965 {"open", (PyCFunction)posix_open,\
10966 METH_VARARGS | METH_KEYWORDS,
10967 posix_open__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010968 {"close", posix_close, METH_VARARGS, posix_close__doc__},
10969 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
10970 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
10971 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
10972 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010973#ifdef HAVE_LOCKF
10974 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
10975#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010976 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
10977 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010978#ifdef HAVE_READV
10979 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
10980#endif
10981#ifdef HAVE_PREAD
10982 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
10983#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010984 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010985#ifdef HAVE_WRITEV
10986 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
10987#endif
10988#ifdef HAVE_PWRITE
10989 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
10990#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010991#ifdef HAVE_SENDFILE
10992 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
10993 posix_sendfile__doc__},
10994#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +010010995 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010996 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010997#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010998 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010999#endif
Charles-François Natalidaafdd52011-05-29 20:07:40 +020011000#ifdef HAVE_PIPE2
Charles-François Natali368f34b2011-06-06 19:49:47 +020011001 {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__},
Charles-François Natalidaafdd52011-05-29 20:07:40 +020011002#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000011003#ifdef HAVE_MKFIFO
Larry Hastings9cf065c2012-06-22 16:30:09 -070011004 {"mkfifo", (PyCFunction)posix_mkfifo,
11005 METH_VARARGS | METH_KEYWORDS,
11006 posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000011007#endif
Neal Norwitz11690112002-07-30 01:08:28 +000011008#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Larry Hastings9cf065c2012-06-22 16:30:09 -070011009 {"mknod", (PyCFunction)posix_mknod,
11010 METH_VARARGS | METH_KEYWORDS,
11011 posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +000011012#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000011013#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +000011014 {"major", posix_major, METH_VARARGS, posix_major__doc__},
11015 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
11016 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000011017#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000011018#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +000011019 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000011020#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020011021#ifdef HAVE_TRUNCATE
Georg Brandl306336b2012-06-24 12:55:33 +020011022 {"truncate", (PyCFunction)posix_truncate,
11023 METH_VARARGS | METH_KEYWORDS,
11024 posix_truncate__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020011025#endif
11026#ifdef HAVE_POSIX_FALLOCATE
11027 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
11028#endif
11029#ifdef HAVE_POSIX_FADVISE
11030 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
11031#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000011032#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000011033 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000011034#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000011035#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +000011036 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +000011037#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011038 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000011039#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +000011040 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000011041#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000011042#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011043 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000011044#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020011045#ifdef HAVE_SYNC
11046 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
11047#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000011048#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011049 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000011050#endif
Guido van Rossumc9641791998-08-04 15:26:23 +000011051#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000011052#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +000011053 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +000011054#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000011055#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000011056 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000011057#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +000011058#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +000011059 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000011060#endif /* WIFSTOPPED */
11061#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +000011062 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000011063#endif /* WIFSIGNALED */
11064#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +000011065 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000011066#endif /* WIFEXITED */
11067#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +000011068 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000011069#endif /* WEXITSTATUS */
11070#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011071 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000011072#endif /* WTERMSIG */
11073#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011074 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000011075#endif /* WSTOPSIG */
11076#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +000011077#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000011078 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000011079#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000011080#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Larry Hastings9cf065c2012-06-22 16:30:09 -070011081 {"statvfs", (PyCFunction)posix_statvfs,
11082 METH_VARARGS | METH_KEYWORDS,
11083 posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000011084#endif
Fred Drakec9680921999-12-13 16:37:25 +000011085#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +000011086 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000011087#endif
11088#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011089 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000011090#endif
11091#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011092 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000011093#endif
11094#ifdef HAVE_PATHCONF
Georg Brandl306336b2012-06-24 12:55:33 +020011095 {"pathconf", (PyCFunction)posix_pathconf,
11096 METH_VARARGS | METH_KEYWORDS,
11097 posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000011098#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011099 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000011100#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000011101 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +000011102 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +000011103 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -050011104 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011105 {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +000011106#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +000011107#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +000011108 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +000011109#endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +010011110 {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011111#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000011112 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011113#endif
11114#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000011115 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011116#endif
11117#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000011118 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011119#endif
11120#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000011121 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011122#endif
11123
Benjamin Peterson9428d532011-09-14 11:45:52 -040011124#ifdef USE_XATTRS
Larry Hastings9cf065c2012-06-22 16:30:09 -070011125 {"setxattr", (PyCFunction)posix_setxattr,
11126 METH_VARARGS | METH_KEYWORDS,
11127 posix_setxattr__doc__},
11128 {"getxattr", (PyCFunction)posix_getxattr,
11129 METH_VARARGS | METH_KEYWORDS,
11130 posix_getxattr__doc__},
11131 {"removexattr", (PyCFunction)posix_removexattr,
11132 METH_VARARGS | METH_KEYWORDS,
11133 posix_removexattr__doc__},
11134 {"listxattr", (PyCFunction)posix_listxattr,
11135 METH_VARARGS | METH_KEYWORDS,
11136 posix_listxattr__doc__},
Benjamin Peterson799bd802011-08-31 22:15:17 -040011137#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011138#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
11139 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
11140#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011141 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011142};
11143
11144
Barry Warsaw4a342091996-12-19 23:50:02 +000011145static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000011146ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +000011147{
Victor Stinner8c62be82010-05-06 00:08:46 +000011148 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +000011149}
11150
Guido van Rossumd48f2521997-12-05 22:19:34 +000011151#if defined(PYOS_OS2)
11152/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +000011153static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +000011154{
11155 APIRET rc;
11156 ULONG values[QSV_MAX+1];
11157 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +000011158 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +000011159
11160 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +000011161 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +000011162 Py_END_ALLOW_THREADS
11163
11164 if (rc != NO_ERROR) {
11165 os2_error(rc);
11166 return -1;
11167 }
11168
Fred Drake4d1e64b2002-04-15 19:40:07 +000011169 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
11170 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
11171 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
11172 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
11173 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
11174 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
11175 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011176
11177 switch (values[QSV_VERSION_MINOR]) {
11178 case 0: ver = "2.00"; break;
11179 case 10: ver = "2.10"; break;
11180 case 11: ver = "2.11"; break;
11181 case 30: ver = "3.00"; break;
11182 case 40: ver = "4.00"; break;
11183 case 50: ver = "5.00"; break;
11184 default:
Tim Peters885d4572001-11-28 20:27:42 +000011185 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +000011186 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +000011187 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +000011188 ver = &tmp[0];
11189 }
11190
11191 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +000011192 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +000011193 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011194
11195 /* Add Indicator of Which Drive was Used to Boot the System */
11196 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
11197 tmp[1] = ':';
11198 tmp[2] = '\0';
11199
Fred Drake4d1e64b2002-04-15 19:40:07 +000011200 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +000011201}
11202#endif
11203
Brian Curtin52173d42010-12-02 18:29:18 +000011204#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000011205static int
Brian Curtin52173d42010-12-02 18:29:18 +000011206enable_symlink()
11207{
11208 HANDLE tok;
11209 TOKEN_PRIVILEGES tok_priv;
11210 LUID luid;
11211 int meth_idx = 0;
11212
11213 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000011214 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000011215
11216 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000011217 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000011218
11219 tok_priv.PrivilegeCount = 1;
11220 tok_priv.Privileges[0].Luid = luid;
11221 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
11222
11223 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
11224 sizeof(TOKEN_PRIVILEGES),
11225 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000011226 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000011227
Brian Curtin3b4499c2010-12-28 14:31:47 +000011228 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
11229 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000011230}
11231#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
11232
Barry Warsaw4a342091996-12-19 23:50:02 +000011233static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000011234all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +000011235{
Guido van Rossum94f6f721999-01-06 18:42:14 +000011236#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011237 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011238#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011239#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011240 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011241#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011242#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011243 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011244#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011245#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011246 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011247#endif
Fred Drakec9680921999-12-13 16:37:25 +000011248#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011249 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000011250#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011251#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011252 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011253#endif
Fred Drake106c1a02002-04-23 15:58:02 +000011254#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000011255 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000011256#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000011257#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +000011258 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011259#endif
Fred Drake106c1a02002-04-23 15:58:02 +000011260#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +000011261 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000011262#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000011263#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000011264 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011265#endif
11266#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000011267 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011268#endif
11269#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +000011270 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011271#endif
11272#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +000011273 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011274#endif
11275#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011276 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011277#endif
11278#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +000011279 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011280#endif
11281#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011282 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011283#endif
11284#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011285 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011286#endif
11287#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011288 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011289#endif
11290#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +000011291 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011292#endif
11293#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000011294 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011295#endif
11296#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +000011297 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011298#endif
11299#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011300 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011301#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000011302#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +000011303 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011304#endif
11305#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +000011306 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011307#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020011308#ifdef O_XATTR
11309 if (ins(d, "O_XATTR", (long)O_XATTR)) return -1;
11310#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011311#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000011312 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011313#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +000011314#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011315 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011316#endif
11317#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011318 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011319#endif
Jesus Ceacf381202012-04-24 20:44:40 +020011320#ifdef O_EXEC
11321 if (ins(d, "O_EXEC", (long)O_EXEC)) return -1;
11322#endif
11323#ifdef O_SEARCH
11324 if (ins(d, "O_SEARCH", (long)O_SEARCH)) return -1;
11325#endif
11326#ifdef O_TTY_INIT
11327 if (ins(d, "O_TTY_INIT", (long)O_TTY_INIT)) return -1;
11328#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011329#ifdef PRIO_PROCESS
11330 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
11331#endif
11332#ifdef PRIO_PGRP
11333 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
11334#endif
11335#ifdef PRIO_USER
11336 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
11337#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020011338#ifdef O_CLOEXEC
11339 if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1;
11340#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020011341#ifdef O_ACCMODE
11342 if (ins(d, "O_ACCMODE", (long)O_ACCMODE)) return -1;
11343#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011344
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011345
Jesus Cea94363612012-06-22 18:32:07 +020011346#ifdef SEEK_HOLE
11347 if (ins(d, "SEEK_HOLE", (long)SEEK_HOLE)) return -1;
11348#endif
11349#ifdef SEEK_DATA
11350 if (ins(d, "SEEK_DATA", (long)SEEK_DATA)) return -1;
11351#endif
11352
Tim Peters5aa91602002-01-30 05:46:57 +000011353/* MS Windows */
11354#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011355 /* Don't inherit in child processes. */
11356 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011357#endif
11358#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000011359 /* Optimize for short life (keep in memory). */
11360 /* MS forgot to define this one with a non-underscore form too. */
11361 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011362#endif
11363#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000011364 /* Automatically delete when last handle is closed. */
11365 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011366#endif
11367#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000011368 /* Optimize for random access. */
11369 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011370#endif
11371#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011372 /* Optimize for sequential access. */
11373 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011374#endif
11375
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011376/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011377#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011378 /* Send a SIGIO signal whenever input or output
11379 becomes available on file descriptor */
11380 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011381#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011382#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011383 /* Direct disk access. */
11384 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011385#endif
11386#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000011387 /* Must be a directory. */
11388 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011389#endif
11390#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000011391 /* Do not follow links. */
11392 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011393#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020011394#ifdef O_NOLINKS
11395 /* Fails if link count of the named file is greater than 1 */
11396 if (ins(d, "O_NOLINKS", (long)O_NOLINKS)) return -1;
11397#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011398#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011399 /* Do not update the access time. */
11400 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011401#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000011402
Victor Stinner8c62be82010-05-06 00:08:46 +000011403 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011404#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011405 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011406#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011407#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011408 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011409#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011410#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011411 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011412#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011413#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000011414 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011415#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011416#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +000011417 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011418#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011419#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +000011420 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011421#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011422#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000011423 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011424#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011425#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +000011426 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011427#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011428#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011429 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011430#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011431#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000011432 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011433#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011434#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000011435 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011436#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011437#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011438 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011439#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011440#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +000011441 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011442#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011443#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +000011444 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011445#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011446#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +000011447 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011448#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011449#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011450 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011451#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011452#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +000011453 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011454#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011455
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011456 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011457#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011458 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011459#endif /* ST_RDONLY */
11460#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011461 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011462#endif /* ST_NOSUID */
11463
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011464 /* FreeBSD sendfile() constants */
11465#ifdef SF_NODISKIO
11466 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
11467#endif
11468#ifdef SF_MNOWAIT
11469 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
11470#endif
11471#ifdef SF_SYNC
11472 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
11473#endif
11474
Ross Lagerwall7807c352011-03-17 20:20:30 +020011475 /* constants for posix_fadvise */
11476#ifdef POSIX_FADV_NORMAL
11477 if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1;
11478#endif
11479#ifdef POSIX_FADV_SEQUENTIAL
11480 if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1;
11481#endif
11482#ifdef POSIX_FADV_RANDOM
11483 if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1;
11484#endif
11485#ifdef POSIX_FADV_NOREUSE
11486 if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1;
11487#endif
11488#ifdef POSIX_FADV_WILLNEED
11489 if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1;
11490#endif
11491#ifdef POSIX_FADV_DONTNEED
11492 if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1;
11493#endif
11494
11495 /* constants for waitid */
11496#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
11497 if (ins(d, "P_PID", (long)P_PID)) return -1;
11498 if (ins(d, "P_PGID", (long)P_PGID)) return -1;
11499 if (ins(d, "P_ALL", (long)P_ALL)) return -1;
11500#endif
11501#ifdef WEXITED
11502 if (ins(d, "WEXITED", (long)WEXITED)) return -1;
11503#endif
11504#ifdef WNOWAIT
11505 if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1;
11506#endif
11507#ifdef WSTOPPED
11508 if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1;
11509#endif
11510#ifdef CLD_EXITED
11511 if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1;
11512#endif
11513#ifdef CLD_DUMPED
11514 if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1;
11515#endif
11516#ifdef CLD_TRAPPED
11517 if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1;
11518#endif
11519#ifdef CLD_CONTINUED
11520 if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1;
11521#endif
11522
11523 /* constants for lockf */
11524#ifdef F_LOCK
11525 if (ins(d, "F_LOCK", (long)F_LOCK)) return -1;
11526#endif
11527#ifdef F_TLOCK
11528 if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1;
11529#endif
11530#ifdef F_ULOCK
11531 if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1;
11532#endif
11533#ifdef F_TEST
11534 if (ins(d, "F_TEST", (long)F_TEST)) return -1;
11535#endif
11536
Guido van Rossum246bc171999-02-01 23:54:31 +000011537#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011538#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +000011539 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
11540 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
11541 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
11542 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
11543 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
11544 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
11545 if (ins(d, "P_PM", (long)P_PM)) return -1;
11546 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
11547 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
11548 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
11549 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
11550 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
11551 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
11552 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
11553 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
11554 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
11555 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
11556 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
11557 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
11558 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011559#else
Victor Stinner8c62be82010-05-06 00:08:46 +000011560 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
11561 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
11562 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
11563 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
11564 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000011565#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011566#endif
Guido van Rossum246bc171999-02-01 23:54:31 +000011567
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011568#ifdef HAVE_SCHED_H
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011569 if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011570 if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1;
11571 if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1;
11572#ifdef SCHED_SPORADIC
11573 if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1;
11574#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011575#ifdef SCHED_BATCH
11576 if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1;
11577#endif
11578#ifdef SCHED_IDLE
11579 if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1;
11580#endif
11581#ifdef SCHED_RESET_ON_FORK
11582 if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1;
11583#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011584#ifdef SCHED_SYS
11585 if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1;
11586#endif
11587#ifdef SCHED_IA
11588 if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1;
11589#endif
11590#ifdef SCHED_FSS
11591 if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1;
11592#endif
11593#ifdef SCHED_FX
11594 if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1;
11595#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011596#endif
11597
Benjamin Peterson9428d532011-09-14 11:45:52 -040011598#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040011599 if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1;
11600 if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1;
11601 if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1;
11602#endif
11603
Victor Stinner8b905bd2011-10-25 13:34:04 +020011604#ifdef RTLD_LAZY
11605 if (PyModule_AddIntMacro(d, RTLD_LAZY)) return -1;
11606#endif
11607#ifdef RTLD_NOW
11608 if (PyModule_AddIntMacro(d, RTLD_NOW)) return -1;
11609#endif
11610#ifdef RTLD_GLOBAL
11611 if (PyModule_AddIntMacro(d, RTLD_GLOBAL)) return -1;
11612#endif
11613#ifdef RTLD_LOCAL
11614 if (PyModule_AddIntMacro(d, RTLD_LOCAL)) return -1;
11615#endif
11616#ifdef RTLD_NODELETE
11617 if (PyModule_AddIntMacro(d, RTLD_NODELETE)) return -1;
11618#endif
11619#ifdef RTLD_NOLOAD
11620 if (PyModule_AddIntMacro(d, RTLD_NOLOAD)) return -1;
11621#endif
11622#ifdef RTLD_DEEPBIND
11623 if (PyModule_AddIntMacro(d, RTLD_DEEPBIND)) return -1;
11624#endif
11625
Guido van Rossumd48f2521997-12-05 22:19:34 +000011626#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000011627 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011628#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011629 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000011630}
11631
11632
Tim Peters5aa91602002-01-30 05:46:57 +000011633#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011634#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011635#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011636
11637#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011638#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011639#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011640
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011641#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000011642#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011643#define MODNAME "posix"
11644#endif
11645
Martin v. Löwis1a214512008-06-11 05:26:20 +000011646static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000011647 PyModuleDef_HEAD_INIT,
11648 MODNAME,
11649 posix__doc__,
11650 -1,
11651 posix_methods,
11652 NULL,
11653 NULL,
11654 NULL,
11655 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000011656};
11657
11658
Larry Hastings9cf065c2012-06-22 16:30:09 -070011659static char *have_functions[] = {
11660
11661#ifdef HAVE_FACCESSAT
11662 "HAVE_FACCESSAT",
11663#endif
11664
11665#ifdef HAVE_FCHDIR
11666 "HAVE_FCHDIR",
11667#endif
11668
11669#ifdef HAVE_FCHMOD
11670 "HAVE_FCHMOD",
11671#endif
11672
11673#ifdef HAVE_FCHMODAT
11674 "HAVE_FCHMODAT",
11675#endif
11676
11677#ifdef HAVE_FCHOWN
11678 "HAVE_FCHOWN",
11679#endif
11680
11681#ifdef HAVE_FEXECVE
11682 "HAVE_FEXECVE",
11683#endif
11684
11685#ifdef HAVE_FDOPENDIR
11686 "HAVE_FDOPENDIR",
11687#endif
11688
Georg Brandl306336b2012-06-24 12:55:33 +020011689#ifdef HAVE_FPATHCONF
11690 "HAVE_FPATHCONF",
11691#endif
11692
Larry Hastings9cf065c2012-06-22 16:30:09 -070011693#ifdef HAVE_FSTATAT
11694 "HAVE_FSTATAT",
11695#endif
11696
11697#ifdef HAVE_FSTATVFS
11698 "HAVE_FSTATVFS",
11699#endif
11700
Georg Brandl306336b2012-06-24 12:55:33 +020011701#ifdef HAVE_FTRUNCATE
11702 "HAVE_FTRUNCATE",
11703#endif
11704
Larry Hastings9cf065c2012-06-22 16:30:09 -070011705#ifdef HAVE_FUTIMENS
11706 "HAVE_FUTIMENS",
11707#endif
11708
11709#ifdef HAVE_FUTIMES
11710 "HAVE_FUTIMES",
11711#endif
11712
11713#ifdef HAVE_FUTIMESAT
11714 "HAVE_FUTIMESAT",
11715#endif
11716
11717#ifdef HAVE_LINKAT
11718 "HAVE_LINKAT",
11719#endif
11720
11721#ifdef HAVE_LCHFLAGS
11722 "HAVE_LCHFLAGS",
11723#endif
11724
11725#ifdef HAVE_LCHMOD
11726 "HAVE_LCHMOD",
11727#endif
11728
11729#ifdef HAVE_LCHOWN
11730 "HAVE_LCHOWN",
11731#endif
11732
11733#ifdef HAVE_LSTAT
11734 "HAVE_LSTAT",
11735#endif
11736
11737#ifdef HAVE_LUTIMES
11738 "HAVE_LUTIMES",
11739#endif
11740
11741#ifdef HAVE_MKDIRAT
11742 "HAVE_MKDIRAT",
11743#endif
11744
11745#ifdef HAVE_MKFIFOAT
11746 "HAVE_MKFIFOAT",
11747#endif
11748
11749#ifdef HAVE_MKNODAT
11750 "HAVE_MKNODAT",
11751#endif
11752
11753#ifdef HAVE_OPENAT
11754 "HAVE_OPENAT",
11755#endif
11756
11757#ifdef HAVE_READLINKAT
11758 "HAVE_READLINKAT",
11759#endif
11760
11761#ifdef HAVE_RENAMEAT
11762 "HAVE_RENAMEAT",
11763#endif
11764
11765#ifdef HAVE_SYMLINKAT
11766 "HAVE_SYMLINKAT",
11767#endif
11768
11769#ifdef HAVE_UNLINKAT
11770 "HAVE_UNLINKAT",
11771#endif
11772
11773#ifdef HAVE_UTIMENSAT
11774 "HAVE_UTIMENSAT",
11775#endif
11776
11777#ifdef MS_WINDOWS
11778 "MS_WINDOWS",
11779#endif
11780
11781 NULL
11782};
11783
11784
Mark Hammondfe51c6d2002-08-02 02:27:13 +000011785PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000011786INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000011787{
Victor Stinner8c62be82010-05-06 00:08:46 +000011788 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011789 PyObject *list;
11790 char **trace;
Tim Peters5aa91602002-01-30 05:46:57 +000011791
Brian Curtin52173d42010-12-02 18:29:18 +000011792#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000011793 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000011794#endif
11795
Victor Stinner8c62be82010-05-06 00:08:46 +000011796 m = PyModule_Create(&posixmodule);
11797 if (m == NULL)
11798 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000011799
Victor Stinner8c62be82010-05-06 00:08:46 +000011800 /* Initialize environ dictionary */
11801 v = convertenviron();
11802 Py_XINCREF(v);
11803 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
11804 return NULL;
11805 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000011806
Victor Stinner8c62be82010-05-06 00:08:46 +000011807 if (all_ins(m))
11808 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000011809
Victor Stinner8c62be82010-05-06 00:08:46 +000011810 if (setup_confname_tables(m))
11811 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000011812
Victor Stinner8c62be82010-05-06 00:08:46 +000011813 Py_INCREF(PyExc_OSError);
11814 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000011815
Guido van Rossumb3d39562000-01-31 18:41:26 +000011816#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000011817 if (posix_putenv_garbage == NULL)
11818 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000011819#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011820
Victor Stinner8c62be82010-05-06 00:08:46 +000011821 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020011822#if defined(HAVE_WAITID) && !defined(__APPLE__)
11823 waitid_result_desc.name = MODNAME ".waitid_result";
11824 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
11825#endif
11826
Victor Stinner8c62be82010-05-06 00:08:46 +000011827 stat_result_desc.name = MODNAME ".stat_result";
11828 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
11829 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
11830 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
11831 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
11832 structseq_new = StatResultType.tp_new;
11833 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011834
Victor Stinner8c62be82010-05-06 00:08:46 +000011835 statvfs_result_desc.name = MODNAME ".statvfs_result";
11836 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011837#ifdef NEED_TICKS_PER_SECOND
11838# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000011839 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011840# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000011841 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011842# else
Victor Stinner8c62be82010-05-06 00:08:46 +000011843 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011844# endif
11845#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011846
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050011847#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011848 sched_param_desc.name = MODNAME ".sched_param";
11849 PyStructSequence_InitType(&SchedParamType, &sched_param_desc);
11850 SchedParamType.tp_new = sched_param_new;
11851#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011852
11853 /* initialize TerminalSize_info */
11854 PyStructSequence_InitType(&TerminalSizeType, &TerminalSize_desc);
Victor Stinner8c62be82010-05-06 00:08:46 +000011855 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020011856#if defined(HAVE_WAITID) && !defined(__APPLE__)
11857 Py_INCREF((PyObject*) &WaitidResultType);
11858 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
11859#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011860 Py_INCREF((PyObject*) &StatResultType);
11861 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
11862 Py_INCREF((PyObject*) &StatVFSResultType);
11863 PyModule_AddObject(m, "statvfs_result",
11864 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011865
11866#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011867 Py_INCREF(&SchedParamType);
11868 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011869#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000011870
Larry Hastings605a62d2012-06-24 04:33:36 -070011871 times_result_desc.name = MODNAME ".times_result";
11872 PyStructSequence_InitType(&TimesResultType, &times_result_desc);
11873 PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType);
11874
11875 uname_result_desc.name = MODNAME ".uname_result";
11876 PyStructSequence_InitType(&UnameResultType, &uname_result_desc);
11877 PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType);
11878
Thomas Wouters477c8d52006-05-27 19:21:47 +000011879#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000011880 /*
11881 * Step 2 of weak-linking support on Mac OS X.
11882 *
11883 * The code below removes functions that are not available on the
11884 * currently active platform.
11885 *
11886 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070011887 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000011888 * OSX 10.4.
11889 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000011890#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011891 if (fstatvfs == NULL) {
11892 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
11893 return NULL;
11894 }
11895 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011896#endif /* HAVE_FSTATVFS */
11897
11898#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011899 if (statvfs == NULL) {
11900 if (PyObject_DelAttrString(m, "statvfs") == -1) {
11901 return NULL;
11902 }
11903 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011904#endif /* HAVE_STATVFS */
11905
11906# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000011907 if (lchown == NULL) {
11908 if (PyObject_DelAttrString(m, "lchown") == -1) {
11909 return NULL;
11910 }
11911 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011912#endif /* HAVE_LCHOWN */
11913
11914
11915#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011916
Antoine Pitrou9d20e0e2012-09-12 18:01:36 +020011917 Py_INCREF(&TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011918 PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType);
11919
Larry Hastings6fe20b32012-04-19 15:07:49 -070011920 billion = PyLong_FromLong(1000000000);
11921 if (!billion)
11922 return NULL;
11923
Larry Hastings9cf065c2012-06-22 16:30:09 -070011924 /* suppress "function not used" warnings */
11925 {
11926 int ignored;
11927 fd_specified("", -1);
11928 follow_symlinks_specified("", 1);
11929 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
11930 dir_fd_converter(Py_None, &ignored);
11931 dir_fd_unavailable(Py_None, &ignored);
11932 }
11933
11934 /*
11935 * provide list of locally available functions
11936 * so os.py can populate support_* lists
11937 */
11938 list = PyList_New(0);
11939 if (!list)
11940 return NULL;
11941 for (trace = have_functions; *trace; trace++) {
11942 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
11943 if (!unicode)
11944 return NULL;
11945 if (PyList_Append(list, unicode))
11946 return NULL;
11947 Py_DECREF(unicode);
11948 }
11949 PyModule_AddObject(m, "_have_functions", list);
11950
11951 initialized = 1;
11952
Victor Stinner8c62be82010-05-06 00:08:46 +000011953 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011954
Guido van Rossumb6775db1994-08-01 11:34:53 +000011955}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011956
11957#ifdef __cplusplus
11958}
11959#endif