blob: 094ceb7a10422c95a340b45e7bdbcf5615491a8e [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 Peterson9428d532011-09-14 11:45:52 -0400110#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__)
111#define USE_XATTRS
112#endif
113
114#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400115#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400116#endif
117
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000118#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
119#ifdef HAVE_SYS_SOCKET_H
120#include <sys/socket.h>
121#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000122#endif
123
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000124/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000125/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000126#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000127#include <process.h>
128#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000129#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000130#define HAVE_GETCWD 1
131#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000132#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000133#if defined(__OS2__)
134#define HAVE_EXECV 1
135#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000136#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000137#include <process.h>
138#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000139#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000140#define HAVE_EXECV 1
141#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000142#define HAVE_OPENDIR 1
143#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000144#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000145#define HAVE_WAIT 1
146#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000147#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000148#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000149#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000150#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000151#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000152#define HAVE_EXECV 1
153#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000154#define HAVE_SYSTEM 1
155#define HAVE_CWAIT 1
156#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000157#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000158#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000159#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
160/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000161#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000162/* Unix functions that the configure script doesn't check for */
163#define HAVE_EXECV 1
164#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000165#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000166#define HAVE_FORK1 1
167#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000168#define HAVE_GETCWD 1
169#define HAVE_GETEGID 1
170#define HAVE_GETEUID 1
171#define HAVE_GETGID 1
172#define HAVE_GETPPID 1
173#define HAVE_GETUID 1
174#define HAVE_KILL 1
175#define HAVE_OPENDIR 1
176#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000177#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000178#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000179#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000180#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000181#endif /* _MSC_VER */
182#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000183#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000184#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000185
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000186#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000187
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000188#if defined(__sgi)&&_COMPILER_VERSION>=700
189/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
190 (default) */
191extern char *ctermid_r(char *);
192#endif
193
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000194#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000195#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000196extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000197#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000198#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000199extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000200#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000201extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000202#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000203#endif
204#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000205extern int chdir(char *);
206extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000207#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000208extern int chdir(const char *);
209extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000210#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000211#ifdef __BORLANDC__
212extern int chmod(const char *, int);
213#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000214extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000215#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000216/*#ifdef HAVE_FCHMOD
217extern int fchmod(int, mode_t);
218#endif*/
219/*#ifdef HAVE_LCHMOD
220extern int lchmod(const char *, mode_t);
221#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000222extern int chown(const char *, uid_t, gid_t);
223extern char *getcwd(char *, int);
224extern char *strerror(int);
225extern int link(const char *, const char *);
226extern int rename(const char *, const char *);
227extern int stat(const char *, struct stat *);
228extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000229#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000230extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000231#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000233extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000234#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000236
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000237#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000238
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239#ifdef HAVE_UTIME_H
240#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000241#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000242
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000243#ifdef HAVE_SYS_UTIME_H
244#include <sys/utime.h>
245#define HAVE_UTIME_H /* pretend we do for the rest of this file */
246#endif /* HAVE_SYS_UTIME_H */
247
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248#ifdef HAVE_SYS_TIMES_H
249#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000250#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251
252#ifdef HAVE_SYS_PARAM_H
253#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000254#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255
256#ifdef HAVE_SYS_UTSNAME_H
257#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000258#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000259
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000260#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000261#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000262#define NAMLEN(dirent) strlen((dirent)->d_name)
263#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000264#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000265#include <direct.h>
266#define NAMLEN(dirent) strlen((dirent)->d_name)
267#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000268#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000269#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000270#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000271#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000273#endif
274#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000275#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000276#endif
277#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000278#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000279#endif
280#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000281
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000282#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000283#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000284#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000285#endif
286#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000287#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000288#endif
289#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000290#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000291#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000292#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000293#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000294#endif
295#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000296#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000297#endif
298#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000299#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000300#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000301#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000302#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000303#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000304#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000305#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000306#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
307#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000308static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000309#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000310#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000311
Guido van Rossumd48f2521997-12-05 22:19:34 +0000312#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000313#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000314#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315
Tim Petersbc2e10e2002-03-03 23:17:02 +0000316#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000317#if defined(PATH_MAX) && PATH_MAX > 1024
318#define MAXPATHLEN PATH_MAX
319#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000320#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000321#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000322#endif /* MAXPATHLEN */
323
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000324#ifdef UNION_WAIT
325/* Emulate some macros on systems that have a union instead of macros */
326
327#ifndef WIFEXITED
328#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
329#endif
330
331#ifndef WEXITSTATUS
332#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
333#endif
334
335#ifndef WTERMSIG
336#define WTERMSIG(u_wait) ((u_wait).w_termsig)
337#endif
338
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000339#define WAIT_TYPE union wait
340#define WAIT_STATUS_INT(s) (s.w_status)
341
342#else /* !UNION_WAIT */
343#define WAIT_TYPE int
344#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000345#endif /* UNION_WAIT */
346
Greg Wardb48bc172000-03-01 21:51:56 +0000347/* Don't use the "_r" form if we don't need it (also, won't have a
348 prototype for it, at least on Solaris -- maybe others as well?). */
349#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
350#define USE_CTERMID_R
351#endif
352
Fred Drake699f3522000-06-29 21:12:41 +0000353/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000354#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000355#undef FSTAT
356#undef STRUCT_STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000357#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000358# define STAT win32_stat
359# define FSTAT win32_fstat
360# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000361#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000362# define STAT stat
363# define FSTAT fstat
364# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000365#endif
366
Tim Peters11b23062003-04-23 02:39:17 +0000367#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000368#include <sys/mkdev.h>
369#else
370#if defined(MAJOR_IN_SYSMACROS)
371#include <sys/sysmacros.h>
372#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000373#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
374#include <sys/mkdev.h>
375#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000376#endif
Fred Drake699f3522000-06-29 21:12:41 +0000377
Ross Lagerwallb1e5d592011-09-19 08:30:43 +0200378/* A helper used by a number of POSIX-only functions */
379#ifndef MS_WINDOWS
Georg Brandla391b112011-02-25 15:23:18 +0000380static int
381_parse_off_t(PyObject* arg, void* addr)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000382{
383#if !defined(HAVE_LARGEFILE_SUPPORT)
384 *((off_t*)addr) = PyLong_AsLong(arg);
385#else
Antoine Pitroudcc20b82011-02-26 13:38:35 +0000386 *((off_t*)addr) = PyLong_AsLongLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000387#endif
388 if (PyErr_Occurred())
389 return 0;
390 return 1;
391}
Ross Lagerwallb1e5d592011-09-19 08:30:43 +0200392#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000393
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000394#if defined _MSC_VER && _MSC_VER >= 1400
395/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
396 * valid and throw an assertion if it isn't.
397 * Normally, an invalid fd is likely to be a C program error and therefore
398 * an assertion can be useful, but it does contradict the POSIX standard
399 * which for write(2) states:
400 * "Otherwise, -1 shall be returned and errno set to indicate the error."
401 * "[EBADF] The fildes argument is not a valid file descriptor open for
402 * writing."
403 * Furthermore, python allows the user to enter any old integer
404 * as a fd and should merely raise a python exception on error.
405 * The Microsoft CRT doesn't provide an official way to check for the
406 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000407 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000408 * internal structures involved.
409 * The structures below must be updated for each version of visual studio
410 * according to the file internal.h in the CRT source, until MS comes
411 * up with a less hacky way to do this.
412 * (all of this is to avoid globally modifying the CRT behaviour using
413 * _set_invalid_parameter_handler() and _CrtSetReportMode())
414 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000415/* The actual size of the structure is determined at runtime.
416 * Only the first items must be present.
417 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000418typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000419 intptr_t osfhnd;
420 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000421} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000422
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000423extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000424#define IOINFO_L2E 5
425#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
426#define IOINFO_ARRAYS 64
427#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
428#define FOPEN 0x01
429#define _NO_CONSOLE_FILENO (intptr_t)-2
430
431/* This function emulates what the windows CRT does to validate file handles */
432int
433_PyVerify_fd(int fd)
434{
Victor Stinner8c62be82010-05-06 00:08:46 +0000435 const int i1 = fd >> IOINFO_L2E;
436 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000437
Antoine Pitrou22e41552010-08-15 18:07:50 +0000438 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000439
Victor Stinner8c62be82010-05-06 00:08:46 +0000440 /* Determine the actual size of the ioinfo structure,
441 * as used by the CRT loaded in memory
442 */
443 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
444 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
445 }
446 if (sizeof_ioinfo == 0) {
447 /* This should not happen... */
448 goto fail;
449 }
450
451 /* See that it isn't a special CLEAR fileno */
452 if (fd != _NO_CONSOLE_FILENO) {
453 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
454 * we check pointer validity and other info
455 */
456 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
457 /* finally, check that the file is open */
458 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
459 if (info->osfile & FOPEN) {
460 return 1;
461 }
462 }
463 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000464 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000465 errno = EBADF;
466 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000467}
468
469/* the special case of checking dup2. The target fd must be in a sensible range */
470static int
471_PyVerify_fd_dup2(int fd1, int fd2)
472{
Victor Stinner8c62be82010-05-06 00:08:46 +0000473 if (!_PyVerify_fd(fd1))
474 return 0;
475 if (fd2 == _NO_CONSOLE_FILENO)
476 return 0;
477 if ((unsigned)fd2 < _NHANDLE_)
478 return 1;
479 else
480 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000481}
482#else
483/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
484#define _PyVerify_fd_dup2(A, B) (1)
485#endif
486
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000487#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000488/* The following structure was copied from
489 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
490 include doesn't seem to be present in the Windows SDK (at least as included
491 with Visual Studio Express). */
492typedef struct _REPARSE_DATA_BUFFER {
493 ULONG ReparseTag;
494 USHORT ReparseDataLength;
495 USHORT Reserved;
496 union {
497 struct {
498 USHORT SubstituteNameOffset;
499 USHORT SubstituteNameLength;
500 USHORT PrintNameOffset;
501 USHORT PrintNameLength;
502 ULONG Flags;
503 WCHAR PathBuffer[1];
504 } SymbolicLinkReparseBuffer;
505
506 struct {
507 USHORT SubstituteNameOffset;
508 USHORT SubstituteNameLength;
509 USHORT PrintNameOffset;
510 USHORT PrintNameLength;
511 WCHAR PathBuffer[1];
512 } MountPointReparseBuffer;
513
514 struct {
515 UCHAR DataBuffer[1];
516 } GenericReparseBuffer;
517 };
518} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
519
520#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
521 GenericReparseBuffer)
522#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
523
524static int
Brian Curtind25aef52011-06-13 15:16:04 -0500525win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000526{
527 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
528 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
529 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000530
531 if (0 == DeviceIoControl(
532 reparse_point_handle,
533 FSCTL_GET_REPARSE_POINT,
534 NULL, 0, /* in buffer */
535 target_buffer, sizeof(target_buffer),
536 &n_bytes_returned,
537 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -0500538 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000539
540 if (reparse_tag)
541 *reparse_tag = rdb->ReparseTag;
542
Brian Curtind25aef52011-06-13 15:16:04 -0500543 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000544}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000545#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000546
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000547/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000548#ifdef WITH_NEXT_FRAMEWORK
549/* On Darwin/MacOSX a shared library or framework has no access to
550** environ directly, we must obtain it with _NSGetEnviron().
551*/
552#include <crt_externs.h>
553static char **environ;
554#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000555extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000556#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000557
Barry Warsaw53699e91996-12-10 23:23:01 +0000558static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000559convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000560{
Victor Stinner8c62be82010-05-06 00:08:46 +0000561 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000562#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000563 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000564#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000565 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000566#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000567#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000568 APIRET rc;
569 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
570#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000571
Victor Stinner8c62be82010-05-06 00:08:46 +0000572 d = PyDict_New();
573 if (d == NULL)
574 return NULL;
575#ifdef WITH_NEXT_FRAMEWORK
576 if (environ == NULL)
577 environ = *_NSGetEnviron();
578#endif
579#ifdef MS_WINDOWS
580 /* _wenviron must be initialized in this way if the program is started
581 through main() instead of wmain(). */
582 _wgetenv(L"");
583 if (_wenviron == NULL)
584 return d;
585 /* This part ignores errors */
586 for (e = _wenviron; *e != NULL; e++) {
587 PyObject *k;
588 PyObject *v;
589 wchar_t *p = wcschr(*e, L'=');
590 if (p == NULL)
591 continue;
592 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
593 if (k == NULL) {
594 PyErr_Clear();
595 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000596 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000597 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
598 if (v == NULL) {
599 PyErr_Clear();
600 Py_DECREF(k);
601 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000602 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000603 if (PyDict_GetItem(d, k) == NULL) {
604 if (PyDict_SetItem(d, k, v) != 0)
605 PyErr_Clear();
606 }
607 Py_DECREF(k);
608 Py_DECREF(v);
609 }
610#else
611 if (environ == NULL)
612 return d;
613 /* This part ignores errors */
614 for (e = environ; *e != NULL; e++) {
615 PyObject *k;
616 PyObject *v;
617 char *p = strchr(*e, '=');
618 if (p == NULL)
619 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000620 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000621 if (k == NULL) {
622 PyErr_Clear();
623 continue;
624 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000625 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000626 if (v == NULL) {
627 PyErr_Clear();
628 Py_DECREF(k);
629 continue;
630 }
631 if (PyDict_GetItem(d, k) == NULL) {
632 if (PyDict_SetItem(d, k, v) != 0)
633 PyErr_Clear();
634 }
635 Py_DECREF(k);
636 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000637 }
638#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000639#if defined(PYOS_OS2)
640 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
641 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
642 PyObject *v = PyBytes_FromString(buffer);
643 PyDict_SetItemString(d, "BEGINLIBPATH", v);
644 Py_DECREF(v);
645 }
646 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
647 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
648 PyObject *v = PyBytes_FromString(buffer);
649 PyDict_SetItemString(d, "ENDLIBPATH", v);
650 Py_DECREF(v);
651 }
652#endif
653 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000654}
655
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000656/* Set a POSIX-specific error from errno, and return NULL */
657
Barry Warsawd58d7641998-07-23 16:14:40 +0000658static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000659posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000660{
Victor Stinner8c62be82010-05-06 00:08:46 +0000661 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000662}
Barry Warsawd58d7641998-07-23 16:14:40 +0000663static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000664posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000665{
Victor Stinner8c62be82010-05-06 00:08:46 +0000666 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000667}
668
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000669
Mark Hammondef8b6542001-05-13 08:04:26 +0000670static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000671posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000672{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000673 PyObject *name_str, *rc;
674 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
675 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000676 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000677 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
678 name_str);
679 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000680 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000681}
682
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000683#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000684static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000685win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000686{
Victor Stinner8c62be82010-05-06 00:08:46 +0000687 /* XXX We should pass the function name along in the future.
688 (winreg.c also wants to pass the function name.)
689 This would however require an additional param to the
690 Windows error object, which is non-trivial.
691 */
692 errno = GetLastError();
693 if (filename)
694 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
695 else
696 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000697}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000698
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000699static PyObject *
Victor Stinnereb5657a2011-09-30 01:44:27 +0200700win32_error_unicode(char* function, wchar_t* filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000701{
Victor Stinner8c62be82010-05-06 00:08:46 +0000702 /* XXX - see win32_error for comments on 'function' */
703 errno = GetLastError();
704 if (filename)
705 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
706 else
707 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000708}
709
Victor Stinnereb5657a2011-09-30 01:44:27 +0200710static PyObject *
711win32_error_object(char* function, PyObject* filename)
712{
713 /* XXX - see win32_error for comments on 'function' */
714 errno = GetLastError();
715 if (filename)
716 return PyErr_SetExcFromWindowsErrWithFilenameObject(
717 PyExc_WindowsError,
718 errno,
719 filename);
720 else
721 return PyErr_SetFromWindowsErr(errno);
722}
723
Thomas Wouters477c8d52006-05-27 19:21:47 +0000724static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000725convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000726{
Victor Stinner8c62be82010-05-06 00:08:46 +0000727 if (PyUnicode_CheckExact(*param))
728 Py_INCREF(*param);
729 else if (PyUnicode_Check(*param))
730 /* For a Unicode subtype that's not a Unicode object,
731 return a true Unicode object with the same data. */
732 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
733 PyUnicode_GET_SIZE(*param));
734 else
735 *param = PyUnicode_FromEncodedObject(*param,
736 Py_FileSystemDefaultEncoding,
737 "strict");
738 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000739}
740
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000741#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000742
Guido van Rossumd48f2521997-12-05 22:19:34 +0000743#if defined(PYOS_OS2)
744/**********************************************************************
745 * Helper Function to Trim and Format OS/2 Messages
746 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000747static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000748os2_formatmsg(char *msgbuf, int msglen, char *reason)
749{
750 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
751
752 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
753 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
754
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000755 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000756 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
757 }
758
759 /* Add Optional Reason Text */
760 if (reason) {
761 strcat(msgbuf, " : ");
762 strcat(msgbuf, reason);
763 }
764}
765
766/**********************************************************************
767 * Decode an OS/2 Operating System Error Code
768 *
769 * A convenience function to lookup an OS/2 error code and return a
770 * text message we can use to raise a Python exception.
771 *
772 * Notes:
773 * The messages for errors returned from the OS/2 kernel reside in
774 * the file OSO001.MSG in the \OS2 directory hierarchy.
775 *
776 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000777static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000778os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
779{
780 APIRET rc;
781 ULONG msglen;
782
783 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
784 Py_BEGIN_ALLOW_THREADS
785 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
786 errorcode, "oso001.msg", &msglen);
787 Py_END_ALLOW_THREADS
788
789 if (rc == NO_ERROR)
790 os2_formatmsg(msgbuf, msglen, reason);
791 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000792 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000793 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000794
795 return msgbuf;
796}
797
798/* Set an OS/2-specific error and return NULL. OS/2 kernel
799 errors are not in a global variable e.g. 'errno' nor are
800 they congruent with posix error numbers. */
801
Victor Stinner8c62be82010-05-06 00:08:46 +0000802static PyObject *
803os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000804{
805 char text[1024];
806 PyObject *v;
807
808 os2_strerror(text, sizeof(text), code, "");
809
810 v = Py_BuildValue("(is)", code, text);
811 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000812 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000813 Py_DECREF(v);
814 }
815 return NULL; /* Signal to Python that an Exception is Pending */
816}
817
818#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000819
820/* POSIX generic methods */
821
Barry Warsaw53699e91996-12-10 23:23:01 +0000822static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000823posix_fildes(PyObject *fdobj, int (*func)(int))
824{
Victor Stinner8c62be82010-05-06 00:08:46 +0000825 int fd;
826 int res;
827 fd = PyObject_AsFileDescriptor(fdobj);
828 if (fd < 0)
829 return NULL;
830 if (!_PyVerify_fd(fd))
831 return posix_error();
832 Py_BEGIN_ALLOW_THREADS
833 res = (*func)(fd);
834 Py_END_ALLOW_THREADS
835 if (res < 0)
836 return posix_error();
837 Py_INCREF(Py_None);
838 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000839}
Guido van Rossum21142a01999-01-08 21:05:37 +0000840
841static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000842posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000843{
Victor Stinner8c62be82010-05-06 00:08:46 +0000844 PyObject *opath1 = NULL;
845 char *path1;
846 int res;
847 if (!PyArg_ParseTuple(args, format,
848 PyUnicode_FSConverter, &opath1))
849 return NULL;
850 path1 = PyBytes_AsString(opath1);
851 Py_BEGIN_ALLOW_THREADS
852 res = (*func)(path1);
853 Py_END_ALLOW_THREADS
854 if (res < 0)
855 return posix_error_with_allocated_filename(opath1);
856 Py_DECREF(opath1);
857 Py_INCREF(Py_None);
858 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000859}
860
Barry Warsaw53699e91996-12-10 23:23:01 +0000861static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000862posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000863 char *format,
864 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000865{
Victor Stinner8c62be82010-05-06 00:08:46 +0000866 PyObject *opath1 = NULL, *opath2 = NULL;
867 char *path1, *path2;
868 int res;
869 if (!PyArg_ParseTuple(args, format,
870 PyUnicode_FSConverter, &opath1,
871 PyUnicode_FSConverter, &opath2)) {
872 return NULL;
873 }
874 path1 = PyBytes_AsString(opath1);
875 path2 = PyBytes_AsString(opath2);
876 Py_BEGIN_ALLOW_THREADS
877 res = (*func)(path1, path2);
878 Py_END_ALLOW_THREADS
879 Py_DECREF(opath1);
880 Py_DECREF(opath2);
881 if (res != 0)
882 /* XXX how to report both path1 and path2??? */
883 return posix_error();
884 Py_INCREF(Py_None);
885 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000886}
887
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000888#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000889static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000890win32_1str(PyObject* args, char* func,
891 char* format, BOOL (__stdcall *funcA)(LPCSTR),
892 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000893{
Victor Stinner8c62be82010-05-06 00:08:46 +0000894 PyObject *uni;
895 char *ansi;
896 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000897
Victor Stinnereb5657a2011-09-30 01:44:27 +0200898 if (PyArg_ParseTuple(args, wformat, &uni))
899 {
900 wchar_t *wstr = PyUnicode_AsUnicode(uni);
901 if (wstr == NULL)
902 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +0000903 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +0200904 result = funcW(wstr);
Victor Stinner8c62be82010-05-06 00:08:46 +0000905 Py_END_ALLOW_THREADS
906 if (!result)
Victor Stinnereb5657a2011-09-30 01:44:27 +0200907 return win32_error_object(func, uni);
Victor Stinner8c62be82010-05-06 00:08:46 +0000908 Py_INCREF(Py_None);
909 return Py_None;
910 }
Victor Stinnereb5657a2011-09-30 01:44:27 +0200911 PyErr_Clear();
912
Victor Stinner8c62be82010-05-06 00:08:46 +0000913 if (!PyArg_ParseTuple(args, format, &ansi))
914 return NULL;
915 Py_BEGIN_ALLOW_THREADS
916 result = funcA(ansi);
917 Py_END_ALLOW_THREADS
918 if (!result)
919 return win32_error(func, ansi);
920 Py_INCREF(Py_None);
921 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000922
923}
924
925/* This is a reimplementation of the C library's chdir function,
926 but one that produces Win32 errors instead of DOS error codes.
927 chdir is essentially a wrapper around SetCurrentDirectory; however,
928 it also needs to set "magic" environment variables indicating
929 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000930static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000931win32_chdir(LPCSTR path)
932{
Victor Stinner8c62be82010-05-06 00:08:46 +0000933 char new_path[MAX_PATH+1];
934 int result;
935 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000936
Victor Stinner8c62be82010-05-06 00:08:46 +0000937 if(!SetCurrentDirectoryA(path))
938 return FALSE;
939 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
940 if (!result)
941 return FALSE;
942 /* In the ANSI API, there should not be any paths longer
943 than MAX_PATH. */
944 assert(result <= MAX_PATH+1);
945 if (strncmp(new_path, "\\\\", 2) == 0 ||
946 strncmp(new_path, "//", 2) == 0)
947 /* UNC path, nothing to do. */
948 return TRUE;
949 env[1] = new_path[0];
950 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000951}
952
953/* The Unicode version differs from the ANSI version
954 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000955static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000956win32_wchdir(LPCWSTR path)
957{
Victor Stinner8c62be82010-05-06 00:08:46 +0000958 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
959 int result;
960 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000961
Victor Stinner8c62be82010-05-06 00:08:46 +0000962 if(!SetCurrentDirectoryW(path))
963 return FALSE;
964 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
965 if (!result)
966 return FALSE;
967 if (result > MAX_PATH+1) {
968 new_path = malloc(result * sizeof(wchar_t));
969 if (!new_path) {
970 SetLastError(ERROR_OUTOFMEMORY);
971 return FALSE;
972 }
973 result = GetCurrentDirectoryW(result, new_path);
974 if (!result) {
975 free(new_path);
976 return FALSE;
977 }
978 }
979 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
980 wcsncmp(new_path, L"//", 2) == 0)
981 /* UNC path, nothing to do. */
982 return TRUE;
983 env[1] = new_path[0];
984 result = SetEnvironmentVariableW(env, new_path);
985 if (new_path != _new_path)
986 free(new_path);
987 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000988}
989#endif
990
Martin v. Löwis14694662006-02-03 12:54:16 +0000991#ifdef MS_WINDOWS
992/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
993 - time stamps are restricted to second resolution
994 - file modification times suffer from forth-and-back conversions between
995 UTC and local time
996 Therefore, we implement our own stat, based on the Win32 API directly.
997*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000998#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000999
1000struct win32_stat{
1001 int st_dev;
1002 __int64 st_ino;
1003 unsigned short st_mode;
1004 int st_nlink;
1005 int st_uid;
1006 int st_gid;
1007 int st_rdev;
1008 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001009 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001010 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001011 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001012 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001013 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001014 int st_ctime_nsec;
1015};
1016
1017static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
1018
1019static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001020FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +00001021{
Victor Stinner8c62be82010-05-06 00:08:46 +00001022 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
1023 /* Cannot simply cast and dereference in_ptr,
1024 since it might not be aligned properly */
1025 __int64 in;
1026 memcpy(&in, in_ptr, sizeof(in));
1027 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001028 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001029}
1030
Thomas Wouters477c8d52006-05-27 19:21:47 +00001031static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001032time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001033{
Victor Stinner8c62be82010-05-06 00:08:46 +00001034 /* XXX endianness */
1035 __int64 out;
1036 out = time_in + secs_between_epochs;
1037 out = out * 10000000 + nsec_in / 100;
1038 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001039}
1040
Martin v. Löwis14694662006-02-03 12:54:16 +00001041/* Below, we *know* that ugo+r is 0444 */
1042#if _S_IREAD != 0400
1043#error Unsupported C library
1044#endif
1045static int
1046attributes_to_mode(DWORD attr)
1047{
Victor Stinner8c62be82010-05-06 00:08:46 +00001048 int m = 0;
1049 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1050 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1051 else
1052 m |= _S_IFREG;
1053 if (attr & FILE_ATTRIBUTE_READONLY)
1054 m |= 0444;
1055 else
1056 m |= 0666;
1057 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001058}
1059
1060static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001061attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001062{
Victor Stinner8c62be82010-05-06 00:08:46 +00001063 memset(result, 0, sizeof(*result));
1064 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1065 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1066 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1067 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1068 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001069 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001070 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001071 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1072 /* first clear the S_IFMT bits */
1073 result->st_mode ^= (result->st_mode & 0170000);
1074 /* now set the bits that make this a symlink */
1075 result->st_mode |= 0120000;
1076 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001077
Victor Stinner8c62be82010-05-06 00:08:46 +00001078 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001079}
1080
Guido van Rossumd8faa362007-04-27 19:54:29 +00001081static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001082attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001083{
Victor Stinner8c62be82010-05-06 00:08:46 +00001084 HANDLE hFindFile;
1085 WIN32_FIND_DATAA FileData;
1086 hFindFile = FindFirstFileA(pszFile, &FileData);
1087 if (hFindFile == INVALID_HANDLE_VALUE)
1088 return FALSE;
1089 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001090 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001091 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001092 info->dwFileAttributes = FileData.dwFileAttributes;
1093 info->ftCreationTime = FileData.ftCreationTime;
1094 info->ftLastAccessTime = FileData.ftLastAccessTime;
1095 info->ftLastWriteTime = FileData.ftLastWriteTime;
1096 info->nFileSizeHigh = FileData.nFileSizeHigh;
1097 info->nFileSizeLow = FileData.nFileSizeLow;
1098/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001099 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1100 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001101 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001102}
1103
1104static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001105attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001106{
Victor Stinner8c62be82010-05-06 00:08:46 +00001107 HANDLE hFindFile;
1108 WIN32_FIND_DATAW FileData;
1109 hFindFile = FindFirstFileW(pszFile, &FileData);
1110 if (hFindFile == INVALID_HANDLE_VALUE)
1111 return FALSE;
1112 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001113 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001114 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001115 info->dwFileAttributes = FileData.dwFileAttributes;
1116 info->ftCreationTime = FileData.ftCreationTime;
1117 info->ftLastAccessTime = FileData.ftLastAccessTime;
1118 info->ftLastWriteTime = FileData.ftLastWriteTime;
1119 info->nFileSizeHigh = FileData.nFileSizeHigh;
1120 info->nFileSizeLow = FileData.nFileSizeLow;
1121/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001122 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1123 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001124 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001125}
1126
Brian Curtind25aef52011-06-13 15:16:04 -05001127/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
1128static int has_GetFinalPathNameByHandle = 0;
1129static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1130 DWORD);
1131static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1132 DWORD);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001133static int
Brian Curtind25aef52011-06-13 15:16:04 -05001134check_GetFinalPathNameByHandle()
Brian Curtinf5e76d02010-11-24 13:14:05 +00001135{
Brian Curtind25aef52011-06-13 15:16:04 -05001136 HINSTANCE hKernel32;
1137 /* only recheck */
1138 if (!has_GetFinalPathNameByHandle)
1139 {
1140 hKernel32 = GetModuleHandle("KERNEL32");
1141 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1142 "GetFinalPathNameByHandleA");
1143 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1144 "GetFinalPathNameByHandleW");
1145 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1146 Py_GetFinalPathNameByHandleW;
1147 }
1148 return has_GetFinalPathNameByHandle;
1149}
1150
1151static BOOL
1152get_target_path(HANDLE hdl, wchar_t **target_path)
1153{
1154 int buf_size, result_length;
1155 wchar_t *buf;
1156
1157 /* We have a good handle to the target, use it to determine
1158 the target path name (then we'll call lstat on it). */
1159 buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0,
1160 VOLUME_NAME_DOS);
1161 if(!buf_size)
1162 return FALSE;
1163
1164 buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001165 if (!buf) {
1166 SetLastError(ERROR_OUTOFMEMORY);
1167 return FALSE;
1168 }
1169
Brian Curtind25aef52011-06-13 15:16:04 -05001170 result_length = Py_GetFinalPathNameByHandleW(hdl,
1171 buf, buf_size, VOLUME_NAME_DOS);
1172
1173 if(!result_length) {
1174 free(buf);
1175 return FALSE;
1176 }
1177
1178 if(!CloseHandle(hdl)) {
1179 free(buf);
1180 return FALSE;
1181 }
1182
1183 buf[result_length] = 0;
1184
1185 *target_path = buf;
1186 return TRUE;
1187}
1188
1189static int
1190win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1191 BOOL traverse);
1192static int
1193win32_xstat_impl(const char *path, struct win32_stat *result,
1194 BOOL traverse)
1195{
Victor Stinner26de69d2011-06-17 15:15:38 +02001196 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001197 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001198 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001199 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001200 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001201 const char *dot;
1202
Brian Curtind25aef52011-06-13 15:16:04 -05001203 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001204 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1205 traverse reparse point. */
1206 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001207 }
1208
Brian Curtinf5e76d02010-11-24 13:14:05 +00001209 hFile = CreateFileA(
1210 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001211 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001212 0, /* share mode */
1213 NULL, /* security attributes */
1214 OPEN_EXISTING,
1215 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001216 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1217 Because of this, calls like GetFinalPathNameByHandle will return
1218 the symlink path agin and not the actual final path. */
1219 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1220 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001221 NULL);
1222
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001223 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001224 /* Either the target doesn't exist, or we don't have access to
1225 get a handle to it. If the former, we need to return an error.
1226 If the latter, we can use attributes_from_dir. */
1227 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001228 return -1;
1229 /* Could not get attributes on open file. Fall back to
1230 reading the directory. */
1231 if (!attributes_from_dir(path, &info, &reparse_tag))
1232 /* Very strange. This should not fail now */
1233 return -1;
1234 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1235 if (traverse) {
1236 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001237 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001238 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001239 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001240 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001241 } else {
1242 if (!GetFileInformationByHandle(hFile, &info)) {
1243 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001244 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001245 }
1246 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001247 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1248 return -1;
1249
1250 /* Close the outer open file handle now that we're about to
1251 reopen it with different flags. */
1252 if (!CloseHandle(hFile))
1253 return -1;
1254
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001255 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001256 /* In order to call GetFinalPathNameByHandle we need to open
1257 the file without the reparse handling flag set. */
1258 hFile2 = CreateFileA(
1259 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1260 NULL, OPEN_EXISTING,
1261 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1262 NULL);
1263 if (hFile2 == INVALID_HANDLE_VALUE)
1264 return -1;
1265
1266 if (!get_target_path(hFile2, &target_path))
1267 return -1;
1268
1269 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001270 free(target_path);
1271 return code;
1272 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001273 } else
1274 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001275 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001276 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001277
1278 /* Set S_IEXEC if it is an .exe, .bat, ... */
1279 dot = strrchr(path, '.');
1280 if (dot) {
1281 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1282 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1283 result->st_mode |= 0111;
1284 }
1285 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001286}
1287
1288static int
Brian Curtind25aef52011-06-13 15:16:04 -05001289win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1290 BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001291{
1292 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001293 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001294 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001295 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001296 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001297 const wchar_t *dot;
1298
Brian Curtind25aef52011-06-13 15:16:04 -05001299 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001300 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1301 traverse reparse point. */
1302 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001303 }
1304
Brian Curtinf5e76d02010-11-24 13:14:05 +00001305 hFile = CreateFileW(
1306 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001307 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001308 0, /* share mode */
1309 NULL, /* security attributes */
1310 OPEN_EXISTING,
1311 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001312 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1313 Because of this, calls like GetFinalPathNameByHandle will return
1314 the symlink path agin and not the actual final path. */
Victor Stinner26de69d2011-06-17 15:15:38 +02001315 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
Brian Curtind25aef52011-06-13 15:16:04 -05001316 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001317 NULL);
1318
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001319 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001320 /* Either the target doesn't exist, or we don't have access to
1321 get a handle to it. If the former, we need to return an error.
1322 If the latter, we can use attributes_from_dir. */
1323 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001324 return -1;
1325 /* Could not get attributes on open file. Fall back to
1326 reading the directory. */
1327 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1328 /* Very strange. This should not fail now */
1329 return -1;
1330 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1331 if (traverse) {
1332 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001333 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001334 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001335 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001336 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001337 } else {
1338 if (!GetFileInformationByHandle(hFile, &info)) {
1339 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001340 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001341 }
1342 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001343 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1344 return -1;
1345
1346 /* Close the outer open file handle now that we're about to
1347 reopen it with different flags. */
1348 if (!CloseHandle(hFile))
1349 return -1;
1350
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001351 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001352 /* In order to call GetFinalPathNameByHandle we need to open
1353 the file without the reparse handling flag set. */
1354 hFile2 = CreateFileW(
1355 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1356 NULL, OPEN_EXISTING,
1357 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1358 NULL);
1359 if (hFile2 == INVALID_HANDLE_VALUE)
1360 return -1;
1361
1362 if (!get_target_path(hFile2, &target_path))
1363 return -1;
1364
1365 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001366 free(target_path);
1367 return code;
1368 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001369 } else
1370 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001371 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001372 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001373
1374 /* Set S_IEXEC if it is an .exe, .bat, ... */
1375 dot = wcsrchr(path, '.');
1376 if (dot) {
1377 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1378 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1379 result->st_mode |= 0111;
1380 }
1381 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001382}
1383
1384static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001385win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001386{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001387 /* Protocol violation: we explicitly clear errno, instead of
1388 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001389 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001390 errno = 0;
1391 return code;
1392}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001393
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001394static int
1395win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1396{
1397 /* Protocol violation: we explicitly clear errno, instead of
1398 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001399 int code = win32_xstat_impl_w(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001400 errno = 0;
1401 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001402}
Brian Curtind25aef52011-06-13 15:16:04 -05001403/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001404
1405 In Posix, stat automatically traverses symlinks and returns the stat
1406 structure for the target. In Windows, the equivalent GetFileAttributes by
1407 default does not traverse symlinks and instead returns attributes for
1408 the symlink.
1409
1410 Therefore, win32_lstat will get the attributes traditionally, and
1411 win32_stat will first explicitly resolve the symlink target and then will
1412 call win32_lstat on that result.
1413
Ezio Melotti4969f702011-03-15 05:59:46 +02001414 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001415
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001416static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001417win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001418{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001419 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001420}
1421
Victor Stinner8c62be82010-05-06 00:08:46 +00001422static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001423win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001424{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001425 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001426}
1427
1428static int
1429win32_stat(const char* path, struct win32_stat *result)
1430{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001431 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001432}
1433
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001434static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001435win32_stat_w(const wchar_t* path, struct win32_stat *result)
1436{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001437 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001438}
1439
1440static int
1441win32_fstat(int file_number, struct win32_stat *result)
1442{
Victor Stinner8c62be82010-05-06 00:08:46 +00001443 BY_HANDLE_FILE_INFORMATION info;
1444 HANDLE h;
1445 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001446
Victor Stinner8c62be82010-05-06 00:08:46 +00001447 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001448
Victor Stinner8c62be82010-05-06 00:08:46 +00001449 /* Protocol violation: we explicitly clear errno, instead of
1450 setting it to a POSIX error. Callers should use GetLastError. */
1451 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001452
Victor Stinner8c62be82010-05-06 00:08:46 +00001453 if (h == INVALID_HANDLE_VALUE) {
1454 /* This is really a C library error (invalid file handle).
1455 We set the Win32 error to the closes one matching. */
1456 SetLastError(ERROR_INVALID_HANDLE);
1457 return -1;
1458 }
1459 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001460
Victor Stinner8c62be82010-05-06 00:08:46 +00001461 type = GetFileType(h);
1462 if (type == FILE_TYPE_UNKNOWN) {
1463 DWORD error = GetLastError();
1464 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001465 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001466 }
1467 /* else: valid but unknown file */
1468 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001469
Victor Stinner8c62be82010-05-06 00:08:46 +00001470 if (type != FILE_TYPE_DISK) {
1471 if (type == FILE_TYPE_CHAR)
1472 result->st_mode = _S_IFCHR;
1473 else if (type == FILE_TYPE_PIPE)
1474 result->st_mode = _S_IFIFO;
1475 return 0;
1476 }
1477
1478 if (!GetFileInformationByHandle(h, &info)) {
1479 return -1;
1480 }
1481
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001482 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001483 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001484 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1485 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001486}
1487
1488#endif /* MS_WINDOWS */
1489
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001490PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001491"stat_result: Result from stat or lstat.\n\n\
1492This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001493 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001494or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1495\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001496Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1497or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001498\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001499See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001500
1501static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001502 {"st_mode", "protection bits"},
1503 {"st_ino", "inode"},
1504 {"st_dev", "device"},
1505 {"st_nlink", "number of hard links"},
1506 {"st_uid", "user ID of owner"},
1507 {"st_gid", "group ID of owner"},
1508 {"st_size", "total size, in bytes"},
1509 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1510 {NULL, "integer time of last access"},
1511 {NULL, "integer time of last modification"},
1512 {NULL, "integer time of last change"},
1513 {"st_atime", "time of last access"},
1514 {"st_mtime", "time of last modification"},
1515 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001516#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001517 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001518#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001519#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001520 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001521#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001522#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001523 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001524#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001525#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001526 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001527#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001528#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001529 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001530#endif
1531#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001532 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001533#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001534 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001535};
1536
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001537#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001538#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001539#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001540#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001541#endif
1542
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001543#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001544#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1545#else
1546#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1547#endif
1548
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001549#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001550#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1551#else
1552#define ST_RDEV_IDX ST_BLOCKS_IDX
1553#endif
1554
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001555#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1556#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1557#else
1558#define ST_FLAGS_IDX ST_RDEV_IDX
1559#endif
1560
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001561#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001562#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001563#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001564#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001565#endif
1566
1567#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1568#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1569#else
1570#define ST_BIRTHTIME_IDX ST_GEN_IDX
1571#endif
1572
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001573static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001574 "stat_result", /* name */
1575 stat_result__doc__, /* doc */
1576 stat_result_fields,
1577 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001578};
1579
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001580PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001581"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1582This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001583 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001584or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001585\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001586See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001587
1588static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001589 {"f_bsize", },
1590 {"f_frsize", },
1591 {"f_blocks", },
1592 {"f_bfree", },
1593 {"f_bavail", },
1594 {"f_files", },
1595 {"f_ffree", },
1596 {"f_favail", },
1597 {"f_flag", },
1598 {"f_namemax",},
1599 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001600};
1601
1602static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001603 "statvfs_result", /* name */
1604 statvfs_result__doc__, /* doc */
1605 statvfs_result_fields,
1606 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001607};
1608
Ross Lagerwall7807c352011-03-17 20:20:30 +02001609#if defined(HAVE_WAITID) && !defined(__APPLE__)
1610PyDoc_STRVAR(waitid_result__doc__,
1611"waitid_result: Result from waitid.\n\n\
1612This object may be accessed either as a tuple of\n\
1613 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1614or via the attributes si_pid, si_uid, and so on.\n\
1615\n\
1616See os.waitid for more information.");
1617
1618static PyStructSequence_Field waitid_result_fields[] = {
1619 {"si_pid", },
1620 {"si_uid", },
1621 {"si_signo", },
1622 {"si_status", },
1623 {"si_code", },
1624 {0}
1625};
1626
1627static PyStructSequence_Desc waitid_result_desc = {
1628 "waitid_result", /* name */
1629 waitid_result__doc__, /* doc */
1630 waitid_result_fields,
1631 5
1632};
1633static PyTypeObject WaitidResultType;
1634#endif
1635
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001636static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001637static PyTypeObject StatResultType;
1638static PyTypeObject StatVFSResultType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001639#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001640static PyTypeObject SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001641#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001642static newfunc structseq_new;
1643
1644static PyObject *
1645statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1646{
Victor Stinner8c62be82010-05-06 00:08:46 +00001647 PyStructSequence *result;
1648 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001649
Victor Stinner8c62be82010-05-06 00:08:46 +00001650 result = (PyStructSequence*)structseq_new(type, args, kwds);
1651 if (!result)
1652 return NULL;
1653 /* If we have been initialized from a tuple,
1654 st_?time might be set to None. Initialize it
1655 from the int slots. */
1656 for (i = 7; i <= 9; i++) {
1657 if (result->ob_item[i+3] == Py_None) {
1658 Py_DECREF(Py_None);
1659 Py_INCREF(result->ob_item[i]);
1660 result->ob_item[i+3] = result->ob_item[i];
1661 }
1662 }
1663 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001664}
1665
1666
1667
1668/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001669static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001670
1671PyDoc_STRVAR(stat_float_times__doc__,
1672"stat_float_times([newval]) -> oldval\n\n\
1673Determine whether os.[lf]stat represents time stamps as float objects.\n\
1674If newval is True, future calls to stat() return floats, if it is False,\n\
1675future calls return ints. \n\
1676If newval is omitted, return the current setting.\n");
1677
1678static PyObject*
1679stat_float_times(PyObject* self, PyObject *args)
1680{
Victor Stinner8c62be82010-05-06 00:08:46 +00001681 int newval = -1;
1682 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1683 return NULL;
1684 if (newval == -1)
1685 /* Return old value */
1686 return PyBool_FromLong(_stat_float_times);
1687 _stat_float_times = newval;
1688 Py_INCREF(Py_None);
1689 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001690}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001691
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001692static void
1693fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1694{
Victor Stinner8c62be82010-05-06 00:08:46 +00001695 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001696#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001697 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001698#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001699 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001700#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001701 if (!ival)
1702 return;
1703 if (_stat_float_times) {
1704 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1705 } else {
1706 fval = ival;
1707 Py_INCREF(fval);
1708 }
1709 PyStructSequence_SET_ITEM(v, index, ival);
1710 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001711}
1712
Tim Peters5aa91602002-01-30 05:46:57 +00001713/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001714 (used by posix_stat() and posix_fstat()) */
1715static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001716_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001717{
Victor Stinner8c62be82010-05-06 00:08:46 +00001718 unsigned long ansec, mnsec, cnsec;
1719 PyObject *v = PyStructSequence_New(&StatResultType);
1720 if (v == NULL)
1721 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001722
Victor Stinner8c62be82010-05-06 00:08:46 +00001723 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001724#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001725 PyStructSequence_SET_ITEM(v, 1,
1726 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001727#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001728 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001729#endif
1730#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001731 PyStructSequence_SET_ITEM(v, 2,
1732 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001733#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001734 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001735#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001736 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1737 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1738 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001739#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001740 PyStructSequence_SET_ITEM(v, 6,
1741 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001742#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001743 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001744#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001745
Martin v. Löwis14694662006-02-03 12:54:16 +00001746#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001747 ansec = st->st_atim.tv_nsec;
1748 mnsec = st->st_mtim.tv_nsec;
1749 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001750#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001751 ansec = st->st_atimespec.tv_nsec;
1752 mnsec = st->st_mtimespec.tv_nsec;
1753 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001754#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001755 ansec = st->st_atime_nsec;
1756 mnsec = st->st_mtime_nsec;
1757 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001758#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001759 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001760#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001761 fill_time(v, 7, st->st_atime, ansec);
1762 fill_time(v, 8, st->st_mtime, mnsec);
1763 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001764
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001765#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001766 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1767 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001768#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001769#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001770 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1771 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001772#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001773#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001774 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1775 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001776#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001777#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001778 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1779 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001780#endif
1781#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001782 {
1783 PyObject *val;
1784 unsigned long bsec,bnsec;
1785 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001786#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001787 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001788#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001789 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001790#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001791 if (_stat_float_times) {
1792 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1793 } else {
1794 val = PyLong_FromLong((long)bsec);
1795 }
1796 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1797 val);
1798 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001799#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001800#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001801 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1802 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001803#endif
Fred Drake699f3522000-06-29 21:12:41 +00001804
Victor Stinner8c62be82010-05-06 00:08:46 +00001805 if (PyErr_Occurred()) {
1806 Py_DECREF(v);
1807 return NULL;
1808 }
Fred Drake699f3522000-06-29 21:12:41 +00001809
Victor Stinner8c62be82010-05-06 00:08:46 +00001810 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001811}
1812
Barry Warsaw53699e91996-12-10 23:23:01 +00001813static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001814posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001815 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001816#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001817 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001818#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001819 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001820#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001821 char *wformat,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001822 int (*wstatfunc)(const wchar_t *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001823{
Victor Stinner8c62be82010-05-06 00:08:46 +00001824 STRUCT_STAT st;
1825 PyObject *opath;
1826 char *path;
1827 int res;
1828 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001829
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001830#ifdef MS_WINDOWS
Victor Stinnereb5657a2011-09-30 01:44:27 +02001831 PyObject *po;
Victor Stinner8c62be82010-05-06 00:08:46 +00001832 if (PyArg_ParseTuple(args, wformat, &po)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02001833 wchar_t *wpath = PyUnicode_AsUnicode(po);
1834 if (wpath == NULL)
1835 return NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00001836
Victor Stinner8c62be82010-05-06 00:08:46 +00001837 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00001838 res = wstatfunc(wpath, &st);
1839 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001840
Victor Stinner8c62be82010-05-06 00:08:46 +00001841 if (res != 0)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001842 return win32_error_object("stat", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00001843 return _pystat_fromstructstat(&st);
1844 }
1845 /* Drop the argument parsing error as narrow strings
1846 are also valid. */
1847 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001848#endif
1849
Victor Stinner8c62be82010-05-06 00:08:46 +00001850 if (!PyArg_ParseTuple(args, format,
1851 PyUnicode_FSConverter, &opath))
1852 return NULL;
1853 path = PyBytes_AsString(opath);
1854 Py_BEGIN_ALLOW_THREADS
1855 res = (*statfunc)(path, &st);
1856 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001857
Victor Stinner8c62be82010-05-06 00:08:46 +00001858 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001859#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001860 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001861#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001862 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001863#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001864 }
1865 else
1866 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001867
Victor Stinner8c62be82010-05-06 00:08:46 +00001868 Py_DECREF(opath);
1869 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001870}
1871
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001872/* POSIX methods */
1873
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001874PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001875"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001876Use the real uid/gid to test for access to a path. Note that most\n\
1877operations will use the effective uid/gid, therefore this routine can\n\
1878be used in a suid/sgid environment to test if the invoking user has the\n\
1879specified access to the path. The mode argument can be F_OK to test\n\
1880existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001881
1882static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001883posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001884{
Victor Stinner8c62be82010-05-06 00:08:46 +00001885 PyObject *opath;
1886 char *path;
1887 int mode;
1888
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001889#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001890 DWORD attr;
Victor Stinnereb5657a2011-09-30 01:44:27 +02001891 PyObject *po;
Victor Stinner8c62be82010-05-06 00:08:46 +00001892 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02001893 wchar_t* wpath = PyUnicode_AsUnicode(po);
1894 if (wpath == NULL)
1895 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001896 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02001897 attr = GetFileAttributesW(wpath);
Victor Stinner8c62be82010-05-06 00:08:46 +00001898 Py_END_ALLOW_THREADS
1899 goto finish;
1900 }
1901 /* Drop the argument parsing error as narrow strings
1902 are also valid. */
1903 PyErr_Clear();
1904 if (!PyArg_ParseTuple(args, "O&i:access",
1905 PyUnicode_FSConverter, &opath, &mode))
1906 return NULL;
1907 path = PyBytes_AsString(opath);
1908 Py_BEGIN_ALLOW_THREADS
1909 attr = GetFileAttributesA(path);
1910 Py_END_ALLOW_THREADS
1911 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001912finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001913 if (attr == 0xFFFFFFFF)
1914 /* File does not exist, or cannot read attributes */
1915 return PyBool_FromLong(0);
1916 /* Access is possible if either write access wasn't requested, or
1917 the file isn't read-only, or if it's a directory, as there are
1918 no read-only directories on Windows. */
1919 return PyBool_FromLong(!(mode & 2)
1920 || !(attr & FILE_ATTRIBUTE_READONLY)
1921 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001922#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001923 int res;
1924 if (!PyArg_ParseTuple(args, "O&i:access",
1925 PyUnicode_FSConverter, &opath, &mode))
1926 return NULL;
1927 path = PyBytes_AsString(opath);
1928 Py_BEGIN_ALLOW_THREADS
1929 res = access(path, mode);
1930 Py_END_ALLOW_THREADS
1931 Py_DECREF(opath);
1932 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001933#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001934}
1935
Guido van Rossumd371ff11999-01-25 16:12:23 +00001936#ifndef F_OK
1937#define F_OK 0
1938#endif
1939#ifndef R_OK
1940#define R_OK 4
1941#endif
1942#ifndef W_OK
1943#define W_OK 2
1944#endif
1945#ifndef X_OK
1946#define X_OK 1
1947#endif
1948
1949#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001950PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001951"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001952Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001953
1954static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001955posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001956{
Victor Stinner8c62be82010-05-06 00:08:46 +00001957 int id;
1958 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001959
Victor Stinner8c62be82010-05-06 00:08:46 +00001960 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1961 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001962
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001963#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001964 /* file descriptor 0 only, the default input device (stdin) */
1965 if (id == 0) {
1966 ret = ttyname();
1967 }
1968 else {
1969 ret = NULL;
1970 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001971#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001972 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001973#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001974 if (ret == NULL)
1975 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001976 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001977}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001978#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001979
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001980#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001981PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001982"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001983Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001984
1985static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001986posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001987{
Victor Stinner8c62be82010-05-06 00:08:46 +00001988 char *ret;
1989 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001990
Greg Wardb48bc172000-03-01 21:51:56 +00001991#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001992 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001993#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001994 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001995#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001996 if (ret == NULL)
1997 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001998 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001999}
2000#endif
2001
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002002PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002003"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002004Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002005
Barry Warsaw53699e91996-12-10 23:23:01 +00002006static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002007posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002008{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002009#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002010 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002011#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002012 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00002013#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00002014 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002015#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002016 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002017#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002018}
2019
Fred Drake4d1e64b2002-04-15 19:40:07 +00002020#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002021PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002022"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00002023Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002024opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00002025
2026static PyObject *
2027posix_fchdir(PyObject *self, PyObject *fdobj)
2028{
Victor Stinner8c62be82010-05-06 00:08:46 +00002029 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002030}
2031#endif /* HAVE_FCHDIR */
2032
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002033
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002034PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002035"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002036Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002037
Barry Warsaw53699e91996-12-10 23:23:01 +00002038static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002039posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002040{
Victor Stinner8c62be82010-05-06 00:08:46 +00002041 PyObject *opath = NULL;
2042 char *path = NULL;
2043 int i;
2044 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002045#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002046 DWORD attr;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002047 PyObject *po;
Victor Stinner8c62be82010-05-06 00:08:46 +00002048 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02002049 wchar_t *wpath = PyUnicode_AsUnicode(po);
2050 if (wpath == NULL)
2051 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002052 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02002053 attr = GetFileAttributesW(wpath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002054 if (attr != 0xFFFFFFFF) {
2055 if (i & _S_IWRITE)
2056 attr &= ~FILE_ATTRIBUTE_READONLY;
2057 else
2058 attr |= FILE_ATTRIBUTE_READONLY;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002059 res = SetFileAttributesW(wpath, attr);
Victor Stinner8c62be82010-05-06 00:08:46 +00002060 }
2061 else
2062 res = 0;
2063 Py_END_ALLOW_THREADS
2064 if (!res)
Victor Stinnereb5657a2011-09-30 01:44:27 +02002065 return win32_error_object("chmod", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00002066 Py_INCREF(Py_None);
2067 return Py_None;
2068 }
2069 /* Drop the argument parsing error as narrow strings
2070 are also valid. */
2071 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002072
Victor Stinner8c62be82010-05-06 00:08:46 +00002073 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2074 &opath, &i))
2075 return NULL;
2076 path = PyBytes_AsString(opath);
2077 Py_BEGIN_ALLOW_THREADS
2078 attr = GetFileAttributesA(path);
2079 if (attr != 0xFFFFFFFF) {
2080 if (i & _S_IWRITE)
2081 attr &= ~FILE_ATTRIBUTE_READONLY;
2082 else
2083 attr |= FILE_ATTRIBUTE_READONLY;
2084 res = SetFileAttributesA(path, attr);
2085 }
2086 else
2087 res = 0;
2088 Py_END_ALLOW_THREADS
2089 if (!res) {
2090 win32_error("chmod", path);
2091 Py_DECREF(opath);
2092 return NULL;
2093 }
2094 Py_DECREF(opath);
2095 Py_INCREF(Py_None);
2096 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002097#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00002098 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2099 &opath, &i))
2100 return NULL;
2101 path = PyBytes_AsString(opath);
2102 Py_BEGIN_ALLOW_THREADS
2103 res = chmod(path, i);
2104 Py_END_ALLOW_THREADS
2105 if (res < 0)
2106 return posix_error_with_allocated_filename(opath);
2107 Py_DECREF(opath);
2108 Py_INCREF(Py_None);
2109 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002110#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002111}
2112
Christian Heimes4e30a842007-11-30 22:12:06 +00002113#ifdef HAVE_FCHMOD
2114PyDoc_STRVAR(posix_fchmod__doc__,
2115"fchmod(fd, mode)\n\n\
2116Change the access permissions of the file given by file\n\
2117descriptor fd.");
2118
2119static PyObject *
2120posix_fchmod(PyObject *self, PyObject *args)
2121{
Victor Stinner8c62be82010-05-06 00:08:46 +00002122 int fd, mode, res;
2123 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2124 return NULL;
2125 Py_BEGIN_ALLOW_THREADS
2126 res = fchmod(fd, mode);
2127 Py_END_ALLOW_THREADS
2128 if (res < 0)
2129 return posix_error();
2130 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002131}
2132#endif /* HAVE_FCHMOD */
2133
2134#ifdef HAVE_LCHMOD
2135PyDoc_STRVAR(posix_lchmod__doc__,
2136"lchmod(path, mode)\n\n\
2137Change the access permissions of a file. If path is a symlink, this\n\
2138affects the link itself rather than the target.");
2139
2140static PyObject *
2141posix_lchmod(PyObject *self, PyObject *args)
2142{
Victor Stinner8c62be82010-05-06 00:08:46 +00002143 PyObject *opath;
2144 char *path;
2145 int i;
2146 int res;
2147 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2148 &opath, &i))
2149 return NULL;
2150 path = PyBytes_AsString(opath);
2151 Py_BEGIN_ALLOW_THREADS
2152 res = lchmod(path, i);
2153 Py_END_ALLOW_THREADS
2154 if (res < 0)
2155 return posix_error_with_allocated_filename(opath);
2156 Py_DECREF(opath);
2157 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002158}
2159#endif /* HAVE_LCHMOD */
2160
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002161
Thomas Wouterscf297e42007-02-23 15:07:44 +00002162#ifdef HAVE_CHFLAGS
2163PyDoc_STRVAR(posix_chflags__doc__,
2164"chflags(path, flags)\n\n\
2165Set file flags.");
2166
2167static PyObject *
2168posix_chflags(PyObject *self, PyObject *args)
2169{
Victor Stinner8c62be82010-05-06 00:08:46 +00002170 PyObject *opath;
2171 char *path;
2172 unsigned long flags;
2173 int res;
2174 if (!PyArg_ParseTuple(args, "O&k:chflags",
2175 PyUnicode_FSConverter, &opath, &flags))
2176 return NULL;
2177 path = PyBytes_AsString(opath);
2178 Py_BEGIN_ALLOW_THREADS
2179 res = chflags(path, flags);
2180 Py_END_ALLOW_THREADS
2181 if (res < 0)
2182 return posix_error_with_allocated_filename(opath);
2183 Py_DECREF(opath);
2184 Py_INCREF(Py_None);
2185 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002186}
2187#endif /* HAVE_CHFLAGS */
2188
2189#ifdef HAVE_LCHFLAGS
2190PyDoc_STRVAR(posix_lchflags__doc__,
2191"lchflags(path, flags)\n\n\
2192Set file flags.\n\
2193This function will not follow symbolic links.");
2194
2195static PyObject *
2196posix_lchflags(PyObject *self, PyObject *args)
2197{
Victor Stinner8c62be82010-05-06 00:08:46 +00002198 PyObject *opath;
2199 char *path;
2200 unsigned long flags;
2201 int res;
2202 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2203 PyUnicode_FSConverter, &opath, &flags))
2204 return NULL;
2205 path = PyBytes_AsString(opath);
2206 Py_BEGIN_ALLOW_THREADS
2207 res = lchflags(path, flags);
2208 Py_END_ALLOW_THREADS
2209 if (res < 0)
2210 return posix_error_with_allocated_filename(opath);
2211 Py_DECREF(opath);
2212 Py_INCREF(Py_None);
2213 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002214}
2215#endif /* HAVE_LCHFLAGS */
2216
Martin v. Löwis244edc82001-10-04 22:44:26 +00002217#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002218PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002219"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002220Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002221
2222static PyObject *
2223posix_chroot(PyObject *self, PyObject *args)
2224{
Victor Stinner8c62be82010-05-06 00:08:46 +00002225 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002226}
2227#endif
2228
Guido van Rossum21142a01999-01-08 21:05:37 +00002229#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002230PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002231"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002232force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002233
2234static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002235posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002236{
Stefan Krah0e803b32010-11-26 16:16:47 +00002237 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002238}
2239#endif /* HAVE_FSYNC */
2240
Ross Lagerwall7807c352011-03-17 20:20:30 +02002241#ifdef HAVE_SYNC
2242PyDoc_STRVAR(posix_sync__doc__,
2243"sync()\n\n\
2244Force write of everything to disk.");
2245
2246static PyObject *
2247posix_sync(PyObject *self, PyObject *noargs)
2248{
2249 Py_BEGIN_ALLOW_THREADS
2250 sync();
2251 Py_END_ALLOW_THREADS
2252 Py_RETURN_NONE;
2253}
2254#endif
2255
Guido van Rossum21142a01999-01-08 21:05:37 +00002256#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002257
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002258#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002259extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2260#endif
2261
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002262PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002263"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002264force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002265 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002266
2267static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002268posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002269{
Stefan Krah0e803b32010-11-26 16:16:47 +00002270 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002271}
2272#endif /* HAVE_FDATASYNC */
2273
2274
Fredrik Lundh10723342000-07-10 16:38:09 +00002275#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002276PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002277"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002278Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002279
Barry Warsaw53699e91996-12-10 23:23:01 +00002280static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002281posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002282{
Victor Stinner8c62be82010-05-06 00:08:46 +00002283 PyObject *opath;
2284 char *path;
2285 long uid, gid;
2286 int res;
2287 if (!PyArg_ParseTuple(args, "O&ll:chown",
2288 PyUnicode_FSConverter, &opath,
2289 &uid, &gid))
2290 return NULL;
2291 path = PyBytes_AsString(opath);
2292 Py_BEGIN_ALLOW_THREADS
2293 res = chown(path, (uid_t) uid, (gid_t) gid);
2294 Py_END_ALLOW_THREADS
2295 if (res < 0)
2296 return posix_error_with_allocated_filename(opath);
2297 Py_DECREF(opath);
2298 Py_INCREF(Py_None);
2299 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002300}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002301#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002302
Christian Heimes4e30a842007-11-30 22:12:06 +00002303#ifdef HAVE_FCHOWN
2304PyDoc_STRVAR(posix_fchown__doc__,
2305"fchown(fd, uid, gid)\n\n\
2306Change the owner and group id of the file given by file descriptor\n\
2307fd to the numeric uid and gid.");
2308
2309static PyObject *
2310posix_fchown(PyObject *self, PyObject *args)
2311{
Victor Stinner8c62be82010-05-06 00:08:46 +00002312 int fd;
2313 long uid, gid;
2314 int res;
Victor Stinner26de69d2011-06-17 15:15:38 +02002315 if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00002316 return NULL;
2317 Py_BEGIN_ALLOW_THREADS
2318 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2319 Py_END_ALLOW_THREADS
2320 if (res < 0)
2321 return posix_error();
2322 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002323}
2324#endif /* HAVE_FCHOWN */
2325
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002326#ifdef HAVE_LCHOWN
2327PyDoc_STRVAR(posix_lchown__doc__,
2328"lchown(path, uid, gid)\n\n\
2329Change the owner and group id of path to the numeric uid and gid.\n\
2330This function will not follow symbolic links.");
2331
2332static PyObject *
2333posix_lchown(PyObject *self, PyObject *args)
2334{
Victor Stinner8c62be82010-05-06 00:08:46 +00002335 PyObject *opath;
2336 char *path;
2337 long uid, gid;
2338 int res;
2339 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2340 PyUnicode_FSConverter, &opath,
2341 &uid, &gid))
2342 return NULL;
2343 path = PyBytes_AsString(opath);
2344 Py_BEGIN_ALLOW_THREADS
2345 res = lchown(path, (uid_t) uid, (gid_t) gid);
2346 Py_END_ALLOW_THREADS
2347 if (res < 0)
2348 return posix_error_with_allocated_filename(opath);
2349 Py_DECREF(opath);
2350 Py_INCREF(Py_None);
2351 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002352}
2353#endif /* HAVE_LCHOWN */
2354
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002355
Guido van Rossum36bc6801995-06-14 22:54:23 +00002356#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002357static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002358posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002359{
Victor Stinner8c62be82010-05-06 00:08:46 +00002360 char buf[1026];
2361 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002362
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002363#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002364 if (!use_bytes) {
2365 wchar_t wbuf[1026];
2366 wchar_t *wbuf2 = wbuf;
2367 PyObject *resobj;
2368 DWORD len;
2369 Py_BEGIN_ALLOW_THREADS
2370 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2371 /* If the buffer is large enough, len does not include the
2372 terminating \0. If the buffer is too small, len includes
2373 the space needed for the terminator. */
2374 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2375 wbuf2 = malloc(len * sizeof(wchar_t));
2376 if (wbuf2)
2377 len = GetCurrentDirectoryW(len, wbuf2);
2378 }
2379 Py_END_ALLOW_THREADS
2380 if (!wbuf2) {
2381 PyErr_NoMemory();
2382 return NULL;
2383 }
2384 if (!len) {
2385 if (wbuf2 != wbuf) free(wbuf2);
2386 return win32_error("getcwdu", NULL);
2387 }
2388 resobj = PyUnicode_FromWideChar(wbuf2, len);
2389 if (wbuf2 != wbuf) free(wbuf2);
2390 return resobj;
2391 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002392#endif
2393
Victor Stinner8c62be82010-05-06 00:08:46 +00002394 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002395#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002396 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002397#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002398 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002399#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002400 Py_END_ALLOW_THREADS
2401 if (res == NULL)
2402 return posix_error();
2403 if (use_bytes)
2404 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002405 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002406}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002407
2408PyDoc_STRVAR(posix_getcwd__doc__,
2409"getcwd() -> path\n\n\
2410Return a unicode string representing the current working directory.");
2411
2412static PyObject *
2413posix_getcwd_unicode(PyObject *self)
2414{
2415 return posix_getcwd(0);
2416}
2417
2418PyDoc_STRVAR(posix_getcwdb__doc__,
2419"getcwdb() -> path\n\n\
2420Return a bytes string representing the current working directory.");
2421
2422static PyObject *
2423posix_getcwd_bytes(PyObject *self)
2424{
2425 return posix_getcwd(1);
2426}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002427#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002428
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002429
Guido van Rossumb6775db1994-08-01 11:34:53 +00002430#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002431PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002432"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002433Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002434
Barry Warsaw53699e91996-12-10 23:23:01 +00002435static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002436posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002437{
Victor Stinner8c62be82010-05-06 00:08:46 +00002438 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002439}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002440#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002441
Brian Curtin1b9df392010-11-24 20:24:31 +00002442#ifdef MS_WINDOWS
2443PyDoc_STRVAR(win32_link__doc__,
2444"link(src, dst)\n\n\
2445Create a hard link to a file.");
2446
2447static PyObject *
2448win32_link(PyObject *self, PyObject *args)
2449{
2450 PyObject *osrc, *odst;
2451 char *src, *dst;
2452 BOOL rslt;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002453 PyObject *usrc, *udst;
Brian Curtin1b9df392010-11-24 20:24:31 +00002454
Victor Stinnereb5657a2011-09-30 01:44:27 +02002455 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst))
2456 {
2457 wchar_t *wsrc, *wdst;
2458 wsrc = PyUnicode_AsUnicode(usrc);
2459 if (wsrc == NULL)
2460 return NULL;
2461 wdst = PyUnicode_AsUnicode(udst);
2462 if (wdst == NULL)
2463 return NULL;
2464
Brian Curtinfc889c42010-11-28 23:59:46 +00002465 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02002466 rslt = CreateHardLinkW(wdst, wsrc, NULL);
Brian Curtinfc889c42010-11-28 23:59:46 +00002467 Py_END_ALLOW_THREADS
2468
2469 if (rslt == 0)
2470 return win32_error("link", NULL);
2471
2472 Py_RETURN_NONE;
2473 }
2474
2475 /* Narrow strings also valid. */
2476 PyErr_Clear();
2477
Brian Curtin1b9df392010-11-24 20:24:31 +00002478 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2479 PyUnicode_FSConverter, &odst))
2480 return NULL;
2481
2482 src = PyBytes_AsString(osrc);
2483 dst = PyBytes_AsString(odst);
2484
2485 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002486 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002487 Py_END_ALLOW_THREADS
2488
Stefan Krah30b341f2010-11-27 11:44:18 +00002489 Py_DECREF(osrc);
2490 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002491 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002492 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002493
2494 Py_RETURN_NONE;
2495}
2496#endif /* MS_WINDOWS */
2497
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002498
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002499PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002500"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002501Return a list containing the names of the entries in the directory.\n\
2502\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002503 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002504\n\
2505The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002506entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002507
Barry Warsaw53699e91996-12-10 23:23:01 +00002508static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002509posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002510{
Victor Stinner8c62be82010-05-06 00:08:46 +00002511 /* XXX Should redo this putting the (now four) versions of opendir
2512 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002513#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002514
Victor Stinner8c62be82010-05-06 00:08:46 +00002515 PyObject *d, *v;
2516 HANDLE hFindFile;
2517 BOOL result;
2518 WIN32_FIND_DATA FileData;
2519 PyObject *opath;
2520 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2521 char *bufptr = namebuf;
2522 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002523
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002524 PyObject *po = NULL;
2525 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002526 WIN32_FIND_DATAW wFileData;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002527 wchar_t *wnamebuf, *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002528
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002529 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002530 po_wchars = L".";
2531 len = 1;
2532 } else {
Victor Stinnereb5657a2011-09-30 01:44:27 +02002533 po_wchars = PyUnicode_AsUnicode(po);
2534 if (po_wchars == NULL)
2535 return NULL;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002536 len = PyUnicode_GET_SIZE(po);
2537 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002538 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002539 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2540 if (!wnamebuf) {
2541 PyErr_NoMemory();
2542 return NULL;
2543 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002544 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002545 if (len > 0) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02002546 wchar_t wch = wnamebuf[len-1];
Victor Stinner8c62be82010-05-06 00:08:46 +00002547 if (wch != L'/' && wch != L'\\' && wch != L':')
2548 wnamebuf[len++] = L'\\';
2549 wcscpy(wnamebuf + len, L"*.*");
2550 }
2551 if ((d = PyList_New(0)) == NULL) {
2552 free(wnamebuf);
2553 return NULL;
2554 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002555 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002556 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002557 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002558 if (hFindFile == INVALID_HANDLE_VALUE) {
2559 int error = GetLastError();
2560 if (error == ERROR_FILE_NOT_FOUND) {
2561 free(wnamebuf);
2562 return d;
2563 }
2564 Py_DECREF(d);
2565 win32_error_unicode("FindFirstFileW", wnamebuf);
2566 free(wnamebuf);
2567 return NULL;
2568 }
2569 do {
2570 /* Skip over . and .. */
2571 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2572 wcscmp(wFileData.cFileName, L"..") != 0) {
2573 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2574 if (v == NULL) {
2575 Py_DECREF(d);
2576 d = NULL;
2577 break;
2578 }
2579 if (PyList_Append(d, v) != 0) {
2580 Py_DECREF(v);
2581 Py_DECREF(d);
2582 d = NULL;
2583 break;
2584 }
2585 Py_DECREF(v);
2586 }
2587 Py_BEGIN_ALLOW_THREADS
2588 result = FindNextFileW(hFindFile, &wFileData);
2589 Py_END_ALLOW_THREADS
2590 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2591 it got to the end of the directory. */
2592 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2593 Py_DECREF(d);
2594 win32_error_unicode("FindNextFileW", wnamebuf);
2595 FindClose(hFindFile);
2596 free(wnamebuf);
2597 return NULL;
2598 }
2599 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002600
Victor Stinner8c62be82010-05-06 00:08:46 +00002601 if (FindClose(hFindFile) == FALSE) {
2602 Py_DECREF(d);
2603 win32_error_unicode("FindClose", wnamebuf);
2604 free(wnamebuf);
2605 return NULL;
2606 }
2607 free(wnamebuf);
2608 return d;
2609 }
2610 /* Drop the argument parsing error as narrow strings
2611 are also valid. */
2612 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002613
Victor Stinner8c62be82010-05-06 00:08:46 +00002614 if (!PyArg_ParseTuple(args, "O&:listdir",
2615 PyUnicode_FSConverter, &opath))
2616 return NULL;
2617 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2618 PyErr_SetString(PyExc_ValueError, "path too long");
2619 Py_DECREF(opath);
2620 return NULL;
2621 }
2622 strcpy(namebuf, PyBytes_AsString(opath));
2623 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002624 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002625 if (len > 0) {
2626 char ch = namebuf[len-1];
2627 if (ch != SEP && ch != ALTSEP && ch != ':')
2628 namebuf[len++] = '/';
2629 strcpy(namebuf + len, "*.*");
2630 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002631
Victor Stinner8c62be82010-05-06 00:08:46 +00002632 if ((d = PyList_New(0)) == NULL)
2633 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002634
Antoine Pitroub73caab2010-08-09 23:39:31 +00002635 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002636 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002637 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002638 if (hFindFile == INVALID_HANDLE_VALUE) {
2639 int error = GetLastError();
2640 if (error == ERROR_FILE_NOT_FOUND)
2641 return d;
2642 Py_DECREF(d);
2643 return win32_error("FindFirstFile", namebuf);
2644 }
2645 do {
2646 /* Skip over . and .. */
2647 if (strcmp(FileData.cFileName, ".") != 0 &&
2648 strcmp(FileData.cFileName, "..") != 0) {
2649 v = PyBytes_FromString(FileData.cFileName);
2650 if (v == NULL) {
2651 Py_DECREF(d);
2652 d = NULL;
2653 break;
2654 }
2655 if (PyList_Append(d, v) != 0) {
2656 Py_DECREF(v);
2657 Py_DECREF(d);
2658 d = NULL;
2659 break;
2660 }
2661 Py_DECREF(v);
2662 }
2663 Py_BEGIN_ALLOW_THREADS
2664 result = FindNextFile(hFindFile, &FileData);
2665 Py_END_ALLOW_THREADS
2666 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2667 it got to the end of the directory. */
2668 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2669 Py_DECREF(d);
2670 win32_error("FindNextFile", namebuf);
2671 FindClose(hFindFile);
2672 return NULL;
2673 }
2674 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002675
Victor Stinner8c62be82010-05-06 00:08:46 +00002676 if (FindClose(hFindFile) == FALSE) {
2677 Py_DECREF(d);
2678 return win32_error("FindClose", namebuf);
2679 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002680
Victor Stinner8c62be82010-05-06 00:08:46 +00002681 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002682
Tim Peters0bb44a42000-09-15 07:44:49 +00002683#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002684
2685#ifndef MAX_PATH
2686#define MAX_PATH CCHMAXPATH
2687#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002688 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002689 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002690 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002691 PyObject *d, *v;
2692 char namebuf[MAX_PATH+5];
2693 HDIR hdir = 1;
2694 ULONG srchcnt = 1;
2695 FILEFINDBUF3 ep;
2696 APIRET rc;
2697
Victor Stinner8c62be82010-05-06 00:08:46 +00002698 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002699 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002700 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002701 name = PyBytes_AsString(oname);
2702 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002703 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002704 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002705 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002706 return NULL;
2707 }
2708 strcpy(namebuf, name);
2709 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002710 if (*pt == ALTSEP)
2711 *pt = SEP;
2712 if (namebuf[len-1] != SEP)
2713 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002714 strcpy(namebuf + len, "*.*");
2715
Neal Norwitz6c913782007-10-14 03:23:09 +00002716 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002717 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002718 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002719 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002720
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002721 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2722 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002723 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002724 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2725 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2726 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002727
2728 if (rc != NO_ERROR) {
2729 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002730 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002731 }
2732
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002733 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002734 do {
2735 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002736 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002737 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002738
2739 strcpy(namebuf, ep.achName);
2740
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002741 /* Leave Case of Name Alone -- In Native Form */
2742 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002743
Christian Heimes72b710a2008-05-26 13:28:38 +00002744 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002745 if (v == NULL) {
2746 Py_DECREF(d);
2747 d = NULL;
2748 break;
2749 }
2750 if (PyList_Append(d, v) != 0) {
2751 Py_DECREF(v);
2752 Py_DECREF(d);
2753 d = NULL;
2754 break;
2755 }
2756 Py_DECREF(v);
2757 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2758 }
2759
Victor Stinnerdcb24032010-04-22 12:08:36 +00002760 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002761 return d;
2762#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002763 PyObject *oname;
2764 char *name;
2765 PyObject *d, *v;
2766 DIR *dirp;
2767 struct dirent *ep;
2768 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002769
Victor Stinner8c62be82010-05-06 00:08:46 +00002770 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002771 /* v is never read, so it does not need to be initialized yet. */
2772 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002773 arg_is_unicode = 0;
2774 PyErr_Clear();
2775 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002776 oname = NULL;
2777 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002778 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002779 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002780 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002781 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002782 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002783 Py_BEGIN_ALLOW_THREADS
2784 dirp = opendir(name);
2785 Py_END_ALLOW_THREADS
2786 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002787 return posix_error_with_allocated_filename(oname);
2788 }
2789 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002790 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002791 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002792 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002793 Py_DECREF(oname);
2794 return NULL;
2795 }
2796 for (;;) {
2797 errno = 0;
2798 Py_BEGIN_ALLOW_THREADS
2799 ep = readdir(dirp);
2800 Py_END_ALLOW_THREADS
2801 if (ep == NULL) {
2802 if (errno == 0) {
2803 break;
2804 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002805 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002806 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002807 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002808 Py_DECREF(d);
2809 return posix_error_with_allocated_filename(oname);
2810 }
2811 }
2812 if (ep->d_name[0] == '.' &&
2813 (NAMLEN(ep) == 1 ||
2814 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2815 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002816 if (arg_is_unicode)
2817 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2818 else
2819 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002820 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002821 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002822 break;
2823 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002824 if (PyList_Append(d, v) != 0) {
2825 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002826 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002827 break;
2828 }
2829 Py_DECREF(v);
2830 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002831 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002832 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002833 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002834 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002835
Victor Stinner8c62be82010-05-06 00:08:46 +00002836 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002837
Tim Peters0bb44a42000-09-15 07:44:49 +00002838#endif /* which OS */
2839} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002840
Antoine Pitrou8250e232011-02-25 23:41:16 +00002841#ifdef HAVE_FDOPENDIR
2842PyDoc_STRVAR(posix_fdlistdir__doc__,
2843"fdlistdir(fd) -> list_of_strings\n\n\
2844Like listdir(), but uses a file descriptor instead.\n\
2845After succesful execution of this function, fd will be closed.");
2846
2847static PyObject *
2848posix_fdlistdir(PyObject *self, PyObject *args)
2849{
2850 PyObject *d, *v;
2851 DIR *dirp;
2852 struct dirent *ep;
2853 int fd;
2854
2855 errno = 0;
2856 if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd))
2857 return NULL;
2858 Py_BEGIN_ALLOW_THREADS
2859 dirp = fdopendir(fd);
2860 Py_END_ALLOW_THREADS
2861 if (dirp == NULL) {
2862 close(fd);
2863 return posix_error();
2864 }
2865 if ((d = PyList_New(0)) == NULL) {
2866 Py_BEGIN_ALLOW_THREADS
2867 closedir(dirp);
2868 Py_END_ALLOW_THREADS
2869 return NULL;
2870 }
2871 for (;;) {
2872 errno = 0;
2873 Py_BEGIN_ALLOW_THREADS
2874 ep = readdir(dirp);
2875 Py_END_ALLOW_THREADS
2876 if (ep == NULL) {
2877 if (errno == 0) {
2878 break;
2879 } else {
2880 Py_BEGIN_ALLOW_THREADS
2881 closedir(dirp);
2882 Py_END_ALLOW_THREADS
2883 Py_DECREF(d);
2884 return posix_error();
2885 }
2886 }
2887 if (ep->d_name[0] == '.' &&
2888 (NAMLEN(ep) == 1 ||
2889 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2890 continue;
2891 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2892 if (v == NULL) {
2893 Py_CLEAR(d);
2894 break;
2895 }
2896 if (PyList_Append(d, v) != 0) {
2897 Py_DECREF(v);
2898 Py_CLEAR(d);
2899 break;
2900 }
2901 Py_DECREF(v);
2902 }
2903 Py_BEGIN_ALLOW_THREADS
2904 closedir(dirp);
2905 Py_END_ALLOW_THREADS
2906
2907 return d;
2908}
2909#endif
2910
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002911#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002912/* A helper function for abspath on win32 */
2913static PyObject *
2914posix__getfullpathname(PyObject *self, PyObject *args)
2915{
Victor Stinner8c62be82010-05-06 00:08:46 +00002916 PyObject *opath;
2917 char *path;
2918 char outbuf[MAX_PATH*2];
2919 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002920#ifdef MS_WINDOWS
Victor Stinnereb5657a2011-09-30 01:44:27 +02002921 PyObject *po;
2922
2923 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po))
2924 {
2925 wchar_t *wpath;
2926 wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2927 wchar_t *wtemp;
Victor Stinner8c62be82010-05-06 00:08:46 +00002928 DWORD result;
2929 PyObject *v;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002930
2931 wpath = PyUnicode_AsUnicode(po);
2932 if (wpath == NULL)
2933 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002934 result = GetFullPathNameW(wpath,
Victor Stinner63941882011-09-29 00:42:28 +02002935 Py_ARRAY_LENGTH(woutbuf),
Victor Stinner8c62be82010-05-06 00:08:46 +00002936 woutbuf, &wtemp);
Victor Stinner63941882011-09-29 00:42:28 +02002937 if (result > Py_ARRAY_LENGTH(woutbuf)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02002938 woutbufp = malloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00002939 if (!woutbufp)
2940 return PyErr_NoMemory();
2941 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2942 }
2943 if (result)
2944 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2945 else
Victor Stinnereb5657a2011-09-30 01:44:27 +02002946 v = win32_error_object("GetFullPathNameW", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00002947 if (woutbufp != woutbuf)
2948 free(woutbufp);
2949 return v;
2950 }
2951 /* Drop the argument parsing error as narrow strings
2952 are also valid. */
2953 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002954#endif
Victor Stinnereb5657a2011-09-30 01:44:27 +02002955
Victor Stinner8c62be82010-05-06 00:08:46 +00002956 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2957 PyUnicode_FSConverter, &opath))
2958 return NULL;
2959 path = PyBytes_AsString(opath);
Victor Stinner63941882011-09-29 00:42:28 +02002960 if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf),
Victor Stinner8c62be82010-05-06 00:08:46 +00002961 outbuf, &temp)) {
2962 win32_error("GetFullPathName", path);
2963 Py_DECREF(opath);
2964 return NULL;
2965 }
2966 Py_DECREF(opath);
2967 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2968 return PyUnicode_Decode(outbuf, strlen(outbuf),
2969 Py_FileSystemDefaultEncoding, NULL);
2970 }
2971 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002972} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002973
Brian Curtind25aef52011-06-13 15:16:04 -05002974
Brian Curtinf5e76d02010-11-24 13:14:05 +00002975
Brian Curtind40e6f72010-07-08 21:39:08 +00002976/* A helper function for samepath on windows */
2977static PyObject *
2978posix__getfinalpathname(PyObject *self, PyObject *args)
2979{
2980 HANDLE hFile;
2981 int buf_size;
2982 wchar_t *target_path;
2983 int result_length;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002984 PyObject *po, *result;
Brian Curtind40e6f72010-07-08 21:39:08 +00002985 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002986
Victor Stinnereb5657a2011-09-30 01:44:27 +02002987 if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po))
Brian Curtind40e6f72010-07-08 21:39:08 +00002988 return NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002989 path = PyUnicode_AsUnicode(po);
2990 if (path == NULL)
2991 return NULL;
Brian Curtind40e6f72010-07-08 21:39:08 +00002992
2993 if(!check_GetFinalPathNameByHandle()) {
2994 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2995 NotImplementedError. */
2996 return PyErr_Format(PyExc_NotImplementedError,
2997 "GetFinalPathNameByHandle not available on this platform");
2998 }
2999
3000 hFile = CreateFileW(
3001 path,
3002 0, /* desired access */
3003 0, /* share mode */
3004 NULL, /* security attributes */
3005 OPEN_EXISTING,
3006 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3007 FILE_FLAG_BACKUP_SEMANTICS,
3008 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003009
Victor Stinnereb5657a2011-09-30 01:44:27 +02003010 if(hFile == INVALID_HANDLE_VALUE)
3011 return win32_error_object("CreateFileW", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003012
3013 /* We have a good handle to the target, use it to determine the
3014 target path name. */
3015 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
3016
3017 if(!buf_size)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003018 return win32_error_object("GetFinalPathNameByHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003019
3020 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
3021 if(!target_path)
3022 return PyErr_NoMemory();
3023
3024 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
3025 buf_size, VOLUME_NAME_DOS);
3026 if(!result_length)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003027 return win32_error_object("GetFinalPathNamyByHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003028
3029 if(!CloseHandle(hFile))
Victor Stinnereb5657a2011-09-30 01:44:27 +02003030 return win32_error_object("CloseHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003031
3032 target_path[result_length] = 0;
3033 result = PyUnicode_FromUnicode(target_path, result_length);
3034 free(target_path);
3035 return result;
3036
3037} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00003038
3039static PyObject *
3040posix__getfileinformation(PyObject *self, PyObject *args)
3041{
3042 HANDLE hFile;
3043 BY_HANDLE_FILE_INFORMATION info;
3044 int fd;
3045
3046 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
3047 return NULL;
3048
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003049 if (!_PyVerify_fd(fd))
3050 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003051
3052 hFile = (HANDLE)_get_osfhandle(fd);
3053 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003054 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003055
3056 if (!GetFileInformationByHandle(hFile, &info))
3057 return win32_error("_getfileinformation", NULL);
3058
3059 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
3060 info.nFileIndexHigh,
3061 info.nFileIndexLow);
3062}
Brian Curtin9c669cc2011-06-08 18:17:18 -05003063
Brian Curtin95d028f2011-06-09 09:10:38 -05003064PyDoc_STRVAR(posix__isdir__doc__,
3065"Return true if the pathname refers to an existing directory.");
3066
Brian Curtin9c669cc2011-06-08 18:17:18 -05003067static PyObject *
3068posix__isdir(PyObject *self, PyObject *args)
3069{
3070 PyObject *opath;
3071 char *path;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003072 PyObject *po;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003073 DWORD attributes;
3074
3075 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003076 wchar_t *wpath = PyUnicode_AsUnicode(po);
3077 if (wpath == NULL)
3078 return NULL;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003079
3080 attributes = GetFileAttributesW(wpath);
3081 if (attributes == INVALID_FILE_ATTRIBUTES)
3082 Py_RETURN_FALSE;
3083 goto check;
3084 }
3085 /* Drop the argument parsing error as narrow strings
3086 are also valid. */
3087 PyErr_Clear();
3088
3089 if (!PyArg_ParseTuple(args, "O&:_isdir",
3090 PyUnicode_FSConverter, &opath))
3091 return NULL;
3092
3093 path = PyBytes_AsString(opath);
3094 attributes = GetFileAttributesA(path);
3095 if (attributes == INVALID_FILE_ATTRIBUTES)
3096 Py_RETURN_FALSE;
3097
3098check:
3099 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3100 Py_RETURN_TRUE;
3101 else
3102 Py_RETURN_FALSE;
3103}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003104#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003105
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003106PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003107"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003108Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003109
Barry Warsaw53699e91996-12-10 23:23:01 +00003110static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003111posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003112{
Victor Stinner8c62be82010-05-06 00:08:46 +00003113 int res;
3114 PyObject *opath;
3115 char *path;
3116 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003117
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003118#ifdef MS_WINDOWS
Victor Stinnereb5657a2011-09-30 01:44:27 +02003119 PyObject *po;
3120 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode))
3121 {
3122 wchar_t *wpath = PyUnicode_AsUnicode(po);
3123 if (wpath == NULL)
3124 return NULL;
3125
Victor Stinner8c62be82010-05-06 00:08:46 +00003126 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02003127 res = CreateDirectoryW(wpath, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00003128 Py_END_ALLOW_THREADS
3129 if (!res)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003130 return win32_error_object("mkdir", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00003131 Py_INCREF(Py_None);
3132 return Py_None;
3133 }
3134 /* Drop the argument parsing error as narrow strings
3135 are also valid. */
3136 PyErr_Clear();
3137 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3138 PyUnicode_FSConverter, &opath, &mode))
3139 return NULL;
3140 path = PyBytes_AsString(opath);
3141 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003142 res = CreateDirectoryA(path, NULL);
3143 Py_END_ALLOW_THREADS
3144 if (!res) {
3145 win32_error("mkdir", path);
3146 Py_DECREF(opath);
3147 return NULL;
3148 }
3149 Py_DECREF(opath);
3150 Py_INCREF(Py_None);
3151 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003152#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003153
Victor Stinner8c62be82010-05-06 00:08:46 +00003154 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3155 PyUnicode_FSConverter, &opath, &mode))
3156 return NULL;
3157 path = PyBytes_AsString(opath);
3158 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00003159#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00003160 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003161#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003162 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003163#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003164 Py_END_ALLOW_THREADS
3165 if (res < 0)
3166 return posix_error_with_allocated_filename(opath);
3167 Py_DECREF(opath);
3168 Py_INCREF(Py_None);
3169 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003170#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003171}
3172
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003173
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003174/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3175#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003176#include <sys/resource.h>
3177#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003178
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003179
3180#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003181PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003182"nice(inc) -> new_priority\n\n\
3183Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003184
Barry Warsaw53699e91996-12-10 23:23:01 +00003185static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003186posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003187{
Victor Stinner8c62be82010-05-06 00:08:46 +00003188 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003189
Victor Stinner8c62be82010-05-06 00:08:46 +00003190 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3191 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003192
Victor Stinner8c62be82010-05-06 00:08:46 +00003193 /* There are two flavours of 'nice': one that returns the new
3194 priority (as required by almost all standards out there) and the
3195 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3196 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003197
Victor Stinner8c62be82010-05-06 00:08:46 +00003198 If we are of the nice family that returns the new priority, we
3199 need to clear errno before the call, and check if errno is filled
3200 before calling posix_error() on a returnvalue of -1, because the
3201 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003202
Victor Stinner8c62be82010-05-06 00:08:46 +00003203 errno = 0;
3204 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003205#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003206 if (value == 0)
3207 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003208#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003209 if (value == -1 && errno != 0)
3210 /* either nice() or getpriority() returned an error */
3211 return posix_error();
3212 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003213}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003214#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003215
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003216
3217#ifdef HAVE_GETPRIORITY
3218PyDoc_STRVAR(posix_getpriority__doc__,
3219"getpriority(which, who) -> current_priority\n\n\
3220Get program scheduling priority.");
3221
3222static PyObject *
3223posix_getpriority(PyObject *self, PyObject *args)
3224{
3225 int which, who, retval;
3226
3227 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3228 return NULL;
3229 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003230 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003231 if (errno != 0)
3232 return posix_error();
3233 return PyLong_FromLong((long)retval);
3234}
3235#endif /* HAVE_GETPRIORITY */
3236
3237
3238#ifdef HAVE_SETPRIORITY
3239PyDoc_STRVAR(posix_setpriority__doc__,
3240"setpriority(which, who, prio) -> None\n\n\
3241Set program scheduling priority.");
3242
3243static PyObject *
3244posix_setpriority(PyObject *self, PyObject *args)
3245{
3246 int which, who, prio, retval;
3247
3248 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3249 return NULL;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003250 retval = setpriority(which, who, prio);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003251 if (retval == -1)
3252 return posix_error();
3253 Py_RETURN_NONE;
3254}
3255#endif /* HAVE_SETPRIORITY */
3256
3257
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003258PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003259"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003260Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003261
Barry Warsaw53699e91996-12-10 23:23:01 +00003262static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003263posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003264{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003265#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003266 PyObject *o1, *o2;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003267 wchar_t *w1, *w2;
Victor Stinner8c62be82010-05-06 00:08:46 +00003268 char *p1, *p2;
3269 BOOL result;
3270 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
3271 goto error;
3272 if (!convert_to_unicode(&o1))
3273 goto error;
3274 if (!convert_to_unicode(&o2)) {
3275 Py_DECREF(o1);
3276 goto error;
3277 }
Victor Stinnereb5657a2011-09-30 01:44:27 +02003278 w1 = PyUnicode_AsUnicode(o1);
3279 if (w1 == NULL)
3280 goto error;
3281 w2 = PyUnicode_AsUnicode(o2);
3282 if (w2 == NULL)
3283 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00003284 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02003285 result = MoveFileW(w1, w2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003286 Py_END_ALLOW_THREADS
3287 Py_DECREF(o1);
3288 Py_DECREF(o2);
3289 if (!result)
3290 return win32_error("rename", NULL);
3291 Py_INCREF(Py_None);
3292 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003293error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003294 PyErr_Clear();
3295 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
3296 return NULL;
3297 Py_BEGIN_ALLOW_THREADS
3298 result = MoveFileA(p1, p2);
3299 Py_END_ALLOW_THREADS
3300 if (!result)
3301 return win32_error("rename", NULL);
3302 Py_INCREF(Py_None);
3303 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003304#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003305 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003306#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003307}
3308
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003309
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003310PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003311"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003312Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003313
Barry Warsaw53699e91996-12-10 23:23:01 +00003314static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003315posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003316{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003317#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003318 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003319#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003320 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003321#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003322}
3323
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003324
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003325PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003326"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003327Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003328
Barry Warsaw53699e91996-12-10 23:23:01 +00003329static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003330posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003331{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003332#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003333 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003334#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003335 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003336#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003337}
3338
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003339
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003340#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003341PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003342"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003343Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003344
Barry Warsaw53699e91996-12-10 23:23:01 +00003345static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003346posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003347{
Victor Stinner8c62be82010-05-06 00:08:46 +00003348 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003349#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003350 wchar_t *command;
3351 if (!PyArg_ParseTuple(args, "u:system", &command))
3352 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003353
Victor Stinner8c62be82010-05-06 00:08:46 +00003354 Py_BEGIN_ALLOW_THREADS
3355 sts = _wsystem(command);
3356 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003357#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003358 PyObject *command_obj;
3359 char *command;
3360 if (!PyArg_ParseTuple(args, "O&:system",
3361 PyUnicode_FSConverter, &command_obj))
3362 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003363
Victor Stinner8c62be82010-05-06 00:08:46 +00003364 command = PyBytes_AsString(command_obj);
3365 Py_BEGIN_ALLOW_THREADS
3366 sts = system(command);
3367 Py_END_ALLOW_THREADS
3368 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003369#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003370 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003371}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003372#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003373
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003374
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003375PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003376"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003377Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003378
Barry Warsaw53699e91996-12-10 23:23:01 +00003379static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003380posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003381{
Victor Stinner8c62be82010-05-06 00:08:46 +00003382 int i;
3383 if (!PyArg_ParseTuple(args, "i:umask", &i))
3384 return NULL;
3385 i = (int)umask(i);
3386 if (i < 0)
3387 return posix_error();
3388 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003389}
3390
Brian Curtind40e6f72010-07-08 21:39:08 +00003391#ifdef MS_WINDOWS
3392
3393/* override the default DeleteFileW behavior so that directory
3394symlinks can be removed with this function, the same as with
3395Unix symlinks */
3396BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3397{
3398 WIN32_FILE_ATTRIBUTE_DATA info;
3399 WIN32_FIND_DATAW find_data;
3400 HANDLE find_data_handle;
3401 int is_directory = 0;
3402 int is_link = 0;
3403
3404 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3405 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003406
Brian Curtind40e6f72010-07-08 21:39:08 +00003407 /* Get WIN32_FIND_DATA structure for the path to determine if
3408 it is a symlink */
3409 if(is_directory &&
3410 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3411 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3412
3413 if(find_data_handle != INVALID_HANDLE_VALUE) {
3414 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3415 FindClose(find_data_handle);
3416 }
3417 }
3418 }
3419
3420 if (is_directory && is_link)
3421 return RemoveDirectoryW(lpFileName);
3422
3423 return DeleteFileW(lpFileName);
3424}
3425#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003426
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003427PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003428"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003429Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003430
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003431PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003432"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003433Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003434
Barry Warsaw53699e91996-12-10 23:23:01 +00003435static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003436posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003437{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003438#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003439 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3440 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003441#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003442 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003443#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003444}
3445
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003446
Guido van Rossumb6775db1994-08-01 11:34:53 +00003447#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003448PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003449"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003450Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003451
Barry Warsaw53699e91996-12-10 23:23:01 +00003452static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003453posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003454{
Victor Stinner8c62be82010-05-06 00:08:46 +00003455 struct utsname u;
3456 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003457
Victor Stinner8c62be82010-05-06 00:08:46 +00003458 Py_BEGIN_ALLOW_THREADS
3459 res = uname(&u);
3460 Py_END_ALLOW_THREADS
3461 if (res < 0)
3462 return posix_error();
3463 return Py_BuildValue("(sssss)",
3464 u.sysname,
3465 u.nodename,
3466 u.release,
3467 u.version,
3468 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003469}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003470#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003471
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003472
3473/*
3474 * Classic POSIX utime functions supported microseconds (1m/sec).
3475 * Newer POSIX functions support nanoseconds (1 billion per sec).
3476 * posixmodule now uses the new functions where possible.
3477 * This improves accuracy in many situations, for example shutil.copy2().
3478 *
3479 * The implementation isn't currently sophisticated enough to handle
3480 * a platform where HAVE_UTIMENSAT is true but HAVE_FUTIMENS is false.
3481 * Specifically, posix_futimes() would break.
3482 *
3483 * Supporting such a platform wouldn't be impossible; you'd need two
3484 * extract_time() functions, or make its precision a parameter.
3485 * Since such a platform seems unlikely we haven't bothered.
3486 */
3487#if defined(HAVE_UTIMENSAT)
3488#define EXTRACT_TIME_PRECISION (1e9)
3489#if !defined(HAVE_FUTIMENS)
3490#error You HAVE_UTIMENSAT but not HAVE_FUTIMENS... please see accompanying comment.
3491#endif
3492#else
3493#define EXTRACT_TIME_PRECISION (1e6)
3494#endif
3495
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003496static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003497extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003498{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003499 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003500 if (PyFloat_Check(t)) {
3501 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003502 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003503 if (!intobj)
3504 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003505#if SIZEOF_TIME_T > SIZEOF_LONG
3506 intval = PyLong_AsUnsignedLongLongMask(intobj);
3507#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003508 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003509#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003510 Py_DECREF(intobj);
3511 if (intval == -1 && PyErr_Occurred())
3512 return -1;
3513 *sec = intval;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003514
3515 *usec = (long)((tval - intval) * EXTRACT_TIME_PRECISION);
Victor Stinner8c62be82010-05-06 00:08:46 +00003516 if (*usec < 0)
3517 /* If rounding gave us a negative number,
3518 truncate. */
3519 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003520 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003521 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003522#if SIZEOF_TIME_T > SIZEOF_LONG
3523 intval = PyLong_AsUnsignedLongLongMask(t);
3524#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003525 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003526#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003527 if (intval == -1 && PyErr_Occurred())
3528 return -1;
3529 *sec = intval;
3530 *usec = 0;
3531 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003532}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003533
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003534PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003535"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003536utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003537Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003538second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003539
Barry Warsaw53699e91996-12-10 23:23:01 +00003540static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003541posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003542{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003543#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003544 PyObject *arg;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003545 PyObject *obwpath;
Victor Stinner8c62be82010-05-06 00:08:46 +00003546 wchar_t *wpath = NULL;
3547 PyObject *oapath;
3548 char *apath;
3549 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003550 time_t atimesec, mtimesec;
3551 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003552 FILETIME atime, mtime;
3553 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003554
Victor Stinner8c62be82010-05-06 00:08:46 +00003555 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003556 wpath = PyUnicode_AsUnicode(obwpath);
3557 if (wpath == NULL)
3558 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003559 Py_BEGIN_ALLOW_THREADS
3560 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3561 NULL, OPEN_EXISTING,
3562 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3563 Py_END_ALLOW_THREADS
3564 if (hFile == INVALID_HANDLE_VALUE)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003565 return win32_error_object("utime", obwpath);
3566 }
3567 else {
Victor Stinner8c62be82010-05-06 00:08:46 +00003568 /* Drop the argument parsing error as narrow strings
3569 are also valid. */
3570 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003571
Victor Stinner8c62be82010-05-06 00:08:46 +00003572 if (!PyArg_ParseTuple(args, "O&O:utime",
3573 PyUnicode_FSConverter, &oapath, &arg))
3574 return NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003575
Victor Stinner8c62be82010-05-06 00:08:46 +00003576 apath = PyBytes_AsString(oapath);
3577 Py_BEGIN_ALLOW_THREADS
3578 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3579 NULL, OPEN_EXISTING,
3580 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3581 Py_END_ALLOW_THREADS
3582 if (hFile == INVALID_HANDLE_VALUE) {
3583 win32_error("utime", apath);
3584 Py_DECREF(oapath);
3585 return NULL;
3586 }
3587 Py_DECREF(oapath);
3588 }
3589
3590 if (arg == Py_None) {
3591 SYSTEMTIME now;
3592 GetSystemTime(&now);
3593 if (!SystemTimeToFileTime(&now, &mtime) ||
3594 !SystemTimeToFileTime(&now, &atime)) {
3595 win32_error("utime", NULL);
3596 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003597 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003598 }
3599 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3600 PyErr_SetString(PyExc_TypeError,
3601 "utime() arg 2 must be a tuple (atime, mtime)");
3602 goto done;
3603 }
3604 else {
3605 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3606 &atimesec, &ausec) == -1)
3607 goto done;
3608 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3609 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3610 &mtimesec, &musec) == -1)
3611 goto done;
3612 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3613 }
3614 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3615 /* Avoid putting the file name into the error here,
3616 as that may confuse the user into believing that
3617 something is wrong with the file, when it also
3618 could be the time stamp that gives a problem. */
3619 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003620 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003621 }
3622 Py_INCREF(Py_None);
3623 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003624done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003625 CloseHandle(hFile);
3626 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003627#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003628
Victor Stinner8c62be82010-05-06 00:08:46 +00003629 PyObject *opath;
3630 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003631 time_t atime, mtime;
3632 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003633 int res;
3634 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003635
Victor Stinner8c62be82010-05-06 00:08:46 +00003636 if (!PyArg_ParseTuple(args, "O&O:utime",
3637 PyUnicode_FSConverter, &opath, &arg))
3638 return NULL;
3639 path = PyBytes_AsString(opath);
3640 if (arg == Py_None) {
3641 /* optional time values not given */
3642 Py_BEGIN_ALLOW_THREADS
3643 res = utime(path, NULL);
3644 Py_END_ALLOW_THREADS
3645 }
3646 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3647 PyErr_SetString(PyExc_TypeError,
3648 "utime() arg 2 must be a tuple (atime, mtime)");
3649 Py_DECREF(opath);
3650 return NULL;
3651 }
3652 else {
3653 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3654 &atime, &ausec) == -1) {
3655 Py_DECREF(opath);
3656 return NULL;
3657 }
3658 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3659 &mtime, &musec) == -1) {
3660 Py_DECREF(opath);
3661 return NULL;
3662 }
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003663
3664 Py_BEGIN_ALLOW_THREADS
3665 {
3666#ifdef HAVE_UTIMENSAT
3667 struct timespec buf[2];
3668 buf[0].tv_sec = atime;
3669 buf[0].tv_nsec = ausec;
3670 buf[1].tv_sec = mtime;
3671 buf[1].tv_nsec = musec;
3672 res = utimensat(AT_FDCWD, path, buf, 0);
3673#elif defined(HAVE_UTIMES)
3674 struct timeval buf[2];
3675 buf[0].tv_sec = atime;
Victor Stinner8c62be82010-05-06 00:08:46 +00003676 buf[0].tv_usec = ausec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003677 buf[1].tv_sec = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00003678 buf[1].tv_usec = musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003679 res = utimes(path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003680#elif defined(HAVE_UTIME_H)
3681 /* XXX should define struct utimbuf instead, above */
3682 struct utimbuf buf;
3683 buf.actime = atime;
3684 buf.modtime = mtime;
3685 res = utime(path, &buf);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003686#else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003687 time_t buf[2];
3688 buf[0] = atime;
3689 buf[1] = mtime;
3690 res = utime(path, buf);
3691#endif
3692 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003693 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003694 }
3695 if (res < 0) {
3696 return posix_error_with_allocated_filename(opath);
3697 }
3698 Py_DECREF(opath);
3699 Py_INCREF(Py_None);
3700 return Py_None;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003701#undef UTIME_EXTRACT
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003702#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003703}
3704
Ross Lagerwall7807c352011-03-17 20:20:30 +02003705#ifdef HAVE_FUTIMES
3706PyDoc_STRVAR(posix_futimes__doc__,
3707"futimes(fd, (atime, mtime))\n\
3708futimes(fd, None)\n\n\
3709Set the access and modified time of the file specified by the file\n\
3710descriptor fd to the given values. If the second form is used, set the\n\
3711access and modified times to the current time.");
3712
3713static PyObject *
3714posix_futimes(PyObject *self, PyObject *args)
3715{
3716 int res, fd;
3717 PyObject* arg;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003718 time_t atime, mtime;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003719 long ausec, musec;
3720
3721 if (!PyArg_ParseTuple(args, "iO:futimes", &fd, &arg))
3722 return NULL;
3723
3724 if (arg == Py_None) {
3725 /* optional time values not given */
3726 Py_BEGIN_ALLOW_THREADS
3727 res = futimes(fd, NULL);
3728 Py_END_ALLOW_THREADS
3729 }
3730 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3731 PyErr_SetString(PyExc_TypeError,
3732 "futimes() arg 2 must be a tuple (atime, mtime)");
3733 return NULL;
3734 }
3735 else {
3736 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003737 &atime, &ausec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003738 return NULL;
3739 }
3740 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003741 &mtime, &musec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003742 return NULL;
3743 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003744 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003745 {
3746#ifdef HAVE_FUTIMENS
3747 struct timespec buf[2];
3748 buf[0].tv_sec = atime;
3749 buf[0].tv_nsec = ausec;
3750 buf[1].tv_sec = mtime;
3751 buf[1].tv_nsec = musec;
3752 res = futimens(fd, buf);
3753#else
3754 struct timeval buf[2];
3755 buf[0].tv_sec = atime;
3756 buf[0].tv_usec = ausec;
3757 buf[1].tv_sec = mtime;
3758 buf[1].tv_usec = musec;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003759 res = futimes(fd, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003760#endif
3761 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003762 Py_END_ALLOW_THREADS
3763 }
3764 if (res < 0)
3765 return posix_error();
3766 Py_RETURN_NONE;
3767}
3768#endif
3769
3770#ifdef HAVE_LUTIMES
3771PyDoc_STRVAR(posix_lutimes__doc__,
3772"lutimes(path, (atime, mtime))\n\
3773lutimes(path, None)\n\n\
3774Like utime(), but if path is a symbolic link, it is not dereferenced.");
3775
3776static PyObject *
3777posix_lutimes(PyObject *self, PyObject *args)
3778{
3779 PyObject *opath, *arg;
3780 const char *path;
3781 int res;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003782 time_t atime, mtime;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003783 long ausec, musec;
3784
3785 if (!PyArg_ParseTuple(args, "O&O:lutimes",
3786 PyUnicode_FSConverter, &opath, &arg))
3787 return NULL;
3788 path = PyBytes_AsString(opath);
3789 if (arg == Py_None) {
3790 /* optional time values not given */
3791 Py_BEGIN_ALLOW_THREADS
3792 res = lutimes(path, NULL);
3793 Py_END_ALLOW_THREADS
3794 }
3795 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3796 PyErr_SetString(PyExc_TypeError,
3797 "lutimes() arg 2 must be a tuple (atime, mtime)");
3798 Py_DECREF(opath);
3799 return NULL;
3800 }
3801 else {
3802 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003803 &atime, &ausec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003804 Py_DECREF(opath);
3805 return NULL;
3806 }
3807 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003808 &mtime, &musec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003809 Py_DECREF(opath);
3810 return NULL;
3811 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003812 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003813 {
3814#ifdef HAVE_UTIMENSAT
3815 struct timespec buf[2];
3816 buf[0].tv_sec = atime;
3817 buf[0].tv_nsec = ausec;
3818 buf[1].tv_sec = mtime;
3819 buf[1].tv_nsec = musec;
3820 res = utimensat(AT_FDCWD, path, buf, AT_SYMLINK_NOFOLLOW);
3821#else
3822 struct timeval buf[2];
3823 buf[0].tv_sec = atime;
3824 buf[0].tv_usec = ausec;
3825 buf[1].tv_sec = mtime;
3826 buf[1].tv_usec = musec;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003827 res = lutimes(path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003828#endif
3829 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003830 Py_END_ALLOW_THREADS
3831 }
3832 Py_DECREF(opath);
3833 if (res < 0)
3834 return posix_error();
3835 Py_RETURN_NONE;
3836}
3837#endif
3838
3839#ifdef HAVE_FUTIMENS
3840PyDoc_STRVAR(posix_futimens__doc__,
3841"futimens(fd, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec))\n\
3842futimens(fd, None, None)\n\n\
3843Updates the timestamps of a file specified by the file descriptor fd, with\n\
3844nanosecond precision.\n\
3845The second form sets atime and mtime to the current time.\n\
3846If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
3847current time.\n\
3848If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
3849
3850static PyObject *
3851posix_futimens(PyObject *self, PyObject *args)
3852{
3853 int res, fd;
3854 PyObject *atime, *mtime;
3855 struct timespec buf[2];
3856
3857 if (!PyArg_ParseTuple(args, "iOO:futimens",
3858 &fd, &atime, &mtime))
3859 return NULL;
3860 if (atime == Py_None && mtime == Py_None) {
3861 /* optional time values not given */
3862 Py_BEGIN_ALLOW_THREADS
3863 res = futimens(fd, NULL);
3864 Py_END_ALLOW_THREADS
3865 }
3866 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
3867 PyErr_SetString(PyExc_TypeError,
3868 "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)");
3869 return NULL;
3870 }
3871 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
3872 PyErr_SetString(PyExc_TypeError,
3873 "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)");
3874 return NULL;
3875 }
3876 else {
3877 if (!PyArg_ParseTuple(atime, "ll:futimens",
3878 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
3879 return NULL;
3880 }
3881 if (!PyArg_ParseTuple(mtime, "ll:futimens",
3882 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
3883 return NULL;
3884 }
3885 Py_BEGIN_ALLOW_THREADS
3886 res = futimens(fd, buf);
3887 Py_END_ALLOW_THREADS
3888 }
3889 if (res < 0)
3890 return posix_error();
3891 Py_RETURN_NONE;
3892}
3893#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003894
Guido van Rossum3b066191991-06-04 19:40:25 +00003895/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003896
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003897PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003898"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003899Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003900
Barry Warsaw53699e91996-12-10 23:23:01 +00003901static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003902posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003903{
Victor Stinner8c62be82010-05-06 00:08:46 +00003904 int sts;
3905 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3906 return NULL;
3907 _exit(sts);
3908 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003909}
3910
Martin v. Löwis114619e2002-10-07 06:44:21 +00003911#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3912static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003913free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003914{
Victor Stinner8c62be82010-05-06 00:08:46 +00003915 Py_ssize_t i;
3916 for (i = 0; i < count; i++)
3917 PyMem_Free(array[i]);
3918 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003919}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003920
Antoine Pitrou69f71142009-05-24 21:25:49 +00003921static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003922int fsconvert_strdup(PyObject *o, char**out)
3923{
Victor Stinner8c62be82010-05-06 00:08:46 +00003924 PyObject *bytes;
3925 Py_ssize_t size;
3926 if (!PyUnicode_FSConverter(o, &bytes))
3927 return 0;
3928 size = PyBytes_GET_SIZE(bytes);
3929 *out = PyMem_Malloc(size+1);
3930 if (!*out)
3931 return 0;
3932 memcpy(*out, PyBytes_AsString(bytes), size+1);
3933 Py_DECREF(bytes);
3934 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003935}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003936#endif
3937
Ross Lagerwall7807c352011-03-17 20:20:30 +02003938#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00003939static char**
3940parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3941{
Victor Stinner8c62be82010-05-06 00:08:46 +00003942 char **envlist;
3943 Py_ssize_t i, pos, envc;
3944 PyObject *keys=NULL, *vals=NULL;
3945 PyObject *key, *val, *key2, *val2;
3946 char *p, *k, *v;
3947 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003948
Victor Stinner8c62be82010-05-06 00:08:46 +00003949 i = PyMapping_Size(env);
3950 if (i < 0)
3951 return NULL;
3952 envlist = PyMem_NEW(char *, i + 1);
3953 if (envlist == NULL) {
3954 PyErr_NoMemory();
3955 return NULL;
3956 }
3957 envc = 0;
3958 keys = PyMapping_Keys(env);
3959 vals = PyMapping_Values(env);
3960 if (!keys || !vals)
3961 goto error;
3962 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3963 PyErr_Format(PyExc_TypeError,
3964 "env.keys() or env.values() is not a list");
3965 goto error;
3966 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003967
Victor Stinner8c62be82010-05-06 00:08:46 +00003968 for (pos = 0; pos < i; pos++) {
3969 key = PyList_GetItem(keys, pos);
3970 val = PyList_GetItem(vals, pos);
3971 if (!key || !val)
3972 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003973
Victor Stinner8c62be82010-05-06 00:08:46 +00003974 if (PyUnicode_FSConverter(key, &key2) == 0)
3975 goto error;
3976 if (PyUnicode_FSConverter(val, &val2) == 0) {
3977 Py_DECREF(key2);
3978 goto error;
3979 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003980
3981#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003982 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3983 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003984#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003985 k = PyBytes_AsString(key2);
3986 v = PyBytes_AsString(val2);
3987 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003988
Victor Stinner8c62be82010-05-06 00:08:46 +00003989 p = PyMem_NEW(char, len);
3990 if (p == NULL) {
3991 PyErr_NoMemory();
3992 Py_DECREF(key2);
3993 Py_DECREF(val2);
3994 goto error;
3995 }
3996 PyOS_snprintf(p, len, "%s=%s", k, v);
3997 envlist[envc++] = p;
3998 Py_DECREF(key2);
3999 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004000#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00004001 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004002#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004003 }
4004 Py_DECREF(vals);
4005 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004006
Victor Stinner8c62be82010-05-06 00:08:46 +00004007 envlist[envc] = 0;
4008 *envc_ptr = envc;
4009 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004010
4011error:
Victor Stinner8c62be82010-05-06 00:08:46 +00004012 Py_XDECREF(keys);
4013 Py_XDECREF(vals);
4014 while (--envc >= 0)
4015 PyMem_DEL(envlist[envc]);
4016 PyMem_DEL(envlist);
4017 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004018}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004019
Ross Lagerwall7807c352011-03-17 20:20:30 +02004020static char**
4021parse_arglist(PyObject* argv, Py_ssize_t *argc)
4022{
4023 int i;
4024 char **argvlist = PyMem_NEW(char *, *argc+1);
4025 if (argvlist == NULL) {
4026 PyErr_NoMemory();
4027 return NULL;
4028 }
4029 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004030 PyObject* item = PySequence_ITEM(argv, i);
4031 if (item == NULL)
4032 goto fail;
4033 if (!fsconvert_strdup(item, &argvlist[i])) {
4034 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004035 goto fail;
4036 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004037 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004038 }
4039 argvlist[*argc] = NULL;
4040 return argvlist;
4041fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004042 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004043 free_string_array(argvlist, *argc);
4044 return NULL;
4045}
4046#endif
4047
4048#ifdef HAVE_EXECV
4049PyDoc_STRVAR(posix_execv__doc__,
4050"execv(path, args)\n\n\
4051Execute an executable path with arguments, replacing current process.\n\
4052\n\
4053 path: path of executable file\n\
4054 args: tuple or list of strings");
4055
4056static PyObject *
4057posix_execv(PyObject *self, PyObject *args)
4058{
4059 PyObject *opath;
4060 char *path;
4061 PyObject *argv;
4062 char **argvlist;
4063 Py_ssize_t argc;
4064
4065 /* execv has two arguments: (path, argv), where
4066 argv is a list or tuple of strings. */
4067
4068 if (!PyArg_ParseTuple(args, "O&O:execv",
4069 PyUnicode_FSConverter,
4070 &opath, &argv))
4071 return NULL;
4072 path = PyBytes_AsString(opath);
4073 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4074 PyErr_SetString(PyExc_TypeError,
4075 "execv() arg 2 must be a tuple or list");
4076 Py_DECREF(opath);
4077 return NULL;
4078 }
4079 argc = PySequence_Size(argv);
4080 if (argc < 1) {
4081 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
4082 Py_DECREF(opath);
4083 return NULL;
4084 }
4085
4086 argvlist = parse_arglist(argv, &argc);
4087 if (argvlist == NULL) {
4088 Py_DECREF(opath);
4089 return NULL;
4090 }
4091
4092 execv(path, argvlist);
4093
4094 /* If we get here it's definitely an error */
4095
4096 free_string_array(argvlist, argc);
4097 Py_DECREF(opath);
4098 return posix_error();
4099}
4100
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004101PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004102"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004103Execute a path with arguments and environment, replacing current process.\n\
4104\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004105 path: path of executable file\n\
4106 args: tuple or list of arguments\n\
4107 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004108
Barry Warsaw53699e91996-12-10 23:23:01 +00004109static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004110posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004111{
Victor Stinner8c62be82010-05-06 00:08:46 +00004112 PyObject *opath;
4113 char *path;
4114 PyObject *argv, *env;
4115 char **argvlist;
4116 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004117 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004118
Victor Stinner8c62be82010-05-06 00:08:46 +00004119 /* execve has three arguments: (path, argv, env), where
4120 argv is a list or tuple of strings and env is a dictionary
4121 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004122
Victor Stinner8c62be82010-05-06 00:08:46 +00004123 if (!PyArg_ParseTuple(args, "O&OO:execve",
4124 PyUnicode_FSConverter,
4125 &opath, &argv, &env))
4126 return NULL;
4127 path = PyBytes_AsString(opath);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004128 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004129 PyErr_SetString(PyExc_TypeError,
4130 "execve() arg 2 must be a tuple or list");
4131 goto fail_0;
4132 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02004133 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00004134 if (!PyMapping_Check(env)) {
4135 PyErr_SetString(PyExc_TypeError,
4136 "execve() arg 3 must be a mapping object");
4137 goto fail_0;
4138 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004139
Ross Lagerwall7807c352011-03-17 20:20:30 +02004140 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004141 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004142 goto fail_0;
4143 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004144
Victor Stinner8c62be82010-05-06 00:08:46 +00004145 envlist = parse_envlist(env, &envc);
4146 if (envlist == NULL)
4147 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004148
Victor Stinner8c62be82010-05-06 00:08:46 +00004149 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00004150
Victor Stinner8c62be82010-05-06 00:08:46 +00004151 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004152
Victor Stinner8c62be82010-05-06 00:08:46 +00004153 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004154
Victor Stinner8c62be82010-05-06 00:08:46 +00004155 while (--envc >= 0)
4156 PyMem_DEL(envlist[envc]);
4157 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004158 fail_1:
Ross Lagerwall7807c352011-03-17 20:20:30 +02004159 free_string_array(argvlist, argc);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004160 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004161 Py_DECREF(opath);
4162 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004163}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004164#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004165
Ross Lagerwall7807c352011-03-17 20:20:30 +02004166#ifdef HAVE_FEXECVE
4167PyDoc_STRVAR(posix_fexecve__doc__,
4168"fexecve(fd, args, env)\n\n\
4169Execute the program specified by a file descriptor with arguments and\n\
4170environment, replacing the current process.\n\
4171\n\
4172 fd: file descriptor of executable\n\
4173 args: tuple or list of arguments\n\
4174 env: dictionary of strings mapping to strings");
4175
4176static PyObject *
4177posix_fexecve(PyObject *self, PyObject *args)
4178{
4179 int fd;
4180 PyObject *argv, *env;
4181 char **argvlist;
4182 char **envlist;
4183 Py_ssize_t argc, envc;
4184
4185 if (!PyArg_ParseTuple(args, "iOO:fexecve",
4186 &fd, &argv, &env))
4187 return NULL;
4188 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4189 PyErr_SetString(PyExc_TypeError,
4190 "fexecve() arg 2 must be a tuple or list");
4191 return NULL;
4192 }
4193 argc = PySequence_Size(argv);
4194 if (!PyMapping_Check(env)) {
4195 PyErr_SetString(PyExc_TypeError,
4196 "fexecve() arg 3 must be a mapping object");
4197 return NULL;
4198 }
4199
4200 argvlist = parse_arglist(argv, &argc);
4201 if (argvlist == NULL)
4202 return NULL;
4203
4204 envlist = parse_envlist(env, &envc);
4205 if (envlist == NULL)
4206 goto fail;
4207
4208 fexecve(fd, argvlist, envlist);
4209
4210 /* If we get here it's definitely an error */
4211
4212 (void) posix_error();
4213
4214 while (--envc >= 0)
4215 PyMem_DEL(envlist[envc]);
4216 PyMem_DEL(envlist);
4217 fail:
4218 free_string_array(argvlist, argc);
4219 return NULL;
4220}
4221#endif /* HAVE_FEXECVE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004222
Guido van Rossuma1065681999-01-25 23:20:23 +00004223#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004224PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004225"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004226Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004227\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004228 mode: mode of process creation\n\
4229 path: path of executable file\n\
4230 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004231
4232static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004233posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004234{
Victor Stinner8c62be82010-05-06 00:08:46 +00004235 PyObject *opath;
4236 char *path;
4237 PyObject *argv;
4238 char **argvlist;
4239 int mode, i;
4240 Py_ssize_t argc;
4241 Py_intptr_t spawnval;
4242 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00004243
Victor Stinner8c62be82010-05-06 00:08:46 +00004244 /* spawnv has three arguments: (mode, path, argv), where
4245 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004246
Victor Stinner8c62be82010-05-06 00:08:46 +00004247 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
4248 PyUnicode_FSConverter,
4249 &opath, &argv))
4250 return NULL;
4251 path = PyBytes_AsString(opath);
4252 if (PyList_Check(argv)) {
4253 argc = PyList_Size(argv);
4254 getitem = PyList_GetItem;
4255 }
4256 else if (PyTuple_Check(argv)) {
4257 argc = PyTuple_Size(argv);
4258 getitem = PyTuple_GetItem;
4259 }
4260 else {
4261 PyErr_SetString(PyExc_TypeError,
4262 "spawnv() arg 2 must be a tuple or list");
4263 Py_DECREF(opath);
4264 return NULL;
4265 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004266
Victor Stinner8c62be82010-05-06 00:08:46 +00004267 argvlist = PyMem_NEW(char *, argc+1);
4268 if (argvlist == NULL) {
4269 Py_DECREF(opath);
4270 return PyErr_NoMemory();
4271 }
4272 for (i = 0; i < argc; i++) {
4273 if (!fsconvert_strdup((*getitem)(argv, i),
4274 &argvlist[i])) {
4275 free_string_array(argvlist, i);
4276 PyErr_SetString(
4277 PyExc_TypeError,
4278 "spawnv() arg 2 must contain only strings");
4279 Py_DECREF(opath);
4280 return NULL;
4281 }
4282 }
4283 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004284
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004285#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004286 Py_BEGIN_ALLOW_THREADS
4287 spawnval = spawnv(mode, path, argvlist);
4288 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004289#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004290 if (mode == _OLD_P_OVERLAY)
4291 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00004292
Victor Stinner8c62be82010-05-06 00:08:46 +00004293 Py_BEGIN_ALLOW_THREADS
4294 spawnval = _spawnv(mode, path, argvlist);
4295 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004296#endif
Tim Peters5aa91602002-01-30 05:46:57 +00004297
Victor Stinner8c62be82010-05-06 00:08:46 +00004298 free_string_array(argvlist, argc);
4299 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00004300
Victor Stinner8c62be82010-05-06 00:08:46 +00004301 if (spawnval == -1)
4302 return posix_error();
4303 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004304#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004305 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004306#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004307 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004308#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004309}
4310
4311
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004312PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004313"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004314Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004315\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004316 mode: mode of process creation\n\
4317 path: path of executable file\n\
4318 args: tuple or list of arguments\n\
4319 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004320
4321static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004322posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004323{
Victor Stinner8c62be82010-05-06 00:08:46 +00004324 PyObject *opath;
4325 char *path;
4326 PyObject *argv, *env;
4327 char **argvlist;
4328 char **envlist;
4329 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004330 int mode;
4331 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004332 Py_intptr_t spawnval;
4333 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4334 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00004335
Victor Stinner8c62be82010-05-06 00:08:46 +00004336 /* spawnve has four arguments: (mode, path, argv, env), where
4337 argv is a list or tuple of strings and env is a dictionary
4338 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004339
Victor Stinner8c62be82010-05-06 00:08:46 +00004340 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
4341 PyUnicode_FSConverter,
4342 &opath, &argv, &env))
4343 return NULL;
4344 path = PyBytes_AsString(opath);
4345 if (PyList_Check(argv)) {
4346 argc = PyList_Size(argv);
4347 getitem = PyList_GetItem;
4348 }
4349 else if (PyTuple_Check(argv)) {
4350 argc = PyTuple_Size(argv);
4351 getitem = PyTuple_GetItem;
4352 }
4353 else {
4354 PyErr_SetString(PyExc_TypeError,
4355 "spawnve() arg 2 must be a tuple or list");
4356 goto fail_0;
4357 }
4358 if (!PyMapping_Check(env)) {
4359 PyErr_SetString(PyExc_TypeError,
4360 "spawnve() arg 3 must be a mapping object");
4361 goto fail_0;
4362 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004363
Victor Stinner8c62be82010-05-06 00:08:46 +00004364 argvlist = PyMem_NEW(char *, argc+1);
4365 if (argvlist == NULL) {
4366 PyErr_NoMemory();
4367 goto fail_0;
4368 }
4369 for (i = 0; i < argc; i++) {
4370 if (!fsconvert_strdup((*getitem)(argv, i),
4371 &argvlist[i]))
4372 {
4373 lastarg = i;
4374 goto fail_1;
4375 }
4376 }
4377 lastarg = argc;
4378 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004379
Victor Stinner8c62be82010-05-06 00:08:46 +00004380 envlist = parse_envlist(env, &envc);
4381 if (envlist == NULL)
4382 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00004383
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004384#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004385 Py_BEGIN_ALLOW_THREADS
4386 spawnval = spawnve(mode, path, argvlist, envlist);
4387 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004388#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004389 if (mode == _OLD_P_OVERLAY)
4390 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00004391
Victor Stinner8c62be82010-05-06 00:08:46 +00004392 Py_BEGIN_ALLOW_THREADS
4393 spawnval = _spawnve(mode, path, argvlist, envlist);
4394 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004395#endif
Tim Peters25059d32001-12-07 20:35:43 +00004396
Victor Stinner8c62be82010-05-06 00:08:46 +00004397 if (spawnval == -1)
4398 (void) posix_error();
4399 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004400#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004401 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004402#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004403 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004404#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004405
Victor Stinner8c62be82010-05-06 00:08:46 +00004406 while (--envc >= 0)
4407 PyMem_DEL(envlist[envc]);
4408 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004409 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004410 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004411 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004412 Py_DECREF(opath);
4413 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00004414}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004415
4416/* OS/2 supports spawnvp & spawnvpe natively */
4417#if defined(PYOS_OS2)
4418PyDoc_STRVAR(posix_spawnvp__doc__,
4419"spawnvp(mode, file, args)\n\n\
4420Execute the program 'file' in a new process, using the environment\n\
4421search path to find the file.\n\
4422\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004423 mode: mode of process creation\n\
4424 file: executable file name\n\
4425 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004426
4427static PyObject *
4428posix_spawnvp(PyObject *self, PyObject *args)
4429{
Victor Stinner8c62be82010-05-06 00:08:46 +00004430 PyObject *opath;
4431 char *path;
4432 PyObject *argv;
4433 char **argvlist;
4434 int mode, i, argc;
4435 Py_intptr_t spawnval;
4436 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004437
Victor Stinner8c62be82010-05-06 00:08:46 +00004438 /* spawnvp has three arguments: (mode, path, argv), where
4439 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004440
Victor Stinner8c62be82010-05-06 00:08:46 +00004441 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
4442 PyUnicode_FSConverter,
4443 &opath, &argv))
4444 return NULL;
4445 path = PyBytes_AsString(opath);
4446 if (PyList_Check(argv)) {
4447 argc = PyList_Size(argv);
4448 getitem = PyList_GetItem;
4449 }
4450 else if (PyTuple_Check(argv)) {
4451 argc = PyTuple_Size(argv);
4452 getitem = PyTuple_GetItem;
4453 }
4454 else {
4455 PyErr_SetString(PyExc_TypeError,
4456 "spawnvp() arg 2 must be a tuple or list");
4457 Py_DECREF(opath);
4458 return NULL;
4459 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004460
Victor Stinner8c62be82010-05-06 00:08:46 +00004461 argvlist = PyMem_NEW(char *, argc+1);
4462 if (argvlist == NULL) {
4463 Py_DECREF(opath);
4464 return PyErr_NoMemory();
4465 }
4466 for (i = 0; i < argc; i++) {
4467 if (!fsconvert_strdup((*getitem)(argv, i),
4468 &argvlist[i])) {
4469 free_string_array(argvlist, i);
4470 PyErr_SetString(
4471 PyExc_TypeError,
4472 "spawnvp() arg 2 must contain only strings");
4473 Py_DECREF(opath);
4474 return NULL;
4475 }
4476 }
4477 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004478
Victor Stinner8c62be82010-05-06 00:08:46 +00004479 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004480#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004481 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004482#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004483 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004484#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004485 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004486
Victor Stinner8c62be82010-05-06 00:08:46 +00004487 free_string_array(argvlist, argc);
4488 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004489
Victor Stinner8c62be82010-05-06 00:08:46 +00004490 if (spawnval == -1)
4491 return posix_error();
4492 else
4493 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004494}
4495
4496
4497PyDoc_STRVAR(posix_spawnvpe__doc__,
4498"spawnvpe(mode, file, args, env)\n\n\
4499Execute the program 'file' in a new process, using the environment\n\
4500search path to find the file.\n\
4501\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004502 mode: mode of process creation\n\
4503 file: executable file name\n\
4504 args: tuple or list of arguments\n\
4505 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004506
4507static PyObject *
4508posix_spawnvpe(PyObject *self, PyObject *args)
4509{
Victor Stinner8c62be82010-05-06 00:08:46 +00004510 PyObject *opath
4511 char *path;
4512 PyObject *argv, *env;
4513 char **argvlist;
4514 char **envlist;
4515 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004516 int mode;
4517 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004518 Py_intptr_t spawnval;
4519 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4520 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004521
Victor Stinner8c62be82010-05-06 00:08:46 +00004522 /* spawnvpe has four arguments: (mode, path, argv, env), where
4523 argv is a list or tuple of strings and env is a dictionary
4524 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004525
Victor Stinner8c62be82010-05-06 00:08:46 +00004526 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4527 PyUnicode_FSConverter,
4528 &opath, &argv, &env))
4529 return NULL;
4530 path = PyBytes_AsString(opath);
4531 if (PyList_Check(argv)) {
4532 argc = PyList_Size(argv);
4533 getitem = PyList_GetItem;
4534 }
4535 else if (PyTuple_Check(argv)) {
4536 argc = PyTuple_Size(argv);
4537 getitem = PyTuple_GetItem;
4538 }
4539 else {
4540 PyErr_SetString(PyExc_TypeError,
4541 "spawnvpe() arg 2 must be a tuple or list");
4542 goto fail_0;
4543 }
4544 if (!PyMapping_Check(env)) {
4545 PyErr_SetString(PyExc_TypeError,
4546 "spawnvpe() arg 3 must be a mapping object");
4547 goto fail_0;
4548 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004549
Victor Stinner8c62be82010-05-06 00:08:46 +00004550 argvlist = PyMem_NEW(char *, argc+1);
4551 if (argvlist == NULL) {
4552 PyErr_NoMemory();
4553 goto fail_0;
4554 }
4555 for (i = 0; i < argc; i++) {
4556 if (!fsconvert_strdup((*getitem)(argv, i),
4557 &argvlist[i]))
4558 {
4559 lastarg = i;
4560 goto fail_1;
4561 }
4562 }
4563 lastarg = argc;
4564 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004565
Victor Stinner8c62be82010-05-06 00:08:46 +00004566 envlist = parse_envlist(env, &envc);
4567 if (envlist == NULL)
4568 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004569
Victor Stinner8c62be82010-05-06 00:08:46 +00004570 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004571#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004572 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004573#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004574 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004575#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004576 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004577
Victor Stinner8c62be82010-05-06 00:08:46 +00004578 if (spawnval == -1)
4579 (void) posix_error();
4580 else
4581 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004582
Victor Stinner8c62be82010-05-06 00:08:46 +00004583 while (--envc >= 0)
4584 PyMem_DEL(envlist[envc]);
4585 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004586 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004587 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004588 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004589 Py_DECREF(opath);
4590 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004591}
4592#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004593#endif /* HAVE_SPAWNV */
4594
4595
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004596#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004597PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004598"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004599Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4600\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004601Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004602
4603static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004604posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004605{
Victor Stinner8c62be82010-05-06 00:08:46 +00004606 pid_t pid;
4607 int result = 0;
4608 _PyImport_AcquireLock();
4609 pid = fork1();
4610 if (pid == 0) {
4611 /* child: this clobbers and resets the import lock. */
4612 PyOS_AfterFork();
4613 } else {
4614 /* parent: release the import lock. */
4615 result = _PyImport_ReleaseLock();
4616 }
4617 if (pid == -1)
4618 return posix_error();
4619 if (result < 0) {
4620 /* Don't clobber the OSError if the fork failed. */
4621 PyErr_SetString(PyExc_RuntimeError,
4622 "not holding the import lock");
4623 return NULL;
4624 }
4625 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004626}
4627#endif
4628
4629
Guido van Rossumad0ee831995-03-01 10:34:45 +00004630#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004631PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004632"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004633Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004634Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004635
Barry Warsaw53699e91996-12-10 23:23:01 +00004636static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004637posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004638{
Victor Stinner8c62be82010-05-06 00:08:46 +00004639 pid_t pid;
4640 int result = 0;
4641 _PyImport_AcquireLock();
4642 pid = fork();
4643 if (pid == 0) {
4644 /* child: this clobbers and resets the import lock. */
4645 PyOS_AfterFork();
4646 } else {
4647 /* parent: release the import lock. */
4648 result = _PyImport_ReleaseLock();
4649 }
4650 if (pid == -1)
4651 return posix_error();
4652 if (result < 0) {
4653 /* Don't clobber the OSError if the fork failed. */
4654 PyErr_SetString(PyExc_RuntimeError,
4655 "not holding the import lock");
4656 return NULL;
4657 }
4658 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004659}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004660#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004661
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004662#ifdef HAVE_SCHED_H
4663
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02004664#ifdef HAVE_SCHED_GET_PRIORITY_MAX
4665
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004666PyDoc_STRVAR(posix_sched_get_priority_max__doc__,
4667"sched_get_priority_max(policy)\n\n\
4668Get the maximum scheduling priority for *policy*.");
4669
4670static PyObject *
4671posix_sched_get_priority_max(PyObject *self, PyObject *args)
4672{
4673 int policy, max;
4674
4675 if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy))
4676 return NULL;
4677 max = sched_get_priority_max(policy);
4678 if (max < 0)
4679 return posix_error();
4680 return PyLong_FromLong(max);
4681}
4682
4683PyDoc_STRVAR(posix_sched_get_priority_min__doc__,
4684"sched_get_priority_min(policy)\n\n\
4685Get the minimum scheduling priority for *policy*.");
4686
4687static PyObject *
4688posix_sched_get_priority_min(PyObject *self, PyObject *args)
4689{
4690 int policy, min;
4691
4692 if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy))
4693 return NULL;
4694 min = sched_get_priority_min(policy);
4695 if (min < 0)
4696 return posix_error();
4697 return PyLong_FromLong(min);
4698}
4699
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02004700#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
4701
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004702#ifdef HAVE_SCHED_SETSCHEDULER
4703
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004704PyDoc_STRVAR(posix_sched_getscheduler__doc__,
4705"sched_getscheduler(pid)\n\n\
4706Get the scheduling policy for the process with a PID of *pid*.\n\
4707Passing a PID of 0 returns the scheduling policy for the calling process.");
4708
4709static PyObject *
4710posix_sched_getscheduler(PyObject *self, PyObject *args)
4711{
4712 pid_t pid;
4713 int policy;
4714
4715 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid))
4716 return NULL;
4717 policy = sched_getscheduler(pid);
4718 if (policy < 0)
4719 return posix_error();
4720 return PyLong_FromLong(policy);
4721}
4722
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004723#endif
4724
4725#if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM)
4726
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004727static PyObject *
4728sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4729{
4730 PyObject *res, *priority;
Benjamin Peterson4e36d5a2011-08-02 19:56:11 -05004731 static char *kwlist[] = {"sched_priority", NULL};
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004732
4733 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority))
4734 return NULL;
4735 res = PyStructSequence_New(type);
4736 if (!res)
4737 return NULL;
4738 Py_INCREF(priority);
4739 PyStructSequence_SET_ITEM(res, 0, priority);
4740 return res;
4741}
4742
4743PyDoc_STRVAR(sched_param__doc__,
4744"sched_param(sched_priority): A scheduling parameter.\n\n\
4745Current has only one field: sched_priority");
4746
4747static PyStructSequence_Field sched_param_fields[] = {
4748 {"sched_priority", "the scheduling priority"},
4749 {0}
4750};
4751
4752static PyStructSequence_Desc sched_param_desc = {
4753 "sched_param", /* name */
4754 sched_param__doc__, /* doc */
4755 sched_param_fields,
4756 1
4757};
4758
4759static int
4760convert_sched_param(PyObject *param, struct sched_param *res)
4761{
4762 long priority;
4763
4764 if (Py_TYPE(param) != &SchedParamType) {
4765 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
4766 return 0;
4767 }
4768 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
4769 if (priority == -1 && PyErr_Occurred())
4770 return 0;
4771 if (priority > INT_MAX || priority < INT_MIN) {
4772 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
4773 return 0;
4774 }
4775 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
4776 return 1;
4777}
4778
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004779#endif
4780
4781#ifdef HAVE_SCHED_SETSCHEDULER
4782
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004783PyDoc_STRVAR(posix_sched_setscheduler__doc__,
4784"sched_setscheduler(pid, policy, param)\n\n\
4785Set the scheduling policy, *policy*, for *pid*.\n\
4786If *pid* is 0, the calling process is changed.\n\
4787*param* is an instance of sched_param.");
4788
4789static PyObject *
4790posix_sched_setscheduler(PyObject *self, PyObject *args)
4791{
4792 pid_t pid;
4793 int policy;
4794 struct sched_param param;
4795
4796 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler",
4797 &pid, &policy, &convert_sched_param, &param))
4798 return NULL;
Jesus Cea9c822272011-09-10 01:40:52 +02004799
4800 /*
Jesus Cea54b01492011-09-10 01:53:19 +02004801 ** sched_setscheduler() returns 0 in Linux, but the previous
4802 ** scheduling policy under Solaris/Illumos, and others.
4803 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02004804 */
4805 if (sched_setscheduler(pid, policy, &param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004806 return posix_error();
4807 Py_RETURN_NONE;
4808}
4809
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004810#endif
4811
4812#ifdef HAVE_SCHED_SETPARAM
4813
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004814PyDoc_STRVAR(posix_sched_getparam__doc__,
4815"sched_getparam(pid) -> sched_param\n\n\
4816Returns scheduling parameters for the process with *pid* as an instance of the\n\
4817sched_param class. A PID of 0 means the calling process.");
4818
4819static PyObject *
4820posix_sched_getparam(PyObject *self, PyObject *args)
4821{
4822 pid_t pid;
4823 struct sched_param param;
4824 PyObject *res, *priority;
4825
4826 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid))
4827 return NULL;
4828 if (sched_getparam(pid, &param))
4829 return posix_error();
4830 res = PyStructSequence_New(&SchedParamType);
4831 if (!res)
4832 return NULL;
4833 priority = PyLong_FromLong(param.sched_priority);
4834 if (!priority) {
4835 Py_DECREF(res);
4836 return NULL;
4837 }
4838 PyStructSequence_SET_ITEM(res, 0, priority);
4839 return res;
4840}
4841
4842PyDoc_STRVAR(posix_sched_setparam__doc__,
4843"sched_setparam(pid, param)\n\n\
4844Set scheduling parameters for a process with PID *pid*.\n\
4845A PID of 0 means the calling process.");
4846
4847static PyObject *
4848posix_sched_setparam(PyObject *self, PyObject *args)
4849{
4850 pid_t pid;
4851 struct sched_param param;
4852
4853 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam",
4854 &pid, &convert_sched_param, &param))
4855 return NULL;
4856 if (sched_setparam(pid, &param))
4857 return posix_error();
4858 Py_RETURN_NONE;
4859}
4860
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004861#endif
4862
4863#ifdef HAVE_SCHED_RR_GET_INTERVAL
4864
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004865PyDoc_STRVAR(posix_sched_rr_get_interval__doc__,
4866"sched_rr_get_interval(pid) -> float\n\n\
4867Return the round-robin quantum for the process with PID *pid* in seconds.");
4868
4869static PyObject *
4870posix_sched_rr_get_interval(PyObject *self, PyObject *args)
4871{
4872 pid_t pid;
4873 struct timespec interval;
4874
4875 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid))
4876 return NULL;
4877 if (sched_rr_get_interval(pid, &interval))
4878 return posix_error();
4879 return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec);
4880}
4881
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004882#endif
4883
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004884PyDoc_STRVAR(posix_sched_yield__doc__,
4885"sched_yield()\n\n\
4886Voluntarily relinquish the CPU.");
4887
4888static PyObject *
4889posix_sched_yield(PyObject *self, PyObject *noargs)
4890{
4891 if (sched_yield())
4892 return posix_error();
4893 Py_RETURN_NONE;
4894}
4895
Benjamin Peterson2740af82011-08-02 17:41:34 -05004896#ifdef HAVE_SCHED_SETAFFINITY
4897
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004898typedef struct {
4899 PyObject_HEAD;
4900 Py_ssize_t size;
4901 int ncpus;
4902 cpu_set_t *set;
4903} Py_cpu_set;
4904
4905static PyTypeObject cpu_set_type;
4906
4907static void
4908cpu_set_dealloc(Py_cpu_set *set)
4909{
4910 assert(set->set);
4911 CPU_FREE(set->set);
4912 Py_TYPE(set)->tp_free(set);
4913}
4914
4915static Py_cpu_set *
4916make_new_cpu_set(PyTypeObject *type, Py_ssize_t size)
4917{
4918 Py_cpu_set *set;
4919
4920 if (size < 0) {
4921 PyErr_SetString(PyExc_ValueError, "negative size");
4922 return NULL;
4923 }
4924 set = (Py_cpu_set *)type->tp_alloc(type, 0);
4925 if (!set)
4926 return NULL;
4927 set->ncpus = size;
4928 set->size = CPU_ALLOC_SIZE(size);
4929 set->set = CPU_ALLOC(size);
4930 if (!set->set) {
4931 type->tp_free(set);
4932 PyErr_NoMemory();
4933 return NULL;
4934 }
4935 CPU_ZERO_S(set->size, set->set);
4936 return set;
4937}
4938
4939static PyObject *
4940cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4941{
4942 int size;
4943
4944 if (!_PyArg_NoKeywords("cpu_set()", kwargs) ||
4945 !PyArg_ParseTuple(args, "i:cpu_set", &size))
4946 return NULL;
4947 return (PyObject *)make_new_cpu_set(type, size);
4948}
4949
4950static PyObject *
4951cpu_set_repr(Py_cpu_set *set)
4952{
4953 return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus);
Victor Stinner63941882011-09-29 00:42:28 +02004954}
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004955
4956static Py_ssize_t
4957cpu_set_len(Py_cpu_set *set)
4958{
4959 return set->ncpus;
4960}
4961
4962static int
4963_get_cpu(Py_cpu_set *set, const char *requester, PyObject *args)
4964{
4965 int cpu;
4966 if (!PyArg_ParseTuple(args, requester, &cpu))
4967 return -1;
4968 if (cpu < 0) {
4969 PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid");
4970 return -1;
4971 }
4972 if (cpu >= set->ncpus) {
4973 PyErr_SetString(PyExc_ValueError, "cpu too large for set");
4974 return -1;
4975 }
4976 return cpu;
4977}
4978
4979PyDoc_STRVAR(cpu_set_set_doc,
4980"cpu_set.set(i)\n\n\
4981Add CPU *i* to the set.");
4982
4983static PyObject *
4984cpu_set_set(Py_cpu_set *set, PyObject *args)
4985{
4986 int cpu = _get_cpu(set, "i|set", args);
4987 if (cpu == -1)
4988 return NULL;
4989 CPU_SET_S(cpu, set->size, set->set);
4990 Py_RETURN_NONE;
4991}
4992
4993PyDoc_STRVAR(cpu_set_count_doc,
4994"cpu_set.count() -> int\n\n\
4995Return the number of CPUs active in the set.");
4996
4997static PyObject *
4998cpu_set_count(Py_cpu_set *set, PyObject *noargs)
4999{
5000 return PyLong_FromLong(CPU_COUNT_S(set->size, set->set));
5001}
5002
5003PyDoc_STRVAR(cpu_set_clear_doc,
5004"cpu_set.clear(i)\n\n\
5005Remove CPU *i* from the set.");
5006
5007static PyObject *
5008cpu_set_clear(Py_cpu_set *set, PyObject *args)
5009{
5010 int cpu = _get_cpu(set, "i|clear", args);
5011 if (cpu == -1)
5012 return NULL;
5013 CPU_CLR_S(cpu, set->size, set->set);
5014 Py_RETURN_NONE;
5015}
5016
5017PyDoc_STRVAR(cpu_set_isset_doc,
5018"cpu_set.isset(i) -> bool\n\n\
5019Test if CPU *i* is in the set.");
5020
5021static PyObject *
5022cpu_set_isset(Py_cpu_set *set, PyObject *args)
5023{
5024 int cpu = _get_cpu(set, "i|isset", args);
5025 if (cpu == -1)
5026 return NULL;
5027 if (CPU_ISSET_S(cpu, set->size, set->set))
5028 Py_RETURN_TRUE;
5029 Py_RETURN_FALSE;
5030}
5031
5032PyDoc_STRVAR(cpu_set_zero_doc,
5033"cpu_set.zero()\n\n\
5034Clear the cpu_set.");
5035
5036static PyObject *
5037cpu_set_zero(Py_cpu_set *set, PyObject *noargs)
5038{
5039 CPU_ZERO_S(set->size, set->set);
5040 Py_RETURN_NONE;
5041}
5042
5043static PyObject *
5044cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op)
5045{
5046 int eq;
5047
Brian Curtindfc80e32011-08-10 20:28:54 -05005048 if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type)
5049 Py_RETURN_NOTIMPLEMENTED;
5050
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005051 eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set);
5052 if ((op == Py_EQ) ? eq : !eq)
5053 Py_RETURN_TRUE;
5054 else
5055 Py_RETURN_FALSE;
5056}
5057
5058#define CPU_SET_BINOP(name, op) \
5059 static PyObject * \
5060 do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \
5061 if (res) { \
5062 Py_INCREF(res); \
5063 } \
5064 else { \
Benjamin Petersone870fe62011-08-02 17:44:26 -05005065 res = make_new_cpu_set(&cpu_set_type, left->ncpus); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005066 if (!res) \
5067 return NULL; \
5068 } \
Benjamin Peterson9b374bf2011-08-02 18:22:30 -05005069 if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005070 Py_DECREF(res); \
Brian Curtindfc80e32011-08-10 20:28:54 -05005071 Py_RETURN_NOTIMPLEMENTED; \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005072 } \
Benjamin Peterson8f7bdd32011-08-02 18:34:30 -05005073 assert(left->size == right->size && right->size == res->size); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005074 op(res->size, res->set, left->set, right->set); \
5075 return (PyObject *)res; \
5076 } \
5077 static PyObject * \
5078 cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \
5079 return do_cpu_set_##name(left, right, NULL); \
5080 } \
5081 static PyObject * \
5082 cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \
5083 return do_cpu_set_##name(left, right, left); \
5084 } \
5085
5086CPU_SET_BINOP(and, CPU_AND_S)
5087CPU_SET_BINOP(or, CPU_OR_S)
5088CPU_SET_BINOP(xor, CPU_XOR_S)
5089#undef CPU_SET_BINOP
5090
5091PyDoc_STRVAR(cpu_set_doc,
5092"cpu_set(size)\n\n\
5093Create an empty mask of CPUs.");
5094
5095static PyNumberMethods cpu_set_as_number = {
5096 0, /*nb_add*/
5097 0, /*nb_subtract*/
5098 0, /*nb_multiply*/
5099 0, /*nb_remainder*/
5100 0, /*nb_divmod*/
5101 0, /*nb_power*/
5102 0, /*nb_negative*/
5103 0, /*nb_positive*/
5104 0, /*nb_absolute*/
5105 0, /*nb_bool*/
5106 0, /*nb_invert*/
5107 0, /*nb_lshift*/
5108 0, /*nb_rshift*/
5109 (binaryfunc)cpu_set_and, /*nb_and*/
5110 (binaryfunc)cpu_set_xor, /*nb_xor*/
5111 (binaryfunc)cpu_set_or, /*nb_or*/
5112 0, /*nb_int*/
5113 0, /*nb_reserved*/
5114 0, /*nb_float*/
5115 0, /*nb_inplace_add*/
5116 0, /*nb_inplace_subtract*/
5117 0, /*nb_inplace_multiply*/
5118 0, /*nb_inplace_remainder*/
5119 0, /*nb_inplace_power*/
5120 0, /*nb_inplace_lshift*/
5121 0, /*nb_inplace_rshift*/
5122 (binaryfunc)cpu_set_iand, /*nb_inplace_and*/
5123 (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/
5124 (binaryfunc)cpu_set_ior, /*nb_inplace_or*/
5125};
5126
5127static PySequenceMethods cpu_set_as_sequence = {
5128 (lenfunc)cpu_set_len, /* sq_length */
5129};
5130
5131static PyMethodDef cpu_set_methods[] = {
5132 {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc},
5133 {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc},
5134 {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc},
5135 {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc},
5136 {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc},
5137 {NULL, NULL} /* sentinel */
5138};
5139
5140static PyTypeObject cpu_set_type = {
5141 PyVarObject_HEAD_INIT(&PyType_Type, 0)
5142 "posix.cpu_set", /* tp_name */
5143 sizeof(Py_cpu_set), /* tp_basicsize */
5144 0, /* tp_itemsize */
5145 /* methods */
5146 (destructor)cpu_set_dealloc, /* tp_dealloc */
5147 0, /* tp_print */
5148 0, /* tp_getattr */
5149 0, /* tp_setattr */
5150 0, /* tp_reserved */
5151 (reprfunc)cpu_set_repr, /* tp_repr */
5152 &cpu_set_as_number, /* tp_as_number */
5153 &cpu_set_as_sequence, /* tp_as_sequence */
5154 0, /* tp_as_mapping */
5155 PyObject_HashNotImplemented, /* tp_hash */
5156 0, /* tp_call */
5157 0, /* tp_str */
5158 PyObject_GenericGetAttr, /* tp_getattro */
5159 0, /* tp_setattro */
5160 0, /* tp_as_buffer */
5161 Py_TPFLAGS_DEFAULT, /* tp_flags */
5162 cpu_set_doc, /* tp_doc */
5163 0, /* tp_traverse */
5164 0, /* tp_clear */
5165 (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */
5166 0, /* tp_weaklistoffset */
5167 0, /* tp_iter */
5168 0, /* tp_iternext */
5169 cpu_set_methods, /* tp_methods */
5170 0, /* tp_members */
5171 0, /* tp_getset */
5172 0, /* tp_base */
5173 0, /* tp_dict */
5174 0, /* tp_descr_get */
5175 0, /* tp_descr_set */
5176 0, /* tp_dictoffset */
5177 0, /* tp_init */
5178 PyType_GenericAlloc, /* tp_alloc */
5179 cpu_set_new, /* tp_new */
5180 PyObject_Del, /* tp_free */
5181};
5182
5183PyDoc_STRVAR(posix_sched_setaffinity__doc__,
5184"sched_setaffinity(pid, cpu_set)\n\n\
5185Set the affinity of the process with PID *pid* to *cpu_set*.");
5186
5187static PyObject *
5188posix_sched_setaffinity(PyObject *self, PyObject *args)
5189{
5190 pid_t pid;
5191 Py_cpu_set *cpu_set;
5192
Benjamin Petersona17a5d62011-08-09 16:49:13 -05005193 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!:sched_setaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005194 &pid, &cpu_set_type, &cpu_set))
5195 return NULL;
5196 if (sched_setaffinity(pid, cpu_set->size, cpu_set->set))
5197 return posix_error();
5198 Py_RETURN_NONE;
5199}
5200
5201PyDoc_STRVAR(posix_sched_getaffinity__doc__,
5202"sched_getaffinity(pid, ncpus) -> cpu_set\n\n\
5203Return the affinity of the process with PID *pid*.\n\
5204The returned cpu_set will be of size *ncpus*.");
5205
5206static PyObject *
5207posix_sched_getaffinity(PyObject *self, PyObject *args)
5208{
5209 pid_t pid;
5210 int ncpus;
5211 Py_cpu_set *res;
5212
Benjamin Peterson7ac92142011-08-03 08:54:26 -05005213 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:sched_getaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005214 &pid, &ncpus))
5215 return NULL;
5216 res = make_new_cpu_set(&cpu_set_type, ncpus);
5217 if (!res)
5218 return NULL;
5219 if (sched_getaffinity(pid, res->size, res->set)) {
5220 Py_DECREF(res);
5221 return posix_error();
5222 }
5223 return (PyObject *)res;
5224}
5225
Benjamin Peterson2740af82011-08-02 17:41:34 -05005226#endif /* HAVE_SCHED_SETAFFINITY */
5227
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005228#endif /* HAVE_SCHED_H */
5229
Neal Norwitzb59798b2003-03-21 01:43:31 +00005230/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005231/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5232#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005233#define DEV_PTY_FILE "/dev/ptc"
5234#define HAVE_DEV_PTMX
5235#else
5236#define DEV_PTY_FILE "/dev/ptmx"
5237#endif
5238
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005239#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005240#ifdef HAVE_PTY_H
5241#include <pty.h>
5242#else
5243#ifdef HAVE_LIBUTIL_H
5244#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005245#else
5246#ifdef HAVE_UTIL_H
5247#include <util.h>
5248#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005249#endif /* HAVE_LIBUTIL_H */
5250#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005251#ifdef HAVE_STROPTS_H
5252#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005253#endif
5254#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005255
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005256#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005257PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005258"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005259Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005260
5261static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005262posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005263{
Victor Stinner8c62be82010-05-06 00:08:46 +00005264 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005265#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005266 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005267#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005268#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005269 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005270#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00005271 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005272#endif
5273#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005274
Thomas Wouters70c21a12000-07-14 14:28:33 +00005275#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005276 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
5277 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005278#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005279 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5280 if (slave_name == NULL)
5281 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00005282
Victor Stinner8c62be82010-05-06 00:08:46 +00005283 slave_fd = open(slave_name, O_RDWR);
5284 if (slave_fd < 0)
5285 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005286#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005287 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
5288 if (master_fd < 0)
5289 return posix_error();
5290 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
5291 /* change permission of slave */
5292 if (grantpt(master_fd) < 0) {
5293 PyOS_setsig(SIGCHLD, sig_saved);
5294 return posix_error();
5295 }
5296 /* unlock slave */
5297 if (unlockpt(master_fd) < 0) {
5298 PyOS_setsig(SIGCHLD, sig_saved);
5299 return posix_error();
5300 }
5301 PyOS_setsig(SIGCHLD, sig_saved);
5302 slave_name = ptsname(master_fd); /* get name of slave */
5303 if (slave_name == NULL)
5304 return posix_error();
5305 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
5306 if (slave_fd < 0)
5307 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005308#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005309 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
5310 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00005311#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00005312 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00005313#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005314#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00005315#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005316
Victor Stinner8c62be82010-05-06 00:08:46 +00005317 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00005318
Fred Drake8cef4cf2000-06-28 16:40:38 +00005319}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005320#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005321
5322#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005323PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005324"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00005325Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
5326Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005327To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005328
5329static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005330posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005331{
Victor Stinner8c62be82010-05-06 00:08:46 +00005332 int master_fd = -1, result = 0;
5333 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00005334
Victor Stinner8c62be82010-05-06 00:08:46 +00005335 _PyImport_AcquireLock();
5336 pid = forkpty(&master_fd, NULL, NULL, NULL);
5337 if (pid == 0) {
5338 /* child: this clobbers and resets the import lock. */
5339 PyOS_AfterFork();
5340 } else {
5341 /* parent: release the import lock. */
5342 result = _PyImport_ReleaseLock();
5343 }
5344 if (pid == -1)
5345 return posix_error();
5346 if (result < 0) {
5347 /* Don't clobber the OSError if the fork failed. */
5348 PyErr_SetString(PyExc_RuntimeError,
5349 "not holding the import lock");
5350 return NULL;
5351 }
5352 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00005353}
5354#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005355
Ross Lagerwall7807c352011-03-17 20:20:30 +02005356
Guido van Rossumad0ee831995-03-01 10:34:45 +00005357#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005358PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005359"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005360Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005361
Barry Warsaw53699e91996-12-10 23:23:01 +00005362static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005363posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005364{
Victor Stinner8c62be82010-05-06 00:08:46 +00005365 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005366}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005367#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005368
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005369
Guido van Rossumad0ee831995-03-01 10:34:45 +00005370#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005371PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005372"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005373Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005374
Barry Warsaw53699e91996-12-10 23:23:01 +00005375static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005376posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005377{
Victor Stinner8c62be82010-05-06 00:08:46 +00005378 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005379}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005380#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005381
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005382
Guido van Rossumad0ee831995-03-01 10:34:45 +00005383#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005384PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005385"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005386Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005387
Barry Warsaw53699e91996-12-10 23:23:01 +00005388static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005389posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005390{
Victor Stinner8c62be82010-05-06 00:08:46 +00005391 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005392}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005393#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005394
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005395
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005396PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005397"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005398Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005399
Barry Warsaw53699e91996-12-10 23:23:01 +00005400static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005401posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005402{
Victor Stinner8c62be82010-05-06 00:08:46 +00005403 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00005404}
5405
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005406#ifdef HAVE_GETGROUPLIST
5407PyDoc_STRVAR(posix_getgrouplist__doc__,
5408"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
5409Returns a list of groups to which a user belongs.\n\n\
5410 user: username to lookup\n\
5411 group: base group id of the user");
5412
5413static PyObject *
5414posix_getgrouplist(PyObject *self, PyObject *args)
5415{
5416#ifdef NGROUPS_MAX
5417#define MAX_GROUPS NGROUPS_MAX
5418#else
5419 /* defined to be 16 on Solaris7, so this should be a small number */
5420#define MAX_GROUPS 64
5421#endif
5422
5423 const char *user;
5424 int i, ngroups;
5425 PyObject *list;
5426#ifdef __APPLE__
5427 int *groups, basegid;
5428#else
5429 gid_t *groups, basegid;
5430#endif
5431 ngroups = MAX_GROUPS;
5432
5433 if (!PyArg_ParseTuple(args, "si", &user, &basegid))
5434 return NULL;
5435
5436#ifdef __APPLE__
5437 groups = PyMem_Malloc(ngroups * sizeof(int));
5438#else
5439 groups = PyMem_Malloc(ngroups * sizeof(gid_t));
5440#endif
5441 if (groups == NULL)
5442 return PyErr_NoMemory();
5443
5444 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
5445 PyMem_Del(groups);
5446 return posix_error();
5447 }
5448
5449 list = PyList_New(ngroups);
5450 if (list == NULL) {
5451 PyMem_Del(groups);
5452 return NULL;
5453 }
5454
5455 for (i = 0; i < ngroups; i++) {
5456 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
5457 if (o == NULL) {
5458 Py_DECREF(list);
5459 PyMem_Del(groups);
5460 return NULL;
5461 }
5462 PyList_SET_ITEM(list, i, o);
5463 }
5464
5465 PyMem_Del(groups);
5466
5467 return list;
5468}
5469#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005470
Fred Drakec9680921999-12-13 16:37:25 +00005471#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005472PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005473"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005474Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00005475
5476static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005477posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00005478{
5479 PyObject *result = NULL;
5480
Fred Drakec9680921999-12-13 16:37:25 +00005481#ifdef NGROUPS_MAX
5482#define MAX_GROUPS NGROUPS_MAX
5483#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005484 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00005485#define MAX_GROUPS 64
5486#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005487 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005488
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005489 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005490 * This is a helper variable to store the intermediate result when
5491 * that happens.
5492 *
5493 * To keep the code readable the OSX behaviour is unconditional,
5494 * according to the POSIX spec this should be safe on all unix-y
5495 * systems.
5496 */
5497 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005498 int n;
Fred Drakec9680921999-12-13 16:37:25 +00005499
Victor Stinner8c62be82010-05-06 00:08:46 +00005500 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005501 if (n < 0) {
5502 if (errno == EINVAL) {
5503 n = getgroups(0, NULL);
5504 if (n == -1) {
5505 return posix_error();
5506 }
5507 if (n == 0) {
5508 /* Avoid malloc(0) */
5509 alt_grouplist = grouplist;
5510 } else {
5511 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
5512 if (alt_grouplist == NULL) {
5513 errno = EINVAL;
5514 return posix_error();
5515 }
5516 n = getgroups(n, alt_grouplist);
5517 if (n == -1) {
5518 PyMem_Free(alt_grouplist);
5519 return posix_error();
5520 }
5521 }
5522 } else {
5523 return posix_error();
5524 }
5525 }
5526 result = PyList_New(n);
5527 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005528 int i;
5529 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005530 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00005531 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00005532 Py_DECREF(result);
5533 result = NULL;
5534 break;
Fred Drakec9680921999-12-13 16:37:25 +00005535 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005536 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00005537 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005538 }
5539
5540 if (alt_grouplist != grouplist) {
5541 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00005542 }
Neal Norwitze241ce82003-02-17 18:17:05 +00005543
Fred Drakec9680921999-12-13 16:37:25 +00005544 return result;
5545}
5546#endif
5547
Antoine Pitroub7572f02009-12-02 20:46:48 +00005548#ifdef HAVE_INITGROUPS
5549PyDoc_STRVAR(posix_initgroups__doc__,
5550"initgroups(username, gid) -> None\n\n\
5551Call the system initgroups() to initialize the group access list with all of\n\
5552the groups of which the specified username is a member, plus the specified\n\
5553group id.");
5554
5555static PyObject *
5556posix_initgroups(PyObject *self, PyObject *args)
5557{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005558 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00005559 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005560 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00005561 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005562
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005563 if (!PyArg_ParseTuple(args, "O&l:initgroups",
5564 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00005565 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005566 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005567
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005568 res = initgroups(username, (gid_t) gid);
5569 Py_DECREF(oname);
5570 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00005571 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005572
Victor Stinner8c62be82010-05-06 00:08:46 +00005573 Py_INCREF(Py_None);
5574 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005575}
5576#endif
5577
Martin v. Löwis606edc12002-06-13 21:09:11 +00005578#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005579PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005580"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005581Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00005582
5583static PyObject *
5584posix_getpgid(PyObject *self, PyObject *args)
5585{
Victor Stinner8c62be82010-05-06 00:08:46 +00005586 pid_t pid, pgid;
5587 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
5588 return NULL;
5589 pgid = getpgid(pid);
5590 if (pgid < 0)
5591 return posix_error();
5592 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00005593}
5594#endif /* HAVE_GETPGID */
5595
5596
Guido van Rossumb6775db1994-08-01 11:34:53 +00005597#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005598PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005599"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005600Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005601
Barry Warsaw53699e91996-12-10 23:23:01 +00005602static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005603posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00005604{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005605#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005606 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005607#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005608 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005609#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00005610}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005611#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00005612
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005613
Guido van Rossumb6775db1994-08-01 11:34:53 +00005614#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005615PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005616"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00005617Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005618
Barry Warsaw53699e91996-12-10 23:23:01 +00005619static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005620posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005621{
Guido van Rossum64933891994-10-20 21:56:42 +00005622#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005623 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005624#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005625 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005626#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005627 return posix_error();
5628 Py_INCREF(Py_None);
5629 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005630}
5631
Guido van Rossumb6775db1994-08-01 11:34:53 +00005632#endif /* HAVE_SETPGRP */
5633
Guido van Rossumad0ee831995-03-01 10:34:45 +00005634#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005635
5636#ifdef MS_WINDOWS
5637#include <tlhelp32.h>
5638
5639static PyObject*
5640win32_getppid()
5641{
5642 HANDLE snapshot;
5643 pid_t mypid;
5644 PyObject* result = NULL;
5645 BOOL have_record;
5646 PROCESSENTRY32 pe;
5647
5648 mypid = getpid(); /* This function never fails */
5649
5650 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5651 if (snapshot == INVALID_HANDLE_VALUE)
5652 return PyErr_SetFromWindowsErr(GetLastError());
5653
5654 pe.dwSize = sizeof(pe);
5655 have_record = Process32First(snapshot, &pe);
5656 while (have_record) {
5657 if (mypid == (pid_t)pe.th32ProcessID) {
5658 /* We could cache the ulong value in a static variable. */
5659 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
5660 break;
5661 }
5662
5663 have_record = Process32Next(snapshot, &pe);
5664 }
5665
5666 /* If our loop exits and our pid was not found (result will be NULL)
5667 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
5668 * error anyway, so let's raise it. */
5669 if (!result)
5670 result = PyErr_SetFromWindowsErr(GetLastError());
5671
5672 CloseHandle(snapshot);
5673
5674 return result;
5675}
5676#endif /*MS_WINDOWS*/
5677
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005678PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005679"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005680Return the parent's process id. If the parent process has already exited,\n\
5681Windows machines will still return its id; others systems will return the id\n\
5682of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005683
Barry Warsaw53699e91996-12-10 23:23:01 +00005684static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005685posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005686{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005687#ifdef MS_WINDOWS
5688 return win32_getppid();
5689#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005690 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00005691#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005692}
5693#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005694
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005695
Fred Drake12c6e2d1999-12-14 21:25:03 +00005696#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005697PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005698"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005699Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00005700
5701static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005702posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005703{
Victor Stinner8c62be82010-05-06 00:08:46 +00005704 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005705#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005706 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02005707 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005708
5709 if (GetUserNameW(user_name, &num_chars)) {
5710 /* num_chars is the number of unicode chars plus null terminator */
5711 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005712 }
5713 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005714 result = PyErr_SetFromWindowsErr(GetLastError());
5715#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005716 char *name;
5717 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005718
Victor Stinner8c62be82010-05-06 00:08:46 +00005719 errno = 0;
5720 name = getlogin();
5721 if (name == NULL) {
5722 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00005723 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00005724 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005725 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00005726 }
5727 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005728 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00005729 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005730#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00005731 return result;
5732}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005733#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00005734
Guido van Rossumad0ee831995-03-01 10:34:45 +00005735#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005736PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005737"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005738Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005739
Barry Warsaw53699e91996-12-10 23:23:01 +00005740static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005741posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005742{
Victor Stinner8c62be82010-05-06 00:08:46 +00005743 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005744}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005745#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005746
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005747
Guido van Rossumad0ee831995-03-01 10:34:45 +00005748#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005749PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005750"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005751Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005752
Barry Warsaw53699e91996-12-10 23:23:01 +00005753static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005754posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005755{
Victor Stinner8c62be82010-05-06 00:08:46 +00005756 pid_t pid;
5757 int sig;
5758 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
5759 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005760#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005761 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
5762 APIRET rc;
5763 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005764 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005765
5766 } else if (sig == XCPT_SIGNAL_KILLPROC) {
5767 APIRET rc;
5768 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005769 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005770
5771 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00005772 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005773#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005774 if (kill(pid, sig) == -1)
5775 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005776#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005777 Py_INCREF(Py_None);
5778 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00005779}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005780#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005781
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005782#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005783PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005784"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005785Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005786
5787static PyObject *
5788posix_killpg(PyObject *self, PyObject *args)
5789{
Victor Stinner8c62be82010-05-06 00:08:46 +00005790 int sig;
5791 pid_t pgid;
5792 /* XXX some man pages make the `pgid` parameter an int, others
5793 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
5794 take the same type. Moreover, pid_t is always at least as wide as
5795 int (else compilation of this module fails), which is safe. */
5796 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
5797 return NULL;
5798 if (killpg(pgid, sig) == -1)
5799 return posix_error();
5800 Py_INCREF(Py_None);
5801 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005802}
5803#endif
5804
Brian Curtineb24d742010-04-12 17:16:38 +00005805#ifdef MS_WINDOWS
5806PyDoc_STRVAR(win32_kill__doc__,
5807"kill(pid, sig)\n\n\
5808Kill a process with a signal.");
5809
5810static PyObject *
5811win32_kill(PyObject *self, PyObject *args)
5812{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00005813 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00005814 DWORD pid, sig, err;
5815 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00005816
Victor Stinner8c62be82010-05-06 00:08:46 +00005817 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
5818 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00005819
Victor Stinner8c62be82010-05-06 00:08:46 +00005820 /* Console processes which share a common console can be sent CTRL+C or
5821 CTRL+BREAK events, provided they handle said events. */
5822 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
5823 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
5824 err = GetLastError();
5825 PyErr_SetFromWindowsErr(err);
5826 }
5827 else
5828 Py_RETURN_NONE;
5829 }
Brian Curtineb24d742010-04-12 17:16:38 +00005830
Victor Stinner8c62be82010-05-06 00:08:46 +00005831 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
5832 attempt to open and terminate the process. */
5833 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
5834 if (handle == NULL) {
5835 err = GetLastError();
5836 return PyErr_SetFromWindowsErr(err);
5837 }
Brian Curtineb24d742010-04-12 17:16:38 +00005838
Victor Stinner8c62be82010-05-06 00:08:46 +00005839 if (TerminateProcess(handle, sig) == 0) {
5840 err = GetLastError();
5841 result = PyErr_SetFromWindowsErr(err);
5842 } else {
5843 Py_INCREF(Py_None);
5844 result = Py_None;
5845 }
Brian Curtineb24d742010-04-12 17:16:38 +00005846
Victor Stinner8c62be82010-05-06 00:08:46 +00005847 CloseHandle(handle);
5848 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00005849}
5850#endif /* MS_WINDOWS */
5851
Guido van Rossumc0125471996-06-28 18:55:32 +00005852#ifdef HAVE_PLOCK
5853
5854#ifdef HAVE_SYS_LOCK_H
5855#include <sys/lock.h>
5856#endif
5857
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005858PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005859"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005860Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005861
Barry Warsaw53699e91996-12-10 23:23:01 +00005862static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005863posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00005864{
Victor Stinner8c62be82010-05-06 00:08:46 +00005865 int op;
5866 if (!PyArg_ParseTuple(args, "i:plock", &op))
5867 return NULL;
5868 if (plock(op) == -1)
5869 return posix_error();
5870 Py_INCREF(Py_None);
5871 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00005872}
5873#endif
5874
Guido van Rossumb6775db1994-08-01 11:34:53 +00005875#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005876PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005877"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005878Set the current process's user id.");
5879
Barry Warsaw53699e91996-12-10 23:23:01 +00005880static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005881posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005882{
Victor Stinner8c62be82010-05-06 00:08:46 +00005883 long uid_arg;
5884 uid_t uid;
5885 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5886 return NULL;
5887 uid = uid_arg;
5888 if (uid != uid_arg) {
5889 PyErr_SetString(PyExc_OverflowError, "user id too big");
5890 return NULL;
5891 }
5892 if (setuid(uid) < 0)
5893 return posix_error();
5894 Py_INCREF(Py_None);
5895 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005896}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005897#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005898
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005899
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005900#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005901PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005902"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005903Set the current process's effective user id.");
5904
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005905static PyObject *
5906posix_seteuid (PyObject *self, PyObject *args)
5907{
Victor Stinner8c62be82010-05-06 00:08:46 +00005908 long euid_arg;
5909 uid_t euid;
5910 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5911 return NULL;
5912 euid = euid_arg;
5913 if (euid != euid_arg) {
5914 PyErr_SetString(PyExc_OverflowError, "user id too big");
5915 return NULL;
5916 }
5917 if (seteuid(euid) < 0) {
5918 return posix_error();
5919 } else {
5920 Py_INCREF(Py_None);
5921 return Py_None;
5922 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005923}
5924#endif /* HAVE_SETEUID */
5925
5926#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005927PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005928"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005929Set the current process's effective group id.");
5930
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005931static PyObject *
5932posix_setegid (PyObject *self, PyObject *args)
5933{
Victor Stinner8c62be82010-05-06 00:08:46 +00005934 long egid_arg;
5935 gid_t egid;
5936 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5937 return NULL;
5938 egid = egid_arg;
5939 if (egid != egid_arg) {
5940 PyErr_SetString(PyExc_OverflowError, "group id too big");
5941 return NULL;
5942 }
5943 if (setegid(egid) < 0) {
5944 return posix_error();
5945 } else {
5946 Py_INCREF(Py_None);
5947 return Py_None;
5948 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005949}
5950#endif /* HAVE_SETEGID */
5951
5952#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005953PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005954"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005955Set the current process's real and effective user ids.");
5956
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005957static PyObject *
5958posix_setreuid (PyObject *self, PyObject *args)
5959{
Victor Stinner8c62be82010-05-06 00:08:46 +00005960 long ruid_arg, euid_arg;
5961 uid_t ruid, euid;
5962 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5963 return NULL;
5964 if (ruid_arg == -1)
5965 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5966 else
5967 ruid = ruid_arg; /* otherwise, assign from our long */
5968 if (euid_arg == -1)
5969 euid = (uid_t)-1;
5970 else
5971 euid = euid_arg;
5972 if ((euid_arg != -1 && euid != euid_arg) ||
5973 (ruid_arg != -1 && ruid != ruid_arg)) {
5974 PyErr_SetString(PyExc_OverflowError, "user id too big");
5975 return NULL;
5976 }
5977 if (setreuid(ruid, euid) < 0) {
5978 return posix_error();
5979 } else {
5980 Py_INCREF(Py_None);
5981 return Py_None;
5982 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005983}
5984#endif /* HAVE_SETREUID */
5985
5986#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005987PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005988"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005989Set the current process's real and effective group ids.");
5990
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005991static PyObject *
5992posix_setregid (PyObject *self, PyObject *args)
5993{
Victor Stinner8c62be82010-05-06 00:08:46 +00005994 long rgid_arg, egid_arg;
5995 gid_t rgid, egid;
5996 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
5997 return NULL;
5998 if (rgid_arg == -1)
5999 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
6000 else
6001 rgid = rgid_arg; /* otherwise, assign from our long */
6002 if (egid_arg == -1)
6003 egid = (gid_t)-1;
6004 else
6005 egid = egid_arg;
6006 if ((egid_arg != -1 && egid != egid_arg) ||
6007 (rgid_arg != -1 && rgid != rgid_arg)) {
6008 PyErr_SetString(PyExc_OverflowError, "group id too big");
6009 return NULL;
6010 }
6011 if (setregid(rgid, egid) < 0) {
6012 return posix_error();
6013 } else {
6014 Py_INCREF(Py_None);
6015 return Py_None;
6016 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006017}
6018#endif /* HAVE_SETREGID */
6019
Guido van Rossumb6775db1994-08-01 11:34:53 +00006020#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006021PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006022"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006023Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006024
Barry Warsaw53699e91996-12-10 23:23:01 +00006025static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006026posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006027{
Victor Stinner8c62be82010-05-06 00:08:46 +00006028 long gid_arg;
6029 gid_t gid;
6030 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
6031 return NULL;
6032 gid = gid_arg;
6033 if (gid != gid_arg) {
6034 PyErr_SetString(PyExc_OverflowError, "group id too big");
6035 return NULL;
6036 }
6037 if (setgid(gid) < 0)
6038 return posix_error();
6039 Py_INCREF(Py_None);
6040 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006041}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006042#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006043
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006044#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006045PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006046"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006047Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006048
6049static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00006050posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006051{
Victor Stinner8c62be82010-05-06 00:08:46 +00006052 int i, len;
6053 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00006054
Victor Stinner8c62be82010-05-06 00:08:46 +00006055 if (!PySequence_Check(groups)) {
6056 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
6057 return NULL;
6058 }
6059 len = PySequence_Size(groups);
6060 if (len > MAX_GROUPS) {
6061 PyErr_SetString(PyExc_ValueError, "too many groups");
6062 return NULL;
6063 }
6064 for(i = 0; i < len; i++) {
6065 PyObject *elem;
6066 elem = PySequence_GetItem(groups, i);
6067 if (!elem)
6068 return NULL;
6069 if (!PyLong_Check(elem)) {
6070 PyErr_SetString(PyExc_TypeError,
6071 "groups must be integers");
6072 Py_DECREF(elem);
6073 return NULL;
6074 } else {
6075 unsigned long x = PyLong_AsUnsignedLong(elem);
6076 if (PyErr_Occurred()) {
6077 PyErr_SetString(PyExc_TypeError,
6078 "group id too big");
6079 Py_DECREF(elem);
6080 return NULL;
6081 }
6082 grouplist[i] = x;
6083 /* read back the value to see if it fitted in gid_t */
6084 if (grouplist[i] != x) {
6085 PyErr_SetString(PyExc_TypeError,
6086 "group id too big");
6087 Py_DECREF(elem);
6088 return NULL;
6089 }
6090 }
6091 Py_DECREF(elem);
6092 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006093
Victor Stinner8c62be82010-05-06 00:08:46 +00006094 if (setgroups(len, grouplist) < 0)
6095 return posix_error();
6096 Py_INCREF(Py_None);
6097 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006098}
6099#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006100
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006101#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
6102static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00006103wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006104{
Victor Stinner8c62be82010-05-06 00:08:46 +00006105 PyObject *result;
6106 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006107
Victor Stinner8c62be82010-05-06 00:08:46 +00006108 if (pid == -1)
6109 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006110
Victor Stinner8c62be82010-05-06 00:08:46 +00006111 if (struct_rusage == NULL) {
6112 PyObject *m = PyImport_ImportModuleNoBlock("resource");
6113 if (m == NULL)
6114 return NULL;
6115 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
6116 Py_DECREF(m);
6117 if (struct_rusage == NULL)
6118 return NULL;
6119 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006120
Victor Stinner8c62be82010-05-06 00:08:46 +00006121 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6122 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6123 if (!result)
6124 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006125
6126#ifndef doubletime
6127#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6128#endif
6129
Victor Stinner8c62be82010-05-06 00:08:46 +00006130 PyStructSequence_SET_ITEM(result, 0,
6131 PyFloat_FromDouble(doubletime(ru->ru_utime)));
6132 PyStructSequence_SET_ITEM(result, 1,
6133 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006134#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00006135 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
6136 SET_INT(result, 2, ru->ru_maxrss);
6137 SET_INT(result, 3, ru->ru_ixrss);
6138 SET_INT(result, 4, ru->ru_idrss);
6139 SET_INT(result, 5, ru->ru_isrss);
6140 SET_INT(result, 6, ru->ru_minflt);
6141 SET_INT(result, 7, ru->ru_majflt);
6142 SET_INT(result, 8, ru->ru_nswap);
6143 SET_INT(result, 9, ru->ru_inblock);
6144 SET_INT(result, 10, ru->ru_oublock);
6145 SET_INT(result, 11, ru->ru_msgsnd);
6146 SET_INT(result, 12, ru->ru_msgrcv);
6147 SET_INT(result, 13, ru->ru_nsignals);
6148 SET_INT(result, 14, ru->ru_nvcsw);
6149 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006150#undef SET_INT
6151
Victor Stinner8c62be82010-05-06 00:08:46 +00006152 if (PyErr_Occurred()) {
6153 Py_DECREF(result);
6154 return NULL;
6155 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006156
Victor Stinner8c62be82010-05-06 00:08:46 +00006157 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006158}
6159#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6160
6161#ifdef HAVE_WAIT3
6162PyDoc_STRVAR(posix_wait3__doc__,
6163"wait3(options) -> (pid, status, rusage)\n\n\
6164Wait for completion of a child process.");
6165
6166static PyObject *
6167posix_wait3(PyObject *self, PyObject *args)
6168{
Victor Stinner8c62be82010-05-06 00:08:46 +00006169 pid_t pid;
6170 int options;
6171 struct rusage ru;
6172 WAIT_TYPE status;
6173 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006174
Victor Stinner8c62be82010-05-06 00:08:46 +00006175 if (!PyArg_ParseTuple(args, "i:wait3", &options))
6176 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006177
Victor Stinner8c62be82010-05-06 00:08:46 +00006178 Py_BEGIN_ALLOW_THREADS
6179 pid = wait3(&status, options, &ru);
6180 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006181
Victor Stinner8c62be82010-05-06 00:08:46 +00006182 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006183}
6184#endif /* HAVE_WAIT3 */
6185
6186#ifdef HAVE_WAIT4
6187PyDoc_STRVAR(posix_wait4__doc__,
6188"wait4(pid, options) -> (pid, status, rusage)\n\n\
6189Wait for completion of a given child process.");
6190
6191static PyObject *
6192posix_wait4(PyObject *self, PyObject *args)
6193{
Victor Stinner8c62be82010-05-06 00:08:46 +00006194 pid_t pid;
6195 int options;
6196 struct rusage ru;
6197 WAIT_TYPE status;
6198 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006199
Victor Stinner8c62be82010-05-06 00:08:46 +00006200 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
6201 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006202
Victor Stinner8c62be82010-05-06 00:08:46 +00006203 Py_BEGIN_ALLOW_THREADS
6204 pid = wait4(pid, &status, options, &ru);
6205 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006206
Victor Stinner8c62be82010-05-06 00:08:46 +00006207 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006208}
6209#endif /* HAVE_WAIT4 */
6210
Ross Lagerwall7807c352011-03-17 20:20:30 +02006211#if defined(HAVE_WAITID) && !defined(__APPLE__)
6212PyDoc_STRVAR(posix_waitid__doc__,
6213"waitid(idtype, id, options) -> waitid_result\n\n\
6214Wait for the completion of one or more child processes.\n\n\
6215idtype can be P_PID, P_PGID or P_ALL.\n\
6216id specifies the pid to wait on.\n\
6217options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
6218or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
6219Returns either waitid_result or None if WNOHANG is specified and there are\n\
6220no children in a waitable state.");
6221
6222static PyObject *
6223posix_waitid(PyObject *self, PyObject *args)
6224{
6225 PyObject *result;
6226 idtype_t idtype;
6227 id_t id;
6228 int options, res;
6229 siginfo_t si;
6230 si.si_pid = 0;
6231 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
6232 return NULL;
6233 Py_BEGIN_ALLOW_THREADS
6234 res = waitid(idtype, id, &si, options);
6235 Py_END_ALLOW_THREADS
6236 if (res == -1)
6237 return posix_error();
6238
6239 if (si.si_pid == 0)
6240 Py_RETURN_NONE;
6241
6242 result = PyStructSequence_New(&WaitidResultType);
6243 if (!result)
6244 return NULL;
6245
6246 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
6247 PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid));
6248 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
6249 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
6250 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
6251 if (PyErr_Occurred()) {
6252 Py_DECREF(result);
6253 return NULL;
6254 }
6255
6256 return result;
6257}
6258#endif
6259
Guido van Rossumb6775db1994-08-01 11:34:53 +00006260#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006261PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006262"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006263Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006264
Barry Warsaw53699e91996-12-10 23:23:01 +00006265static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006266posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006267{
Victor Stinner8c62be82010-05-06 00:08:46 +00006268 pid_t pid;
6269 int options;
6270 WAIT_TYPE status;
6271 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006272
Victor Stinner8c62be82010-05-06 00:08:46 +00006273 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6274 return NULL;
6275 Py_BEGIN_ALLOW_THREADS
6276 pid = waitpid(pid, &status, options);
6277 Py_END_ALLOW_THREADS
6278 if (pid == -1)
6279 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006280
Victor Stinner8c62be82010-05-06 00:08:46 +00006281 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006282}
6283
Tim Petersab034fa2002-02-01 11:27:43 +00006284#elif defined(HAVE_CWAIT)
6285
6286/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006287PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006288"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006289"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00006290
6291static PyObject *
6292posix_waitpid(PyObject *self, PyObject *args)
6293{
Victor Stinner8c62be82010-05-06 00:08:46 +00006294 Py_intptr_t pid;
6295 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006296
Victor Stinner8c62be82010-05-06 00:08:46 +00006297 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6298 return NULL;
6299 Py_BEGIN_ALLOW_THREADS
6300 pid = _cwait(&status, pid, options);
6301 Py_END_ALLOW_THREADS
6302 if (pid == -1)
6303 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006304
Victor Stinner8c62be82010-05-06 00:08:46 +00006305 /* shift the status left a byte so this is more like the POSIX waitpid */
6306 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006307}
6308#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006309
Guido van Rossumad0ee831995-03-01 10:34:45 +00006310#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006311PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006312"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006313Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006314
Barry Warsaw53699e91996-12-10 23:23:01 +00006315static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006316posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006317{
Victor Stinner8c62be82010-05-06 00:08:46 +00006318 pid_t pid;
6319 WAIT_TYPE status;
6320 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006321
Victor Stinner8c62be82010-05-06 00:08:46 +00006322 Py_BEGIN_ALLOW_THREADS
6323 pid = wait(&status);
6324 Py_END_ALLOW_THREADS
6325 if (pid == -1)
6326 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006327
Victor Stinner8c62be82010-05-06 00:08:46 +00006328 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006329}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006330#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006331
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006332
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006333PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006334"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006335Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006336
Barry Warsaw53699e91996-12-10 23:23:01 +00006337static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006338posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006339{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006340#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00006341 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006342#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006343#ifdef MS_WINDOWS
Brian Curtind25aef52011-06-13 15:16:04 -05006344 return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat",
Brian Curtind40e6f72010-07-08 21:39:08 +00006345 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006346#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006347 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006348#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006349#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006350}
6351
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006352
Guido van Rossumb6775db1994-08-01 11:34:53 +00006353#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006354PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006355"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006356Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006357
Barry Warsaw53699e91996-12-10 23:23:01 +00006358static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006359posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006360{
Victor Stinner8c62be82010-05-06 00:08:46 +00006361 PyObject* v;
6362 char buf[MAXPATHLEN];
6363 PyObject *opath;
6364 char *path;
6365 int n;
6366 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006367
Victor Stinner8c62be82010-05-06 00:08:46 +00006368 if (!PyArg_ParseTuple(args, "O&:readlink",
6369 PyUnicode_FSConverter, &opath))
6370 return NULL;
6371 path = PyBytes_AsString(opath);
6372 v = PySequence_GetItem(args, 0);
6373 if (v == NULL) {
6374 Py_DECREF(opath);
6375 return NULL;
6376 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00006377
Victor Stinner8c62be82010-05-06 00:08:46 +00006378 if (PyUnicode_Check(v)) {
6379 arg_is_unicode = 1;
6380 }
6381 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006382
Victor Stinner8c62be82010-05-06 00:08:46 +00006383 Py_BEGIN_ALLOW_THREADS
6384 n = readlink(path, buf, (int) sizeof buf);
6385 Py_END_ALLOW_THREADS
6386 if (n < 0)
6387 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006388
Victor Stinner8c62be82010-05-06 00:08:46 +00006389 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00006390 if (arg_is_unicode)
6391 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
6392 else
6393 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006394}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006395#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006396
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006397
Brian Curtin52173d42010-12-02 18:29:18 +00006398#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006399PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006400"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006401Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006402
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006403static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006404posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006405{
Victor Stinner8c62be82010-05-06 00:08:46 +00006406 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006407}
6408#endif /* HAVE_SYMLINK */
6409
Brian Curtind40e6f72010-07-08 21:39:08 +00006410#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
6411
6412PyDoc_STRVAR(win_readlink__doc__,
6413"readlink(path) -> path\n\n\
6414Return a string representing the path to which the symbolic link points.");
6415
Brian Curtind40e6f72010-07-08 21:39:08 +00006416/* Windows readlink implementation */
6417static PyObject *
6418win_readlink(PyObject *self, PyObject *args)
6419{
6420 wchar_t *path;
6421 DWORD n_bytes_returned;
6422 DWORD io_result;
Victor Stinnereb5657a2011-09-30 01:44:27 +02006423 PyObject *po, *result;
Brian Curtind40e6f72010-07-08 21:39:08 +00006424 HANDLE reparse_point_handle;
6425
6426 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
6427 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
6428 wchar_t *print_name;
6429
6430 if (!PyArg_ParseTuple(args,
Victor Stinnereb5657a2011-09-30 01:44:27 +02006431 "U:readlink",
6432 &po))
6433 return NULL;
6434 path = PyUnicode_AsUnicode(po);
6435 if (path == NULL)
Brian Curtind40e6f72010-07-08 21:39:08 +00006436 return NULL;
6437
6438 /* First get a handle to the reparse point */
6439 Py_BEGIN_ALLOW_THREADS
6440 reparse_point_handle = CreateFileW(
6441 path,
6442 0,
6443 0,
6444 0,
6445 OPEN_EXISTING,
6446 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
6447 0);
6448 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006449
Brian Curtind40e6f72010-07-08 21:39:08 +00006450 if (reparse_point_handle==INVALID_HANDLE_VALUE)
Victor Stinnereb5657a2011-09-30 01:44:27 +02006451 return win32_error_object("readlink", po);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006452
Brian Curtind40e6f72010-07-08 21:39:08 +00006453 Py_BEGIN_ALLOW_THREADS
6454 /* New call DeviceIoControl to read the reparse point */
6455 io_result = DeviceIoControl(
6456 reparse_point_handle,
6457 FSCTL_GET_REPARSE_POINT,
6458 0, 0, /* in buffer */
6459 target_buffer, sizeof(target_buffer),
6460 &n_bytes_returned,
6461 0 /* we're not using OVERLAPPED_IO */
6462 );
6463 CloseHandle(reparse_point_handle);
6464 Py_END_ALLOW_THREADS
6465
6466 if (io_result==0)
Victor Stinnereb5657a2011-09-30 01:44:27 +02006467 return win32_error_object("readlink", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00006468
6469 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
6470 {
6471 PyErr_SetString(PyExc_ValueError,
6472 "not a symbolic link");
6473 return NULL;
6474 }
Brian Curtin74e45612010-07-09 15:58:59 +00006475 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
6476 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
6477
6478 result = PyUnicode_FromWideChar(print_name,
6479 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00006480 return result;
6481}
6482
6483#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
6484
Brian Curtin52173d42010-12-02 18:29:18 +00006485#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00006486
6487/* Grab CreateSymbolicLinkW dynamically from kernel32 */
6488static int has_CreateSymbolicLinkW = 0;
6489static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
6490static int
6491check_CreateSymbolicLinkW()
6492{
6493 HINSTANCE hKernel32;
6494 /* only recheck */
6495 if (has_CreateSymbolicLinkW)
6496 return has_CreateSymbolicLinkW;
6497 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00006498 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
6499 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00006500 if (Py_CreateSymbolicLinkW)
6501 has_CreateSymbolicLinkW = 1;
6502 return has_CreateSymbolicLinkW;
6503}
6504
6505PyDoc_STRVAR(win_symlink__doc__,
6506"symlink(src, dst, target_is_directory=False)\n\n\
6507Create a symbolic link pointing to src named dst.\n\
6508target_is_directory is required if the target is to be interpreted as\n\
6509a directory.\n\
6510This function requires Windows 6.0 or greater, and raises a\n\
6511NotImplementedError otherwise.");
6512
6513static PyObject *
6514win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
6515{
6516 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
6517 PyObject *src, *dest;
Victor Stinnereb5657a2011-09-30 01:44:27 +02006518 wchar_t *wsrc, *wdest;
Brian Curtind40e6f72010-07-08 21:39:08 +00006519 int target_is_directory = 0;
6520 DWORD res;
6521 WIN32_FILE_ATTRIBUTE_DATA src_info;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006522
Brian Curtind40e6f72010-07-08 21:39:08 +00006523 if (!check_CreateSymbolicLinkW())
6524 {
6525 /* raise NotImplementedError */
6526 return PyErr_Format(PyExc_NotImplementedError,
6527 "CreateSymbolicLinkW not found");
6528 }
6529 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
6530 kwlist, &src, &dest, &target_is_directory))
6531 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00006532
6533 if (win32_can_symlink == 0)
6534 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
6535
Victor Stinnereb5657a2011-09-30 01:44:27 +02006536 if (!convert_to_unicode(&src))
6537 return NULL;
Brian Curtind40e6f72010-07-08 21:39:08 +00006538 if (!convert_to_unicode(&dest)) {
6539 Py_DECREF(src);
6540 return NULL;
6541 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006542
Victor Stinnereb5657a2011-09-30 01:44:27 +02006543 wsrc = PyUnicode_AsUnicode(src);
6544 if (wsrc == NULL)
6545 goto error;
6546 wdest = PyUnicode_AsUnicode(dest);
6547 if (wsrc == NULL)
6548 goto error;
6549
Brian Curtind40e6f72010-07-08 21:39:08 +00006550 /* if src is a directory, ensure target_is_directory==1 */
6551 if(
6552 GetFileAttributesExW(
Victor Stinnereb5657a2011-09-30 01:44:27 +02006553 wsrc, GetFileExInfoStandard, &src_info
Brian Curtind40e6f72010-07-08 21:39:08 +00006554 ))
6555 {
6556 target_is_directory = target_is_directory ||
6557 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
6558 }
6559
6560 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006561 res = Py_CreateSymbolicLinkW(wdest, wsrc, target_is_directory);
Brian Curtind40e6f72010-07-08 21:39:08 +00006562 Py_END_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006563
Brian Curtind40e6f72010-07-08 21:39:08 +00006564 Py_DECREF(src);
6565 Py_DECREF(dest);
6566 if (!res)
Victor Stinnereb5657a2011-09-30 01:44:27 +02006567 return win32_error_object("symlink", src);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006568
Brian Curtind40e6f72010-07-08 21:39:08 +00006569 Py_INCREF(Py_None);
6570 return Py_None;
Victor Stinnereb5657a2011-09-30 01:44:27 +02006571
6572error:
6573 Py_DECREF(src);
6574 Py_DECREF(dest);
6575 return NULL;
Brian Curtind40e6f72010-07-08 21:39:08 +00006576}
Brian Curtin52173d42010-12-02 18:29:18 +00006577#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006578
6579#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006580#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6581static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006582system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006583{
6584 ULONG value = 0;
6585
6586 Py_BEGIN_ALLOW_THREADS
6587 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6588 Py_END_ALLOW_THREADS
6589
6590 return value;
6591}
6592
6593static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006594posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006595{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006596 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00006597 return Py_BuildValue("ddddd",
6598 (double)0 /* t.tms_utime / HZ */,
6599 (double)0 /* t.tms_stime / HZ */,
6600 (double)0 /* t.tms_cutime / HZ */,
6601 (double)0 /* t.tms_cstime / HZ */,
6602 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006603}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006604#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00006605#define NEED_TICKS_PER_SECOND
6606static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006607static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006608posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006609{
Victor Stinner8c62be82010-05-06 00:08:46 +00006610 struct tms t;
6611 clock_t c;
6612 errno = 0;
6613 c = times(&t);
6614 if (c == (clock_t) -1)
6615 return posix_error();
6616 return Py_BuildValue("ddddd",
6617 (double)t.tms_utime / ticks_per_second,
6618 (double)t.tms_stime / ticks_per_second,
6619 (double)t.tms_cutime / ticks_per_second,
6620 (double)t.tms_cstime / ticks_per_second,
6621 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006622}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006623#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006624#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006625
6626
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006627#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006628#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006629static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006630posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006631{
Victor Stinner8c62be82010-05-06 00:08:46 +00006632 FILETIME create, exit, kernel, user;
6633 HANDLE hProc;
6634 hProc = GetCurrentProcess();
6635 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6636 /* The fields of a FILETIME structure are the hi and lo part
6637 of a 64-bit value expressed in 100 nanosecond units.
6638 1e7 is one second in such units; 1e-7 the inverse.
6639 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6640 */
6641 return Py_BuildValue(
6642 "ddddd",
6643 (double)(user.dwHighDateTime*429.4967296 +
6644 user.dwLowDateTime*1e-7),
6645 (double)(kernel.dwHighDateTime*429.4967296 +
6646 kernel.dwLowDateTime*1e-7),
6647 (double)0,
6648 (double)0,
6649 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006650}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006651#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006652
6653#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006654PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006655"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006656Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006657#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006658
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006659
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006660#ifdef HAVE_GETSID
6661PyDoc_STRVAR(posix_getsid__doc__,
6662"getsid(pid) -> sid\n\n\
6663Call the system call getsid().");
6664
6665static PyObject *
6666posix_getsid(PyObject *self, PyObject *args)
6667{
Victor Stinner8c62be82010-05-06 00:08:46 +00006668 pid_t pid;
6669 int sid;
6670 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
6671 return NULL;
6672 sid = getsid(pid);
6673 if (sid < 0)
6674 return posix_error();
6675 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006676}
6677#endif /* HAVE_GETSID */
6678
6679
Guido van Rossumb6775db1994-08-01 11:34:53 +00006680#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006681PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006682"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006683Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006684
Barry Warsaw53699e91996-12-10 23:23:01 +00006685static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006686posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006687{
Victor Stinner8c62be82010-05-06 00:08:46 +00006688 if (setsid() < 0)
6689 return posix_error();
6690 Py_INCREF(Py_None);
6691 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006692}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006693#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006694
Guido van Rossumb6775db1994-08-01 11:34:53 +00006695#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006696PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006697"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006698Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006699
Barry Warsaw53699e91996-12-10 23:23:01 +00006700static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006701posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006702{
Victor Stinner8c62be82010-05-06 00:08:46 +00006703 pid_t pid;
6704 int pgrp;
6705 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
6706 return NULL;
6707 if (setpgid(pid, pgrp) < 0)
6708 return posix_error();
6709 Py_INCREF(Py_None);
6710 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006711}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006712#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006713
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006714
Guido van Rossumb6775db1994-08-01 11:34:53 +00006715#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006716PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006717"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006718Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006719
Barry Warsaw53699e91996-12-10 23:23:01 +00006720static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006721posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006722{
Victor Stinner8c62be82010-05-06 00:08:46 +00006723 int fd;
6724 pid_t pgid;
6725 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6726 return NULL;
6727 pgid = tcgetpgrp(fd);
6728 if (pgid < 0)
6729 return posix_error();
6730 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006731}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006732#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006733
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006734
Guido van Rossumb6775db1994-08-01 11:34:53 +00006735#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006736PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006737"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006738Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006739
Barry Warsaw53699e91996-12-10 23:23:01 +00006740static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006741posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006742{
Victor Stinner8c62be82010-05-06 00:08:46 +00006743 int fd;
6744 pid_t pgid;
6745 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
6746 return NULL;
6747 if (tcsetpgrp(fd, pgid) < 0)
6748 return posix_error();
6749 Py_INCREF(Py_None);
6750 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006751}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006752#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006753
Guido van Rossum687dd131993-05-17 08:34:16 +00006754/* Functions acting on file descriptors */
6755
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006756PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006757"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006758Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006759
Barry Warsaw53699e91996-12-10 23:23:01 +00006760static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006761posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006762{
Victor Stinner8c62be82010-05-06 00:08:46 +00006763 PyObject *ofile;
6764 char *file;
6765 int flag;
6766 int mode = 0777;
6767 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006768
6769#ifdef MS_WINDOWS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006770 PyObject *po;
Victor Stinner26de69d2011-06-17 15:15:38 +02006771 if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02006772 wchar_t *wpath = PyUnicode_AsUnicode(po);
6773 if (wpath == NULL)
6774 return NULL;
6775
Victor Stinner8c62be82010-05-06 00:08:46 +00006776 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006777 fd = _wopen(wpath, flag, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00006778 Py_END_ALLOW_THREADS
6779 if (fd < 0)
6780 return posix_error();
6781 return PyLong_FromLong((long)fd);
6782 }
6783 /* Drop the argument parsing error as narrow strings
6784 are also valid. */
6785 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006786#endif
6787
Victor Stinner26de69d2011-06-17 15:15:38 +02006788 if (!PyArg_ParseTuple(args, "O&i|i:open",
Victor Stinner8c62be82010-05-06 00:08:46 +00006789 PyUnicode_FSConverter, &ofile,
6790 &flag, &mode))
6791 return NULL;
6792 file = PyBytes_AsString(ofile);
6793 Py_BEGIN_ALLOW_THREADS
6794 fd = open(file, flag, mode);
6795 Py_END_ALLOW_THREADS
6796 if (fd < 0)
6797 return posix_error_with_allocated_filename(ofile);
6798 Py_DECREF(ofile);
6799 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006800}
6801
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006802
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006803PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006804"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006805Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006806
Barry Warsaw53699e91996-12-10 23:23:01 +00006807static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006808posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006809{
Victor Stinner8c62be82010-05-06 00:08:46 +00006810 int fd, res;
6811 if (!PyArg_ParseTuple(args, "i:close", &fd))
6812 return NULL;
6813 if (!_PyVerify_fd(fd))
6814 return posix_error();
6815 Py_BEGIN_ALLOW_THREADS
6816 res = close(fd);
6817 Py_END_ALLOW_THREADS
6818 if (res < 0)
6819 return posix_error();
6820 Py_INCREF(Py_None);
6821 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006822}
6823
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006824
Victor Stinner8c62be82010-05-06 00:08:46 +00006825PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00006826"closerange(fd_low, fd_high)\n\n\
6827Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6828
6829static PyObject *
6830posix_closerange(PyObject *self, PyObject *args)
6831{
Victor Stinner8c62be82010-05-06 00:08:46 +00006832 int fd_from, fd_to, i;
6833 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6834 return NULL;
6835 Py_BEGIN_ALLOW_THREADS
6836 for (i = fd_from; i < fd_to; i++)
6837 if (_PyVerify_fd(i))
6838 close(i);
6839 Py_END_ALLOW_THREADS
6840 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00006841}
6842
6843
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006844PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006845"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006846Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006847
Barry Warsaw53699e91996-12-10 23:23:01 +00006848static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006849posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006850{
Victor Stinner8c62be82010-05-06 00:08:46 +00006851 int fd;
6852 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6853 return NULL;
6854 if (!_PyVerify_fd(fd))
6855 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006856 fd = dup(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00006857 if (fd < 0)
6858 return posix_error();
6859 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006860}
6861
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006862
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006863PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006864"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006865Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006866
Barry Warsaw53699e91996-12-10 23:23:01 +00006867static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006868posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006869{
Victor Stinner8c62be82010-05-06 00:08:46 +00006870 int fd, fd2, res;
6871 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6872 return NULL;
6873 if (!_PyVerify_fd_dup2(fd, fd2))
6874 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006875 res = dup2(fd, fd2);
Victor Stinner8c62be82010-05-06 00:08:46 +00006876 if (res < 0)
6877 return posix_error();
6878 Py_INCREF(Py_None);
6879 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006880}
6881
Ross Lagerwall7807c352011-03-17 20:20:30 +02006882#ifdef HAVE_LOCKF
6883PyDoc_STRVAR(posix_lockf__doc__,
6884"lockf(fd, cmd, len)\n\n\
6885Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
6886fd is an open file descriptor.\n\
6887cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
6888F_TEST.\n\
6889len specifies the section of the file to lock.");
6890
6891static PyObject *
6892posix_lockf(PyObject *self, PyObject *args)
6893{
6894 int fd, cmd, res;
6895 off_t len;
6896 if (!PyArg_ParseTuple(args, "iiO&:lockf",
6897 &fd, &cmd, _parse_off_t, &len))
6898 return NULL;
6899
6900 Py_BEGIN_ALLOW_THREADS
6901 res = lockf(fd, cmd, len);
6902 Py_END_ALLOW_THREADS
6903
6904 if (res < 0)
6905 return posix_error();
6906
6907 Py_RETURN_NONE;
6908}
6909#endif
6910
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006911
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006912PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006913"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006914Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006915
Barry Warsaw53699e91996-12-10 23:23:01 +00006916static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006917posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006918{
Victor Stinner8c62be82010-05-06 00:08:46 +00006919 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006920#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006921 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006922#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006923 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006924#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02006925 PyObject *posobj;
6926 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00006927 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006928#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00006929 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6930 switch (how) {
6931 case 0: how = SEEK_SET; break;
6932 case 1: how = SEEK_CUR; break;
6933 case 2: how = SEEK_END; break;
6934 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006935#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006936
Ross Lagerwall8e749672011-03-17 21:54:07 +02006937#if !defined(HAVE_LARGEFILE_SUPPORT)
6938 pos = PyLong_AsLong(posobj);
6939#else
6940 pos = PyLong_AsLongLong(posobj);
6941#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006942 if (PyErr_Occurred())
6943 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006944
Victor Stinner8c62be82010-05-06 00:08:46 +00006945 if (!_PyVerify_fd(fd))
6946 return posix_error();
6947 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006948#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006949 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006950#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006951 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006952#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006953 Py_END_ALLOW_THREADS
6954 if (res < 0)
6955 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006956
6957#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006958 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006959#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006960 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006961#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006962}
6963
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006964
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006965PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006966"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006967Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006968
Barry Warsaw53699e91996-12-10 23:23:01 +00006969static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006970posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006971{
Victor Stinner8c62be82010-05-06 00:08:46 +00006972 int fd, size;
6973 Py_ssize_t n;
6974 PyObject *buffer;
6975 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6976 return NULL;
6977 if (size < 0) {
6978 errno = EINVAL;
6979 return posix_error();
6980 }
6981 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6982 if (buffer == NULL)
6983 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00006984 if (!_PyVerify_fd(fd)) {
6985 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00006986 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00006987 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006988 Py_BEGIN_ALLOW_THREADS
6989 n = read(fd, PyBytes_AS_STRING(buffer), size);
6990 Py_END_ALLOW_THREADS
6991 if (n < 0) {
6992 Py_DECREF(buffer);
6993 return posix_error();
6994 }
6995 if (n != size)
6996 _PyBytes_Resize(&buffer, n);
6997 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00006998}
6999
Ross Lagerwall7807c352011-03-17 20:20:30 +02007000#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
7001 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007002static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007003iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
7004{
7005 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007006 Py_ssize_t blen, total = 0;
7007
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007008 *iov = PyMem_New(struct iovec, cnt);
7009 if (*iov == NULL) {
7010 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007011 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007012 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007013
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007014 *buf = PyMem_New(Py_buffer, cnt);
7015 if (*buf == NULL) {
7016 PyMem_Del(*iov);
7017 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007018 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007019 }
7020
7021 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007022 PyObject *item = PySequence_GetItem(seq, i);
7023 if (item == NULL)
7024 goto fail;
7025 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
7026 Py_DECREF(item);
7027 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007028 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007029 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007030 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007031 blen = (*buf)[i].len;
7032 (*iov)[i].iov_len = blen;
7033 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007034 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007035 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007036
7037fail:
7038 PyMem_Del(*iov);
7039 for (j = 0; j < i; j++) {
7040 PyBuffer_Release(&(*buf)[j]);
7041 }
7042 PyMem_Del(*buf);
7043 return 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007044}
7045
7046static void
7047iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
7048{
7049 int i;
7050 PyMem_Del(iov);
7051 for (i = 0; i < cnt; i++) {
7052 PyBuffer_Release(&buf[i]);
7053 }
7054 PyMem_Del(buf);
7055}
7056#endif
7057
Ross Lagerwall7807c352011-03-17 20:20:30 +02007058#ifdef HAVE_READV
7059PyDoc_STRVAR(posix_readv__doc__,
7060"readv(fd, buffers) -> bytesread\n\n\
7061Read from a file descriptor into a number of writable buffers. buffers\n\
7062is an arbitrary sequence of writable buffers.\n\
7063Returns the total number of bytes read.");
7064
7065static PyObject *
7066posix_readv(PyObject *self, PyObject *args)
7067{
7068 int fd, cnt;
7069 Py_ssize_t n;
7070 PyObject *seq;
7071 struct iovec *iov;
7072 Py_buffer *buf;
7073
7074 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
7075 return NULL;
7076 if (!PySequence_Check(seq)) {
7077 PyErr_SetString(PyExc_TypeError,
7078 "readv() arg 2 must be a sequence");
7079 return NULL;
7080 }
7081 cnt = PySequence_Size(seq);
7082
7083 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
7084 return NULL;
7085
7086 Py_BEGIN_ALLOW_THREADS
7087 n = readv(fd, iov, cnt);
7088 Py_END_ALLOW_THREADS
7089
7090 iov_cleanup(iov, buf, cnt);
7091 return PyLong_FromSsize_t(n);
7092}
7093#endif
7094
7095#ifdef HAVE_PREAD
7096PyDoc_STRVAR(posix_pread__doc__,
7097"pread(fd, buffersize, offset) -> string\n\n\
7098Read from a file descriptor, fd, at a position of offset. It will read up\n\
7099to buffersize number of bytes. The file offset remains unchanged.");
7100
7101static PyObject *
7102posix_pread(PyObject *self, PyObject *args)
7103{
7104 int fd, size;
7105 off_t offset;
7106 Py_ssize_t n;
7107 PyObject *buffer;
7108 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
7109 return NULL;
7110
7111 if (size < 0) {
7112 errno = EINVAL;
7113 return posix_error();
7114 }
7115 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
7116 if (buffer == NULL)
7117 return NULL;
7118 if (!_PyVerify_fd(fd)) {
7119 Py_DECREF(buffer);
7120 return posix_error();
7121 }
7122 Py_BEGIN_ALLOW_THREADS
7123 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
7124 Py_END_ALLOW_THREADS
7125 if (n < 0) {
7126 Py_DECREF(buffer);
7127 return posix_error();
7128 }
7129 if (n != size)
7130 _PyBytes_Resize(&buffer, n);
7131 return buffer;
7132}
7133#endif
7134
7135PyDoc_STRVAR(posix_write__doc__,
7136"write(fd, string) -> byteswritten\n\n\
7137Write a string to a file descriptor.");
7138
7139static PyObject *
7140posix_write(PyObject *self, PyObject *args)
7141{
7142 Py_buffer pbuf;
7143 int fd;
7144 Py_ssize_t size, len;
7145
7146 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
7147 return NULL;
7148 if (!_PyVerify_fd(fd)) {
7149 PyBuffer_Release(&pbuf);
7150 return posix_error();
7151 }
7152 len = pbuf.len;
7153 Py_BEGIN_ALLOW_THREADS
7154#if defined(MS_WIN64) || defined(MS_WINDOWS)
7155 if (len > INT_MAX)
7156 len = INT_MAX;
7157 size = write(fd, pbuf.buf, (int)len);
7158#else
7159 size = write(fd, pbuf.buf, len);
7160#endif
7161 Py_END_ALLOW_THREADS
7162 PyBuffer_Release(&pbuf);
7163 if (size < 0)
7164 return posix_error();
7165 return PyLong_FromSsize_t(size);
7166}
7167
7168#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007169PyDoc_STRVAR(posix_sendfile__doc__,
7170"sendfile(out, in, offset, nbytes) -> byteswritten\n\
7171sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
7172 -> byteswritten\n\
7173Copy nbytes bytes from file descriptor in to file descriptor out.");
7174
7175static PyObject *
7176posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
7177{
7178 int in, out;
7179 Py_ssize_t ret;
7180 off_t offset;
7181
7182#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
7183#ifndef __APPLE__
7184 Py_ssize_t len;
7185#endif
7186 PyObject *headers = NULL, *trailers = NULL;
7187 Py_buffer *hbuf, *tbuf;
7188 off_t sbytes;
7189 struct sf_hdtr sf;
7190 int flags = 0;
7191 sf.headers = NULL;
7192 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00007193 static char *keywords[] = {"out", "in",
7194 "offset", "count",
7195 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007196
7197#ifdef __APPLE__
7198 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007199 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007200#else
7201 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007202 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007203#endif
7204 &headers, &trailers, &flags))
7205 return NULL;
7206 if (headers != NULL) {
7207 if (!PySequence_Check(headers)) {
7208 PyErr_SetString(PyExc_TypeError,
7209 "sendfile() headers must be a sequence or None");
7210 return NULL;
7211 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007212 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007213 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007214 if (sf.hdr_cnt > 0 &&
7215 !(i = iov_setup(&(sf.headers), &hbuf,
7216 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007217 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007218#ifdef __APPLE__
7219 sbytes += i;
7220#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007221 }
7222 }
7223 if (trailers != NULL) {
7224 if (!PySequence_Check(trailers)) {
7225 PyErr_SetString(PyExc_TypeError,
7226 "sendfile() trailers must be a sequence or None");
7227 return NULL;
7228 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007229 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007230 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007231 if (sf.trl_cnt > 0 &&
7232 !(i = iov_setup(&(sf.trailers), &tbuf,
7233 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007234 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007235#ifdef __APPLE__
7236 sbytes += i;
7237#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007238 }
7239 }
7240
7241 Py_BEGIN_ALLOW_THREADS
7242#ifdef __APPLE__
7243 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
7244#else
7245 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
7246#endif
7247 Py_END_ALLOW_THREADS
7248
7249 if (sf.headers != NULL)
7250 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
7251 if (sf.trailers != NULL)
7252 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
7253
7254 if (ret < 0) {
7255 if ((errno == EAGAIN) || (errno == EBUSY)) {
7256 if (sbytes != 0) {
7257 // some data has been sent
7258 goto done;
7259 }
7260 else {
7261 // no data has been sent; upper application is supposed
7262 // to retry on EAGAIN or EBUSY
7263 return posix_error();
7264 }
7265 }
7266 return posix_error();
7267 }
7268 goto done;
7269
7270done:
7271 #if !defined(HAVE_LARGEFILE_SUPPORT)
7272 return Py_BuildValue("l", sbytes);
7273 #else
7274 return Py_BuildValue("L", sbytes);
7275 #endif
7276
7277#else
7278 Py_ssize_t count;
7279 PyObject *offobj;
7280 static char *keywords[] = {"out", "in",
7281 "offset", "count", NULL};
7282 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
7283 keywords, &out, &in, &offobj, &count))
7284 return NULL;
7285#ifdef linux
7286 if (offobj == Py_None) {
7287 Py_BEGIN_ALLOW_THREADS
7288 ret = sendfile(out, in, NULL, count);
7289 Py_END_ALLOW_THREADS
7290 if (ret < 0)
7291 return posix_error();
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02007292 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007293 }
7294#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00007295 if (!_parse_off_t(offobj, &offset))
7296 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007297 Py_BEGIN_ALLOW_THREADS
7298 ret = sendfile(out, in, &offset, count);
7299 Py_END_ALLOW_THREADS
7300 if (ret < 0)
7301 return posix_error();
7302 return Py_BuildValue("n", ret);
7303#endif
7304}
7305#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007306
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007307PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007308"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007309Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007310
Barry Warsaw53699e91996-12-10 23:23:01 +00007311static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007312posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007313{
Victor Stinner8c62be82010-05-06 00:08:46 +00007314 int fd;
7315 STRUCT_STAT st;
7316 int res;
7317 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
7318 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007319#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007320 /* on OpenVMS we must ensure that all bytes are written to the file */
7321 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007322#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007323 if (!_PyVerify_fd(fd))
7324 return posix_error();
7325 Py_BEGIN_ALLOW_THREADS
7326 res = FSTAT(fd, &st);
7327 Py_END_ALLOW_THREADS
7328 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00007329#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007330 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00007331#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007332 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00007333#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007334 }
Tim Peters5aa91602002-01-30 05:46:57 +00007335
Victor Stinner8c62be82010-05-06 00:08:46 +00007336 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00007337}
7338
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007339PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007340"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007341Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007342connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00007343
7344static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00007345posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00007346{
Victor Stinner8c62be82010-05-06 00:08:46 +00007347 int fd;
7348 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
7349 return NULL;
7350 if (!_PyVerify_fd(fd))
7351 return PyBool_FromLong(0);
7352 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00007353}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007354
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007355#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007356PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007357"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007358Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007359
Barry Warsaw53699e91996-12-10 23:23:01 +00007360static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007361posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00007362{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007363#if defined(PYOS_OS2)
7364 HFILE read, write;
7365 APIRET rc;
7366
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007367 rc = DosCreatePipe( &read, &write, 4096);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007368 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007369 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007370
7371 return Py_BuildValue("(ii)", read, write);
7372#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007373#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007374 int fds[2];
7375 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007376 res = pipe(fds);
Victor Stinner8c62be82010-05-06 00:08:46 +00007377 if (res != 0)
7378 return posix_error();
7379 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007380#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007381 HANDLE read, write;
7382 int read_fd, write_fd;
7383 BOOL ok;
Victor Stinner8c62be82010-05-06 00:08:46 +00007384 ok = CreatePipe(&read, &write, NULL, 0);
Victor Stinner8c62be82010-05-06 00:08:46 +00007385 if (!ok)
7386 return win32_error("CreatePipe", NULL);
7387 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
7388 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
7389 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007390#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007391#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00007392}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007393#endif /* HAVE_PIPE */
7394
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007395#ifdef HAVE_PIPE2
7396PyDoc_STRVAR(posix_pipe2__doc__,
Charles-François Natali368f34b2011-06-06 19:49:47 +02007397"pipe2(flags) -> (read_end, write_end)\n\n\
7398Create a pipe with flags set atomically.\n\
7399flags can be constructed by ORing together one or more of these values:\n\
7400O_NONBLOCK, O_CLOEXEC.\n\
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007401");
7402
7403static PyObject *
Charles-François Natali368f34b2011-06-06 19:49:47 +02007404posix_pipe2(PyObject *self, PyObject *arg)
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007405{
Charles-François Natali368f34b2011-06-06 19:49:47 +02007406 int flags;
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007407 int fds[2];
7408 int res;
7409
Charles-François Natali368f34b2011-06-06 19:49:47 +02007410 flags = PyLong_AsLong(arg);
7411 if (flags == -1 && PyErr_Occurred())
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007412 return NULL;
7413
7414 res = pipe2(fds, flags);
7415 if (res != 0)
7416 return posix_error();
7417 return Py_BuildValue("(ii)", fds[0], fds[1]);
7418}
7419#endif /* HAVE_PIPE2 */
7420
Ross Lagerwall7807c352011-03-17 20:20:30 +02007421#ifdef HAVE_WRITEV
7422PyDoc_STRVAR(posix_writev__doc__,
7423"writev(fd, buffers) -> byteswritten\n\n\
7424Write the contents of buffers to a file descriptor, where buffers is an\n\
7425arbitrary sequence of buffers.\n\
7426Returns the total bytes written.");
7427
7428static PyObject *
7429posix_writev(PyObject *self, PyObject *args)
7430{
7431 int fd, cnt;
7432 Py_ssize_t res;
7433 PyObject *seq;
7434 struct iovec *iov;
7435 Py_buffer *buf;
7436 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
7437 return NULL;
7438 if (!PySequence_Check(seq)) {
7439 PyErr_SetString(PyExc_TypeError,
7440 "writev() arg 2 must be a sequence");
7441 return NULL;
7442 }
7443 cnt = PySequence_Size(seq);
7444
7445 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
7446 return NULL;
7447 }
7448
7449 Py_BEGIN_ALLOW_THREADS
7450 res = writev(fd, iov, cnt);
7451 Py_END_ALLOW_THREADS
7452
7453 iov_cleanup(iov, buf, cnt);
7454 return PyLong_FromSsize_t(res);
7455}
7456#endif
7457
7458#ifdef HAVE_PWRITE
7459PyDoc_STRVAR(posix_pwrite__doc__,
7460"pwrite(fd, string, offset) -> byteswritten\n\n\
7461Write string to a file descriptor, fd, from offset, leaving the file\n\
7462offset unchanged.");
7463
7464static PyObject *
7465posix_pwrite(PyObject *self, PyObject *args)
7466{
7467 Py_buffer pbuf;
7468 int fd;
7469 off_t offset;
7470 Py_ssize_t size;
7471
7472 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
7473 return NULL;
7474
7475 if (!_PyVerify_fd(fd)) {
7476 PyBuffer_Release(&pbuf);
7477 return posix_error();
7478 }
7479 Py_BEGIN_ALLOW_THREADS
7480 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
7481 Py_END_ALLOW_THREADS
7482 PyBuffer_Release(&pbuf);
7483 if (size < 0)
7484 return posix_error();
7485 return PyLong_FromSsize_t(size);
7486}
7487#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007488
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007489#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007490PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007491"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007492Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007493
Barry Warsaw53699e91996-12-10 23:23:01 +00007494static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007495posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007496{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007497 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007498 char *filename;
7499 int mode = 0666;
7500 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007501 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
7502 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00007503 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007504 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007505 Py_BEGIN_ALLOW_THREADS
7506 res = mkfifo(filename, mode);
7507 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007508 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007509 if (res < 0)
7510 return posix_error();
7511 Py_INCREF(Py_None);
7512 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007513}
7514#endif
7515
7516
Neal Norwitz11690112002-07-30 01:08:28 +00007517#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007518PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007519"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007520Create a filesystem node (file, device special file or named pipe)\n\
7521named filename. mode specifies both the permissions to use and the\n\
7522type of node to be created, being combined (bitwise OR) with one of\n\
7523S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007524device defines the newly created device special file (probably using\n\
7525os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007526
7527
7528static PyObject *
7529posix_mknod(PyObject *self, PyObject *args)
7530{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007531 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007532 char *filename;
7533 int mode = 0600;
7534 int device = 0;
7535 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007536 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
7537 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00007538 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007539 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007540 Py_BEGIN_ALLOW_THREADS
7541 res = mknod(filename, mode, device);
7542 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007543 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007544 if (res < 0)
7545 return posix_error();
7546 Py_INCREF(Py_None);
7547 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007548}
7549#endif
7550
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007551#ifdef HAVE_DEVICE_MACROS
7552PyDoc_STRVAR(posix_major__doc__,
7553"major(device) -> major number\n\
7554Extracts a device major number from a raw device number.");
7555
7556static PyObject *
7557posix_major(PyObject *self, PyObject *args)
7558{
Victor Stinner8c62be82010-05-06 00:08:46 +00007559 int device;
7560 if (!PyArg_ParseTuple(args, "i:major", &device))
7561 return NULL;
7562 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007563}
7564
7565PyDoc_STRVAR(posix_minor__doc__,
7566"minor(device) -> minor number\n\
7567Extracts a device minor number from a raw device number.");
7568
7569static PyObject *
7570posix_minor(PyObject *self, PyObject *args)
7571{
Victor Stinner8c62be82010-05-06 00:08:46 +00007572 int device;
7573 if (!PyArg_ParseTuple(args, "i:minor", &device))
7574 return NULL;
7575 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007576}
7577
7578PyDoc_STRVAR(posix_makedev__doc__,
7579"makedev(major, minor) -> device number\n\
7580Composes a raw device number from the major and minor device numbers.");
7581
7582static PyObject *
7583posix_makedev(PyObject *self, PyObject *args)
7584{
Victor Stinner8c62be82010-05-06 00:08:46 +00007585 int major, minor;
7586 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
7587 return NULL;
7588 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007589}
7590#endif /* device macros */
7591
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007592
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007593#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007594PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007595"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007596Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007597
Barry Warsaw53699e91996-12-10 23:23:01 +00007598static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007599posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007600{
Victor Stinner8c62be82010-05-06 00:08:46 +00007601 int fd;
7602 off_t length;
7603 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007604
Ross Lagerwall7807c352011-03-17 20:20:30 +02007605 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00007606 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007607
Victor Stinner8c62be82010-05-06 00:08:46 +00007608 Py_BEGIN_ALLOW_THREADS
7609 res = ftruncate(fd, length);
7610 Py_END_ALLOW_THREADS
7611 if (res < 0)
7612 return posix_error();
7613 Py_INCREF(Py_None);
7614 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007615}
7616#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00007617
Ross Lagerwall7807c352011-03-17 20:20:30 +02007618#ifdef HAVE_TRUNCATE
7619PyDoc_STRVAR(posix_truncate__doc__,
7620"truncate(path, length)\n\n\
7621Truncate the file given by path to length bytes.");
7622
7623static PyObject *
7624posix_truncate(PyObject *self, PyObject *args)
7625{
7626 PyObject *opath;
7627 const char *path;
7628 off_t length;
7629 int res;
7630
7631 if (!PyArg_ParseTuple(args, "O&O&:truncate",
7632 PyUnicode_FSConverter, &opath, _parse_off_t, &length))
7633 return NULL;
7634 path = PyBytes_AsString(opath);
7635
7636 Py_BEGIN_ALLOW_THREADS
7637 res = truncate(path, length);
7638 Py_END_ALLOW_THREADS
7639 Py_DECREF(opath);
7640 if (res < 0)
7641 return posix_error();
7642 Py_RETURN_NONE;
7643}
7644#endif
7645
7646#ifdef HAVE_POSIX_FALLOCATE
7647PyDoc_STRVAR(posix_posix_fallocate__doc__,
7648"posix_fallocate(fd, offset, len)\n\n\
7649Ensures that enough disk space is allocated for the file specified by fd\n\
7650starting from offset and continuing for len bytes.");
7651
7652static PyObject *
7653posix_posix_fallocate(PyObject *self, PyObject *args)
7654{
7655 off_t len, offset;
7656 int res, fd;
7657
7658 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
7659 &fd, _parse_off_t, &offset, _parse_off_t, &len))
7660 return NULL;
7661
7662 Py_BEGIN_ALLOW_THREADS
7663 res = posix_fallocate(fd, offset, len);
7664 Py_END_ALLOW_THREADS
7665 if (res != 0) {
7666 errno = res;
7667 return posix_error();
7668 }
7669 Py_RETURN_NONE;
7670}
7671#endif
7672
7673#ifdef HAVE_POSIX_FADVISE
7674PyDoc_STRVAR(posix_posix_fadvise__doc__,
7675"posix_fadvise(fd, offset, len, advice)\n\n\
7676Announces an intention to access data in a specific pattern thus allowing\n\
7677the kernel to make optimizations.\n\
7678The advice applies to the region of the file specified by fd starting at\n\
7679offset and continuing for len bytes.\n\
7680advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
7681POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
7682POSIX_FADV_DONTNEED.");
7683
7684static PyObject *
7685posix_posix_fadvise(PyObject *self, PyObject *args)
7686{
7687 off_t len, offset;
7688 int res, fd, advice;
7689
7690 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
7691 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
7692 return NULL;
7693
7694 Py_BEGIN_ALLOW_THREADS
7695 res = posix_fadvise(fd, offset, len, advice);
7696 Py_END_ALLOW_THREADS
7697 if (res != 0) {
7698 errno = res;
7699 return posix_error();
7700 }
7701 Py_RETURN_NONE;
7702}
7703#endif
7704
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007705#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007706PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007707"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007708Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007709
Fred Drake762e2061999-08-26 17:23:54 +00007710/* Save putenv() parameters as values here, so we can collect them when they
7711 * get re-set with another call for the same key. */
7712static PyObject *posix_putenv_garbage;
7713
Tim Peters5aa91602002-01-30 05:46:57 +00007714static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007715posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007716{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007717#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007718 wchar_t *s1, *s2;
7719 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007720#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007721 PyObject *os1, *os2;
7722 char *s1, *s2;
7723 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007724#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007725 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00007726 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007727
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007728#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007729 if (!PyArg_ParseTuple(args,
7730 "uu:putenv",
7731 &s1, &s2))
7732 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00007733#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007734 if (!PyArg_ParseTuple(args,
7735 "O&O&:putenv",
7736 PyUnicode_FSConverter, &os1,
7737 PyUnicode_FSConverter, &os2))
7738 return NULL;
7739 s1 = PyBytes_AsString(os1);
7740 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007741#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007742
7743#if defined(PYOS_OS2)
7744 if (stricmp(s1, "BEGINLIBPATH") == 0) {
7745 APIRET rc;
7746
Guido van Rossumd48f2521997-12-05 22:19:34 +00007747 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007748 if (rc != NO_ERROR) {
7749 os2_error(rc);
7750 goto error;
7751 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007752
7753 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
7754 APIRET rc;
7755
Guido van Rossumd48f2521997-12-05 22:19:34 +00007756 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007757 if (rc != NO_ERROR) {
7758 os2_error(rc);
7759 goto error;
7760 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007761 } else {
7762#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007763 /* XXX This can leak memory -- not easy to fix :-( */
7764 /* len includes space for a trailing \0; the size arg to
7765 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007766#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007767 len = wcslen(s1) + wcslen(s2) + 2;
7768 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007769#else
Victor Stinner84ae1182010-05-06 22:05:07 +00007770 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00007771 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007772#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007773 if (newstr == NULL) {
7774 PyErr_NoMemory();
7775 goto error;
7776 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007777#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007778 newenv = PyUnicode_AsUnicode(newstr);
Victor Stinnereb5657a2011-09-30 01:44:27 +02007779 if (newenv == NULL)
7780 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007781 _snwprintf(newenv, len, L"%s=%s", s1, s2);
7782 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007783 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007784 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007785 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007786#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007787 newenv = PyBytes_AS_STRING(newstr);
7788 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
7789 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007790 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007791 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007792 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007793#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007794
Victor Stinner8c62be82010-05-06 00:08:46 +00007795 /* Install the first arg and newstr in posix_putenv_garbage;
7796 * this will cause previous value to be collected. This has to
7797 * happen after the real putenv() call because the old value
7798 * was still accessible until then. */
7799 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007800#ifdef MS_WINDOWS
7801 PyTuple_GET_ITEM(args, 0),
7802#else
7803 os1,
7804#endif
7805 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007806 /* really not much we can do; just leak */
7807 PyErr_Clear();
7808 }
7809 else {
7810 Py_DECREF(newstr);
7811 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007812
7813#if defined(PYOS_OS2)
7814 }
7815#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007816
Martin v. Löwis011e8422009-05-05 04:43:17 +00007817#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007818 Py_DECREF(os1);
7819 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007820#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007821 Py_RETURN_NONE;
7822
7823error:
7824#ifndef MS_WINDOWS
7825 Py_DECREF(os1);
7826 Py_DECREF(os2);
7827#endif
7828 Py_XDECREF(newstr);
7829 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007830}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007831#endif /* putenv */
7832
Guido van Rossumc524d952001-10-19 01:31:59 +00007833#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007834PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007835"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007836Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00007837
7838static PyObject *
7839posix_unsetenv(PyObject *self, PyObject *args)
7840{
Victor Stinner84ae1182010-05-06 22:05:07 +00007841#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007842 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00007843
Victor Stinner8c62be82010-05-06 00:08:46 +00007844 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
7845 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00007846#else
7847 PyObject *os1;
7848 char *s1;
7849
7850 if (!PyArg_ParseTuple(args, "O&:unsetenv",
7851 PyUnicode_FSConverter, &os1))
7852 return NULL;
7853 s1 = PyBytes_AsString(os1);
7854#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007855
Victor Stinner8c62be82010-05-06 00:08:46 +00007856 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00007857
Victor Stinner8c62be82010-05-06 00:08:46 +00007858 /* Remove the key from posix_putenv_garbage;
7859 * this will cause it to be collected. This has to
7860 * happen after the real unsetenv() call because the
7861 * old value was still accessible until then.
7862 */
7863 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007864#ifdef MS_WINDOWS
7865 PyTuple_GET_ITEM(args, 0)
7866#else
7867 os1
7868#endif
7869 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007870 /* really not much we can do; just leak */
7871 PyErr_Clear();
7872 }
Guido van Rossumc524d952001-10-19 01:31:59 +00007873
Victor Stinner84ae1182010-05-06 22:05:07 +00007874#ifndef MS_WINDOWS
7875 Py_DECREF(os1);
7876#endif
7877 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00007878}
7879#endif /* unsetenv */
7880
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007881PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007882"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007883Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007884
Guido van Rossumf68d8e52001-04-14 17:55:09 +00007885static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007886posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00007887{
Victor Stinner8c62be82010-05-06 00:08:46 +00007888 int code;
7889 char *message;
7890 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7891 return NULL;
7892 message = strerror(code);
7893 if (message == NULL) {
7894 PyErr_SetString(PyExc_ValueError,
7895 "strerror() argument out of range");
7896 return NULL;
7897 }
7898 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00007899}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007900
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007901
Guido van Rossumc9641791998-08-04 15:26:23 +00007902#ifdef HAVE_SYS_WAIT_H
7903
Fred Drake106c1a02002-04-23 15:58:02 +00007904#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007905PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007906"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007907Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007908
7909static PyObject *
7910posix_WCOREDUMP(PyObject *self, PyObject *args)
7911{
Victor Stinner8c62be82010-05-06 00:08:46 +00007912 WAIT_TYPE status;
7913 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007914
Victor Stinner8c62be82010-05-06 00:08:46 +00007915 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7916 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007917
Victor Stinner8c62be82010-05-06 00:08:46 +00007918 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007919}
7920#endif /* WCOREDUMP */
7921
7922#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007923PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007924"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007925Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007926job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007927
7928static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007929posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007930{
Victor Stinner8c62be82010-05-06 00:08:46 +00007931 WAIT_TYPE status;
7932 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007933
Victor Stinner8c62be82010-05-06 00:08:46 +00007934 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7935 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007936
Victor Stinner8c62be82010-05-06 00:08:46 +00007937 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007938}
7939#endif /* WIFCONTINUED */
7940
Guido van Rossumc9641791998-08-04 15:26:23 +00007941#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007942PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007943"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007944Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007945
7946static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007947posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007948{
Victor Stinner8c62be82010-05-06 00:08:46 +00007949 WAIT_TYPE status;
7950 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007951
Victor Stinner8c62be82010-05-06 00:08:46 +00007952 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7953 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007954
Victor Stinner8c62be82010-05-06 00:08:46 +00007955 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007956}
7957#endif /* WIFSTOPPED */
7958
7959#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007960PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007961"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007962Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007963
7964static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007965posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007966{
Victor Stinner8c62be82010-05-06 00:08:46 +00007967 WAIT_TYPE status;
7968 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007969
Victor Stinner8c62be82010-05-06 00:08:46 +00007970 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7971 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007972
Victor Stinner8c62be82010-05-06 00:08:46 +00007973 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007974}
7975#endif /* WIFSIGNALED */
7976
7977#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007978PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007979"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007980Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007981system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007982
7983static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007984posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007985{
Victor Stinner8c62be82010-05-06 00:08:46 +00007986 WAIT_TYPE status;
7987 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007988
Victor Stinner8c62be82010-05-06 00:08:46 +00007989 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7990 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007991
Victor Stinner8c62be82010-05-06 00:08:46 +00007992 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007993}
7994#endif /* WIFEXITED */
7995
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007996#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007997PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007998"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007999Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008000
8001static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008002posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008003{
Victor Stinner8c62be82010-05-06 00:08:46 +00008004 WAIT_TYPE status;
8005 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008006
Victor Stinner8c62be82010-05-06 00:08:46 +00008007 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
8008 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008009
Victor Stinner8c62be82010-05-06 00:08:46 +00008010 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008011}
8012#endif /* WEXITSTATUS */
8013
8014#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008015PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008016"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00008017Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008018value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008019
8020static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008021posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008022{
Victor Stinner8c62be82010-05-06 00:08:46 +00008023 WAIT_TYPE status;
8024 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008025
Victor Stinner8c62be82010-05-06 00:08:46 +00008026 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
8027 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008028
Victor Stinner8c62be82010-05-06 00:08:46 +00008029 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008030}
8031#endif /* WTERMSIG */
8032
8033#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008034PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008035"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008036Return the signal that stopped the process that provided\n\
8037the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008038
8039static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008040posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008041{
Victor Stinner8c62be82010-05-06 00:08:46 +00008042 WAIT_TYPE status;
8043 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008044
Victor Stinner8c62be82010-05-06 00:08:46 +00008045 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
8046 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008047
Victor Stinner8c62be82010-05-06 00:08:46 +00008048 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008049}
8050#endif /* WSTOPSIG */
8051
8052#endif /* HAVE_SYS_WAIT_H */
8053
8054
Thomas Wouters477c8d52006-05-27 19:21:47 +00008055#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00008056#ifdef _SCO_DS
8057/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
8058 needed definitions in sys/statvfs.h */
8059#define _SVID3
8060#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008061#include <sys/statvfs.h>
8062
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008063static PyObject*
8064_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008065 PyObject *v = PyStructSequence_New(&StatVFSResultType);
8066 if (v == NULL)
8067 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008068
8069#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00008070 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8071 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8072 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
8073 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
8074 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
8075 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
8076 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
8077 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
8078 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8079 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008080#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008081 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8082 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8083 PyStructSequence_SET_ITEM(v, 2,
8084 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
8085 PyStructSequence_SET_ITEM(v, 3,
8086 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
8087 PyStructSequence_SET_ITEM(v, 4,
8088 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
8089 PyStructSequence_SET_ITEM(v, 5,
8090 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
8091 PyStructSequence_SET_ITEM(v, 6,
8092 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
8093 PyStructSequence_SET_ITEM(v, 7,
8094 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
8095 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8096 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008097#endif
8098
Victor Stinner8c62be82010-05-06 00:08:46 +00008099 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008100}
8101
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008102PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008103"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008104Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008105
8106static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008107posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008108{
Victor Stinner8c62be82010-05-06 00:08:46 +00008109 int fd, res;
8110 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008111
Victor Stinner8c62be82010-05-06 00:08:46 +00008112 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
8113 return NULL;
8114 Py_BEGIN_ALLOW_THREADS
8115 res = fstatvfs(fd, &st);
8116 Py_END_ALLOW_THREADS
8117 if (res != 0)
8118 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008119
Victor Stinner8c62be82010-05-06 00:08:46 +00008120 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008121}
Thomas Wouters477c8d52006-05-27 19:21:47 +00008122#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008123
8124
Thomas Wouters477c8d52006-05-27 19:21:47 +00008125#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008126#include <sys/statvfs.h>
8127
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008128PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008129"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008130Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008131
8132static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008133posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008134{
Victor Stinner6fa67772011-09-20 04:04:33 +02008135 PyObject *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00008136 int res;
8137 struct statvfs st;
Victor Stinner6fa67772011-09-20 04:04:33 +02008138 if (!PyArg_ParseTuple(args, "O&:statvfs", PyUnicode_FSConverter, &path))
Victor Stinner8c62be82010-05-06 00:08:46 +00008139 return NULL;
8140 Py_BEGIN_ALLOW_THREADS
Victor Stinner6fa67772011-09-20 04:04:33 +02008141 res = statvfs(PyBytes_AS_STRING(path), &st);
Victor Stinner8c62be82010-05-06 00:08:46 +00008142 Py_END_ALLOW_THREADS
Victor Stinner6fa67772011-09-20 04:04:33 +02008143 if (res != 0) {
8144 posix_error_with_filename(PyBytes_AS_STRING(path));
8145 Py_DECREF(path);
8146 return NULL;
8147 }
8148 Py_DECREF(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008149
Victor Stinner8c62be82010-05-06 00:08:46 +00008150 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008151}
8152#endif /* HAVE_STATVFS */
8153
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008154#ifdef MS_WINDOWS
8155PyDoc_STRVAR(win32__getdiskusage__doc__,
8156"_getdiskusage(path) -> (total, free)\n\n\
8157Return disk usage statistics about the given path as (total, free) tuple.");
8158
8159static PyObject *
8160win32__getdiskusage(PyObject *self, PyObject *args)
8161{
8162 BOOL retval;
8163 ULARGE_INTEGER _, total, free;
8164 LPCTSTR path;
8165
8166 if (! PyArg_ParseTuple(args, "s", &path))
8167 return NULL;
8168
8169 Py_BEGIN_ALLOW_THREADS
8170 retval = GetDiskFreeSpaceEx(path, &_, &total, &free);
8171 Py_END_ALLOW_THREADS
8172 if (retval == 0)
8173 return PyErr_SetFromWindowsErr(0);
8174
8175 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
8176}
8177#endif
8178
8179
Fred Drakec9680921999-12-13 16:37:25 +00008180/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
8181 * It maps strings representing configuration variable names to
8182 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00008183 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00008184 * rarely-used constants. There are three separate tables that use
8185 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00008186 *
8187 * This code is always included, even if none of the interfaces that
8188 * need it are included. The #if hackery needed to avoid it would be
8189 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00008190 */
8191struct constdef {
8192 char *name;
8193 long value;
8194};
8195
Fred Drake12c6e2d1999-12-14 21:25:03 +00008196static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008197conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00008198 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00008199{
Christian Heimes217cfd12007-12-02 14:31:20 +00008200 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008201 *valuep = PyLong_AS_LONG(arg);
8202 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00008203 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00008204 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00008205 /* look up the value in the table using a binary search */
8206 size_t lo = 0;
8207 size_t mid;
8208 size_t hi = tablesize;
8209 int cmp;
8210 const char *confname;
8211 if (!PyUnicode_Check(arg)) {
8212 PyErr_SetString(PyExc_TypeError,
8213 "configuration names must be strings or integers");
8214 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008215 }
Stefan Krah0e803b32010-11-26 16:16:47 +00008216 confname = _PyUnicode_AsString(arg);
8217 if (confname == NULL)
8218 return 0;
8219 while (lo < hi) {
8220 mid = (lo + hi) / 2;
8221 cmp = strcmp(confname, table[mid].name);
8222 if (cmp < 0)
8223 hi = mid;
8224 else if (cmp > 0)
8225 lo = mid + 1;
8226 else {
8227 *valuep = table[mid].value;
8228 return 1;
8229 }
8230 }
8231 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
8232 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008233 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00008234}
8235
8236
8237#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
8238static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008239#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008240 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008241#endif
8242#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008243 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008244#endif
Fred Drakec9680921999-12-13 16:37:25 +00008245#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008246 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008247#endif
8248#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00008249 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00008250#endif
8251#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00008252 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00008253#endif
8254#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00008255 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00008256#endif
8257#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008258 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008259#endif
8260#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00008261 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00008262#endif
8263#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008264 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00008265#endif
8266#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008267 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008268#endif
8269#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008270 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00008271#endif
8272#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008273 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008274#endif
8275#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008276 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00008277#endif
8278#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008279 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008280#endif
8281#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008282 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00008283#endif
8284#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008285 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008286#endif
8287#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008288 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00008289#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00008290#ifdef _PC_ACL_ENABLED
8291 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
8292#endif
8293#ifdef _PC_MIN_HOLE_SIZE
8294 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
8295#endif
8296#ifdef _PC_ALLOC_SIZE_MIN
8297 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
8298#endif
8299#ifdef _PC_REC_INCR_XFER_SIZE
8300 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
8301#endif
8302#ifdef _PC_REC_MAX_XFER_SIZE
8303 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
8304#endif
8305#ifdef _PC_REC_MIN_XFER_SIZE
8306 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
8307#endif
8308#ifdef _PC_REC_XFER_ALIGN
8309 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
8310#endif
8311#ifdef _PC_SYMLINK_MAX
8312 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
8313#endif
8314#ifdef _PC_XATTR_ENABLED
8315 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
8316#endif
8317#ifdef _PC_XATTR_EXISTS
8318 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
8319#endif
8320#ifdef _PC_TIMESTAMP_RESOLUTION
8321 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
8322#endif
Fred Drakec9680921999-12-13 16:37:25 +00008323};
8324
Fred Drakec9680921999-12-13 16:37:25 +00008325static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008326conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008327{
8328 return conv_confname(arg, valuep, posix_constants_pathconf,
8329 sizeof(posix_constants_pathconf)
8330 / sizeof(struct constdef));
8331}
8332#endif
8333
8334#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008335PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008336"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008337Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008338If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008339
8340static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008341posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008342{
8343 PyObject *result = NULL;
8344 int name, fd;
8345
Fred Drake12c6e2d1999-12-14 21:25:03 +00008346 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
8347 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008348 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008349
Stefan Krah0e803b32010-11-26 16:16:47 +00008350 errno = 0;
8351 limit = fpathconf(fd, name);
8352 if (limit == -1 && errno != 0)
8353 posix_error();
8354 else
8355 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008356 }
8357 return result;
8358}
8359#endif
8360
8361
8362#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008363PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008364"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008365Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008366If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008367
8368static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008369posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008370{
8371 PyObject *result = NULL;
8372 int name;
8373 char *path;
8374
8375 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
8376 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008377 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008378
Victor Stinner8c62be82010-05-06 00:08:46 +00008379 errno = 0;
8380 limit = pathconf(path, name);
8381 if (limit == -1 && errno != 0) {
8382 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00008383 /* could be a path or name problem */
8384 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00008385 else
Stefan Krah99439262010-11-26 12:58:05 +00008386 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00008387 }
8388 else
8389 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008390 }
8391 return result;
8392}
8393#endif
8394
8395#ifdef HAVE_CONFSTR
8396static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008397#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00008398 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00008399#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00008400#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008401 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008402#endif
8403#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008404 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008405#endif
Fred Draked86ed291999-12-15 15:34:33 +00008406#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008407 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008408#endif
8409#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008410 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008411#endif
8412#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008413 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008414#endif
8415#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008416 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008417#endif
Fred Drakec9680921999-12-13 16:37:25 +00008418#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008419 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008420#endif
8421#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008422 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008423#endif
8424#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008425 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008426#endif
8427#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008428 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008429#endif
8430#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008431 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008432#endif
8433#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008434 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008435#endif
8436#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008437 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008438#endif
8439#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008440 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008441#endif
Fred Draked86ed291999-12-15 15:34:33 +00008442#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00008443 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00008444#endif
Fred Drakec9680921999-12-13 16:37:25 +00008445#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00008446 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00008447#endif
Fred Draked86ed291999-12-15 15:34:33 +00008448#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008449 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00008450#endif
8451#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008452 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00008453#endif
8454#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008455 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008456#endif
8457#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008458 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00008459#endif
Fred Drakec9680921999-12-13 16:37:25 +00008460#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008461 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008462#endif
8463#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008464 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008465#endif
8466#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008467 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008468#endif
8469#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008470 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008471#endif
8472#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008473 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008474#endif
8475#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008476 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008477#endif
8478#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008479 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008480#endif
8481#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008482 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008483#endif
8484#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008485 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008486#endif
8487#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008488 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008489#endif
8490#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008491 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008492#endif
8493#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008494 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008495#endif
8496#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008497 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008498#endif
8499#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008500 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008501#endif
8502#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008503 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008504#endif
8505#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008506 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008507#endif
Fred Draked86ed291999-12-15 15:34:33 +00008508#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008509 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008510#endif
8511#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008512 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00008513#endif
8514#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00008515 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00008516#endif
8517#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008518 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008519#endif
8520#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008521 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008522#endif
8523#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00008524 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00008525#endif
8526#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008527 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00008528#endif
8529#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00008530 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00008531#endif
8532#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008533 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008534#endif
8535#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008536 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008537#endif
8538#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008539 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008540#endif
8541#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008542 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008543#endif
8544#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00008545 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00008546#endif
Fred Drakec9680921999-12-13 16:37:25 +00008547};
8548
8549static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008550conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008551{
8552 return conv_confname(arg, valuep, posix_constants_confstr,
8553 sizeof(posix_constants_confstr)
8554 / sizeof(struct constdef));
8555}
8556
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008557PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008558"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008559Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008560
8561static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008562posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008563{
8564 PyObject *result = NULL;
8565 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00008566 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00008567 int len;
Fred Drakec9680921999-12-13 16:37:25 +00008568
Victor Stinnercb043522010-09-10 23:49:04 +00008569 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
8570 return NULL;
8571
8572 errno = 0;
8573 len = confstr(name, buffer, sizeof(buffer));
8574 if (len == 0) {
8575 if (errno) {
8576 posix_error();
8577 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00008578 }
8579 else {
Victor Stinnercb043522010-09-10 23:49:04 +00008580 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00008581 }
8582 }
Victor Stinnercb043522010-09-10 23:49:04 +00008583
8584 if ((unsigned int)len >= sizeof(buffer)) {
8585 char *buf = PyMem_Malloc(len);
8586 if (buf == NULL)
8587 return PyErr_NoMemory();
8588 confstr(name, buf, len);
8589 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
8590 PyMem_Free(buf);
8591 }
8592 else
8593 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00008594 return result;
8595}
8596#endif
8597
8598
8599#ifdef HAVE_SYSCONF
8600static struct constdef posix_constants_sysconf[] = {
8601#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008602 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00008603#endif
8604#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00008605 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00008606#endif
8607#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008608 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008609#endif
8610#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008611 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008612#endif
8613#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008614 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008615#endif
8616#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00008617 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00008618#endif
8619#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00008620 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00008621#endif
8622#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008623 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008624#endif
8625#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00008626 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00008627#endif
8628#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008629 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008630#endif
Fred Draked86ed291999-12-15 15:34:33 +00008631#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008632 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008633#endif
8634#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00008635 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00008636#endif
Fred Drakec9680921999-12-13 16:37:25 +00008637#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008638 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008639#endif
Fred Drakec9680921999-12-13 16:37:25 +00008640#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008641 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008642#endif
8643#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008644 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008645#endif
8646#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008647 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008648#endif
8649#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008650 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008651#endif
8652#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008653 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008654#endif
Fred Draked86ed291999-12-15 15:34:33 +00008655#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008656 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00008657#endif
Fred Drakec9680921999-12-13 16:37:25 +00008658#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008659 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008660#endif
8661#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008662 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008663#endif
8664#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008665 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008666#endif
8667#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008668 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008669#endif
8670#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008671 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008672#endif
Fred Draked86ed291999-12-15 15:34:33 +00008673#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00008674 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00008675#endif
Fred Drakec9680921999-12-13 16:37:25 +00008676#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008677 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008678#endif
8679#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008680 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008681#endif
8682#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008683 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008684#endif
8685#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008686 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008687#endif
8688#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008689 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008690#endif
8691#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008692 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00008693#endif
8694#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008695 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008696#endif
8697#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008698 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008699#endif
8700#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008701 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008702#endif
8703#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008704 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008705#endif
8706#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008707 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008708#endif
8709#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008710 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008711#endif
8712#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008713 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008714#endif
8715#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008716 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008717#endif
8718#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008719 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008720#endif
8721#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008722 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008723#endif
8724#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008725 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00008726#endif
8727#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008728 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008729#endif
8730#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008731 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008732#endif
8733#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008734 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008735#endif
8736#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008737 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008738#endif
8739#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008740 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008741#endif
8742#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008743 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008744#endif
Fred Draked86ed291999-12-15 15:34:33 +00008745#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00008746 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00008747#endif
Fred Drakec9680921999-12-13 16:37:25 +00008748#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008749 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008750#endif
8751#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008752 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008753#endif
8754#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008755 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008756#endif
Fred Draked86ed291999-12-15 15:34:33 +00008757#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008758 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00008759#endif
Fred Drakec9680921999-12-13 16:37:25 +00008760#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00008761 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00008762#endif
Fred Draked86ed291999-12-15 15:34:33 +00008763#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008764 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00008765#endif
8766#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00008767 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00008768#endif
Fred Drakec9680921999-12-13 16:37:25 +00008769#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008770 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008771#endif
8772#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008773 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008774#endif
8775#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008776 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008777#endif
8778#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008779 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008780#endif
Fred Draked86ed291999-12-15 15:34:33 +00008781#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00008782 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00008783#endif
Fred Drakec9680921999-12-13 16:37:25 +00008784#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00008785 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00008786#endif
8787#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00008788 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00008789#endif
8790#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008791 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008792#endif
8793#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008794 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00008795#endif
8796#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008797 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00008798#endif
8799#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00008800 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00008801#endif
8802#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00008803 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00008804#endif
Fred Draked86ed291999-12-15 15:34:33 +00008805#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00008806 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00008807#endif
Fred Drakec9680921999-12-13 16:37:25 +00008808#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008809 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008810#endif
8811#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008812 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008813#endif
Fred Draked86ed291999-12-15 15:34:33 +00008814#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008815 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008816#endif
Fred Drakec9680921999-12-13 16:37:25 +00008817#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008818 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008819#endif
8820#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008821 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008822#endif
8823#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008824 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008825#endif
8826#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008827 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008828#endif
8829#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008830 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008831#endif
8832#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008833 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008834#endif
8835#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008836 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008837#endif
8838#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008839 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00008840#endif
8841#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008842 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00008843#endif
Fred Draked86ed291999-12-15 15:34:33 +00008844#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008845 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00008846#endif
8847#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008848 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00008849#endif
Fred Drakec9680921999-12-13 16:37:25 +00008850#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00008851 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00008852#endif
8853#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008854 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008855#endif
8856#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008857 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008858#endif
8859#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008860 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008861#endif
8862#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008863 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008864#endif
8865#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008866 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008867#endif
8868#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00008869 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00008870#endif
8871#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00008872 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00008873#endif
8874#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008875 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00008876#endif
8877#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008878 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00008879#endif
8880#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00008881 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00008882#endif
8883#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008884 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00008885#endif
8886#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008887 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00008888#endif
8889#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00008890 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00008891#endif
8892#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00008893 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00008894#endif
8895#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00008896 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00008897#endif
8898#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00008899 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00008900#endif
8901#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008902 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008903#endif
8904#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008905 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008906#endif
8907#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00008908 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008909#endif
8910#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008911 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008912#endif
8913#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008914 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008915#endif
8916#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00008917 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008918#endif
8919#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008920 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008921#endif
8922#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008923 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008924#endif
8925#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008926 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008927#endif
8928#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00008929 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008930#endif
8931#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008932 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008933#endif
8934#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008935 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008936#endif
8937#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008938 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008939#endif
8940#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008941 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008942#endif
8943#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008944 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008945#endif
8946#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008947 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008948#endif
8949#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008950 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008951#endif
8952#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008953 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008954#endif
Fred Draked86ed291999-12-15 15:34:33 +00008955#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00008956 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008957#endif
Fred Drakec9680921999-12-13 16:37:25 +00008958#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00008959 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008960#endif
8961#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008962 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008963#endif
8964#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00008965 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008966#endif
8967#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008968 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008969#endif
8970#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008971 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008972#endif
8973#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008974 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008975#endif
8976#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00008977 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008978#endif
8979#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008980 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008981#endif
8982#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008983 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008984#endif
8985#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008986 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008987#endif
8988#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008989 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008990#endif
8991#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008992 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008993#endif
8994#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008995 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008996#endif
8997#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00008998 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008999#endif
9000#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00009001 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00009002#endif
9003#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009004 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009005#endif
9006#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009007 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009008#endif
9009#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00009010 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00009011#endif
9012#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009013 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009014#endif
9015#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009016 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009017#endif
9018#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009019 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009020#endif
9021#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009022 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009023#endif
9024#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009025 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009026#endif
9027#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009028 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009029#endif
9030#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00009031 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00009032#endif
9033#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009034 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009035#endif
9036#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009037 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009038#endif
9039#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009040 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009041#endif
9042#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009043 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009044#endif
9045#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00009046 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00009047#endif
9048#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009049 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00009050#endif
9051#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00009052 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00009053#endif
9054#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009055 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00009056#endif
9057#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00009058 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00009059#endif
9060#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00009061 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00009062#endif
9063#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00009064 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00009065#endif
9066#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00009067 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00009068#endif
9069#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00009070 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00009071#endif
9072#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00009073 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00009074#endif
9075#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00009076 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00009077#endif
9078#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009079 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009080#endif
9081#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009082 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009083#endif
9084#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00009085 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00009086#endif
9087#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00009088 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00009089#endif
9090#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00009091 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00009092#endif
9093};
9094
9095static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009096conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009097{
9098 return conv_confname(arg, valuep, posix_constants_sysconf,
9099 sizeof(posix_constants_sysconf)
9100 / sizeof(struct constdef));
9101}
9102
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009103PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009104"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009105Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00009106
9107static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009108posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00009109{
9110 PyObject *result = NULL;
9111 int name;
9112
9113 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
9114 int value;
9115
9116 errno = 0;
9117 value = sysconf(name);
9118 if (value == -1 && errno != 0)
9119 posix_error();
9120 else
Christian Heimes217cfd12007-12-02 14:31:20 +00009121 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00009122 }
9123 return result;
9124}
9125#endif
9126
9127
Fred Drakebec628d1999-12-15 18:31:10 +00009128/* This code is used to ensure that the tables of configuration value names
9129 * are in sorted order as required by conv_confname(), and also to build the
9130 * the exported dictionaries that are used to publish information about the
9131 * names available on the host platform.
9132 *
9133 * Sorting the table at runtime ensures that the table is properly ordered
9134 * when used, even for platforms we're not able to test on. It also makes
9135 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00009136 */
Fred Drakebec628d1999-12-15 18:31:10 +00009137
9138static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009139cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00009140{
9141 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009142 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00009143 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009144 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00009145
9146 return strcmp(c1->name, c2->name);
9147}
9148
9149static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009150setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00009151 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009152{
Fred Drakebec628d1999-12-15 18:31:10 +00009153 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00009154 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00009155
9156 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
9157 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00009158 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00009159 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009160
Barry Warsaw3155db32000-04-13 15:20:40 +00009161 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009162 PyObject *o = PyLong_FromLong(table[i].value);
9163 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
9164 Py_XDECREF(o);
9165 Py_DECREF(d);
9166 return -1;
9167 }
9168 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00009169 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00009170 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00009171}
9172
Fred Drakebec628d1999-12-15 18:31:10 +00009173/* Return -1 on failure, 0 on success. */
9174static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009175setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009176{
9177#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00009178 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00009179 sizeof(posix_constants_pathconf)
9180 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009181 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009182 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009183#endif
9184#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00009185 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00009186 sizeof(posix_constants_confstr)
9187 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009188 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009189 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009190#endif
9191#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00009192 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00009193 sizeof(posix_constants_sysconf)
9194 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009195 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009196 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009197#endif
Fred Drakebec628d1999-12-15 18:31:10 +00009198 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00009199}
Fred Draked86ed291999-12-15 15:34:33 +00009200
9201
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009202PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009203"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009204Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009205in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009206
9207static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009208posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009209{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009210 abort();
9211 /*NOTREACHED*/
9212 Py_FatalError("abort() called from Python code didn't abort!");
9213 return NULL;
9214}
Fred Drakebec628d1999-12-15 18:31:10 +00009215
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009216#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009217PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00009218"startfile(filepath [, operation]) - Start a file with its associated\n\
9219application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009220\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00009221When \"operation\" is not specified or \"open\", this acts like\n\
9222double-clicking the file in Explorer, or giving the file name as an\n\
9223argument to the DOS \"start\" command: the file is opened with whatever\n\
9224application (if any) its extension is associated.\n\
9225When another \"operation\" is given, it specifies what should be done with\n\
9226the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009227\n\
9228startfile returns as soon as the associated application is launched.\n\
9229There is no option to wait for the application to close, and no way\n\
9230to retrieve the application's exit status.\n\
9231\n\
9232The filepath is relative to the current directory. If you want to use\n\
9233an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009234the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00009235
9236static PyObject *
9237win32_startfile(PyObject *self, PyObject *args)
9238{
Victor Stinner8c62be82010-05-06 00:08:46 +00009239 PyObject *ofilepath;
9240 char *filepath;
9241 char *operation = NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02009242 wchar_t *wpath, *woperation;
Victor Stinner8c62be82010-05-06 00:08:46 +00009243 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00009244
Victor Stinnereb5657a2011-09-30 01:44:27 +02009245 PyObject *unipath, *uoperation = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00009246 if (!PyArg_ParseTuple(args, "U|s:startfile",
9247 &unipath, &operation)) {
9248 PyErr_Clear();
9249 goto normal;
9250 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009251
Victor Stinner8c62be82010-05-06 00:08:46 +00009252 if (operation) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02009253 uoperation = PyUnicode_DecodeASCII(operation,
Victor Stinner8c62be82010-05-06 00:08:46 +00009254 strlen(operation), NULL);
Victor Stinnereb5657a2011-09-30 01:44:27 +02009255 if (!uoperation) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009256 PyErr_Clear();
9257 operation = NULL;
9258 goto normal;
9259 }
9260 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009261
Victor Stinnereb5657a2011-09-30 01:44:27 +02009262 wpath = PyUnicode_AsUnicode(unipath);
9263 if (wpath == NULL)
9264 goto normal;
9265 if (uoperation) {
9266 woperation = PyUnicode_AsUnicode(uoperation);
9267 if (woperation == NULL)
9268 goto normal;
9269 }
9270 else
9271 woperation = NULL;
9272
Victor Stinner8c62be82010-05-06 00:08:46 +00009273 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02009274 rc = ShellExecuteW((HWND)0, woperation, wpath,
9275 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +00009276 Py_END_ALLOW_THREADS
9277
Victor Stinnereb5657a2011-09-30 01:44:27 +02009278 Py_XDECREF(uoperation);
Victor Stinner8c62be82010-05-06 00:08:46 +00009279 if (rc <= (HINSTANCE)32) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02009280 win32_error_object("startfile", unipath);
9281 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00009282 }
9283 Py_INCREF(Py_None);
9284 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009285
9286normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00009287 if (!PyArg_ParseTuple(args, "O&|s:startfile",
9288 PyUnicode_FSConverter, &ofilepath,
9289 &operation))
9290 return NULL;
9291 filepath = PyBytes_AsString(ofilepath);
9292 Py_BEGIN_ALLOW_THREADS
9293 rc = ShellExecute((HWND)0, operation, filepath,
9294 NULL, NULL, SW_SHOWNORMAL);
9295 Py_END_ALLOW_THREADS
9296 if (rc <= (HINSTANCE)32) {
9297 PyObject *errval = win32_error("startfile", filepath);
9298 Py_DECREF(ofilepath);
9299 return errval;
9300 }
9301 Py_DECREF(ofilepath);
9302 Py_INCREF(Py_None);
9303 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00009304}
9305#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009306
Martin v. Löwis438b5342002-12-27 10:16:42 +00009307#ifdef HAVE_GETLOADAVG
9308PyDoc_STRVAR(posix_getloadavg__doc__,
9309"getloadavg() -> (float, float, float)\n\n\
9310Return the number of processes in the system run queue averaged over\n\
9311the last 1, 5, and 15 minutes or raises OSError if the load average\n\
9312was unobtainable");
9313
9314static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009315posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00009316{
9317 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00009318 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00009319 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
9320 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00009321 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00009322 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00009323}
9324#endif
9325
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009326#ifdef MS_WINDOWS
9327
9328PyDoc_STRVAR(win32_urandom__doc__,
9329"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009330Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009331
9332typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
9333 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
9334 DWORD dwFlags );
9335typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
9336 BYTE *pbBuffer );
9337
9338static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00009339/* This handle is never explicitly released. Instead, the operating
9340 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009341static HCRYPTPROV hCryptProv = 0;
9342
Tim Peters4ad82172004-08-30 17:02:04 +00009343static PyObject*
9344win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009345{
Victor Stinner8c62be82010-05-06 00:08:46 +00009346 int howMany;
9347 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009348
Victor Stinner8c62be82010-05-06 00:08:46 +00009349 /* Read arguments */
9350 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9351 return NULL;
9352 if (howMany < 0)
9353 return PyErr_Format(PyExc_ValueError,
9354 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009355
Victor Stinner8c62be82010-05-06 00:08:46 +00009356 if (hCryptProv == 0) {
9357 HINSTANCE hAdvAPI32 = NULL;
9358 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009359
Victor Stinner8c62be82010-05-06 00:08:46 +00009360 /* Obtain handle to the DLL containing CryptoAPI
9361 This should not fail */
9362 hAdvAPI32 = GetModuleHandle("advapi32.dll");
9363 if(hAdvAPI32 == NULL)
9364 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009365
Victor Stinner8c62be82010-05-06 00:08:46 +00009366 /* Obtain pointers to the CryptoAPI functions
9367 This will fail on some early versions of Win95 */
9368 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
9369 hAdvAPI32,
9370 "CryptAcquireContextA");
9371 if (pCryptAcquireContext == NULL)
9372 return PyErr_Format(PyExc_NotImplementedError,
9373 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009374
Victor Stinner8c62be82010-05-06 00:08:46 +00009375 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
9376 hAdvAPI32, "CryptGenRandom");
9377 if (pCryptGenRandom == NULL)
9378 return PyErr_Format(PyExc_NotImplementedError,
9379 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009380
Victor Stinner8c62be82010-05-06 00:08:46 +00009381 /* Acquire context */
9382 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
9383 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
9384 return win32_error("CryptAcquireContext", NULL);
9385 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009386
Victor Stinner8c62be82010-05-06 00:08:46 +00009387 /* Allocate bytes */
9388 result = PyBytes_FromStringAndSize(NULL, howMany);
9389 if (result != NULL) {
9390 /* Get random data */
9391 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
9392 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
9393 PyBytes_AS_STRING(result))) {
9394 Py_DECREF(result);
9395 return win32_error("CryptGenRandom", NULL);
9396 }
9397 }
9398 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009399}
9400#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00009401
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009402PyDoc_STRVAR(device_encoding__doc__,
9403"device_encoding(fd) -> str\n\n\
9404Return a string describing the encoding of the device\n\
9405if the output is a terminal; else return None.");
9406
9407static PyObject *
9408device_encoding(PyObject *self, PyObject *args)
9409{
Victor Stinner8c62be82010-05-06 00:08:46 +00009410 int fd;
Victor Stinner7870bdf2011-05-23 18:12:52 +02009411#if defined(MS_WINDOWS) || defined(MS_WIN64)
9412 UINT cp;
9413#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009414 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
9415 return NULL;
9416 if (!_PyVerify_fd(fd) || !isatty(fd)) {
9417 Py_INCREF(Py_None);
9418 return Py_None;
9419 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009420#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner7870bdf2011-05-23 18:12:52 +02009421 if (fd == 0)
9422 cp = GetConsoleCP();
9423 else if (fd == 1 || fd == 2)
9424 cp = GetConsoleOutputCP();
9425 else
9426 cp = 0;
9427 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
9428 has no console */
9429 if (cp != 0)
9430 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009431#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00009432 {
9433 char *codeset = nl_langinfo(CODESET);
9434 if (codeset != NULL && codeset[0] != 0)
9435 return PyUnicode_FromString(codeset);
9436 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009437#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009438 Py_INCREF(Py_None);
9439 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009440}
9441
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009442#ifdef __VMS
9443/* Use openssl random routine */
9444#include <openssl/rand.h>
9445PyDoc_STRVAR(vms_urandom__doc__,
9446"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009447Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009448
9449static PyObject*
9450vms_urandom(PyObject *self, PyObject *args)
9451{
Victor Stinner8c62be82010-05-06 00:08:46 +00009452 int howMany;
9453 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009454
Victor Stinner8c62be82010-05-06 00:08:46 +00009455 /* Read arguments */
9456 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9457 return NULL;
9458 if (howMany < 0)
9459 return PyErr_Format(PyExc_ValueError,
9460 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009461
Victor Stinner8c62be82010-05-06 00:08:46 +00009462 /* Allocate bytes */
9463 result = PyBytes_FromStringAndSize(NULL, howMany);
9464 if (result != NULL) {
9465 /* Get random data */
9466 if (RAND_pseudo_bytes((unsigned char*)
9467 PyBytes_AS_STRING(result),
9468 howMany) < 0) {
9469 Py_DECREF(result);
9470 return PyErr_Format(PyExc_ValueError,
9471 "RAND_pseudo_bytes");
9472 }
9473 }
9474 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009475}
9476#endif
9477
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009478#ifdef HAVE_SETRESUID
9479PyDoc_STRVAR(posix_setresuid__doc__,
9480"setresuid(ruid, euid, suid)\n\n\
9481Set the current process's real, effective, and saved user ids.");
9482
9483static PyObject*
9484posix_setresuid (PyObject *self, PyObject *args)
9485{
Victor Stinner8c62be82010-05-06 00:08:46 +00009486 /* We assume uid_t is no larger than a long. */
9487 long ruid, euid, suid;
9488 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
9489 return NULL;
9490 if (setresuid(ruid, euid, suid) < 0)
9491 return posix_error();
9492 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009493}
9494#endif
9495
9496#ifdef HAVE_SETRESGID
9497PyDoc_STRVAR(posix_setresgid__doc__,
9498"setresgid(rgid, egid, sgid)\n\n\
9499Set the current process's real, effective, and saved group ids.");
9500
9501static PyObject*
9502posix_setresgid (PyObject *self, PyObject *args)
9503{
Victor Stinner8c62be82010-05-06 00:08:46 +00009504 /* We assume uid_t is no larger than a long. */
9505 long rgid, egid, sgid;
9506 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
9507 return NULL;
9508 if (setresgid(rgid, egid, sgid) < 0)
9509 return posix_error();
9510 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009511}
9512#endif
9513
9514#ifdef HAVE_GETRESUID
9515PyDoc_STRVAR(posix_getresuid__doc__,
9516"getresuid() -> (ruid, euid, suid)\n\n\
9517Get tuple of the current process's real, effective, and saved user ids.");
9518
9519static PyObject*
9520posix_getresuid (PyObject *self, PyObject *noargs)
9521{
Victor Stinner8c62be82010-05-06 00:08:46 +00009522 uid_t ruid, euid, suid;
9523 long l_ruid, l_euid, l_suid;
9524 if (getresuid(&ruid, &euid, &suid) < 0)
9525 return posix_error();
9526 /* Force the values into long's as we don't know the size of uid_t. */
9527 l_ruid = ruid;
9528 l_euid = euid;
9529 l_suid = suid;
9530 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009531}
9532#endif
9533
9534#ifdef HAVE_GETRESGID
9535PyDoc_STRVAR(posix_getresgid__doc__,
9536"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00009537Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009538
9539static PyObject*
9540posix_getresgid (PyObject *self, PyObject *noargs)
9541{
Victor Stinner8c62be82010-05-06 00:08:46 +00009542 uid_t rgid, egid, sgid;
9543 long l_rgid, l_egid, l_sgid;
9544 if (getresgid(&rgid, &egid, &sgid) < 0)
9545 return posix_error();
9546 /* Force the values into long's as we don't know the size of uid_t. */
9547 l_rgid = rgid;
9548 l_egid = egid;
9549 l_sgid = sgid;
9550 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009551}
9552#endif
9553
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009554/* Posix *at family of functions:
9555 faccessat, fchmodat, fchownat, fstatat, futimesat,
9556 linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat,
9557 unlinkat, utimensat, mkfifoat */
9558
9559#ifdef HAVE_FACCESSAT
9560PyDoc_STRVAR(posix_faccessat__doc__,
9561"faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\
9562Like access() but if path is relative, it is taken as relative to dirfd.\n\
9563flags is optional and can be constructed by ORing together zero or more\n\
9564of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\
9565If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9566is interpreted relative to the current working directory.");
9567
9568static PyObject *
9569posix_faccessat(PyObject *self, PyObject *args)
9570{
9571 PyObject *opath;
9572 char *path;
9573 int mode;
9574 int res;
9575 int dirfd, flags = 0;
9576 if (!PyArg_ParseTuple(args, "iO&i|i:faccessat",
9577 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9578 return NULL;
9579 path = PyBytes_AsString(opath);
9580 Py_BEGIN_ALLOW_THREADS
9581 res = faccessat(dirfd, path, mode, flags);
9582 Py_END_ALLOW_THREADS
9583 Py_DECREF(opath);
9584 return PyBool_FromLong(res == 0);
9585}
9586#endif
9587
9588#ifdef HAVE_FCHMODAT
9589PyDoc_STRVAR(posix_fchmodat__doc__,
9590"fchmodat(dirfd, path, mode, flags=0)\n\n\
9591Like chmod() but if path is relative, it is taken as relative to dirfd.\n\
9592flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9593If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9594is interpreted relative to the current working directory.");
9595
9596static PyObject *
9597posix_fchmodat(PyObject *self, PyObject *args)
9598{
9599 int dirfd, mode, res;
9600 int flags = 0;
9601 PyObject *opath;
9602 char *path;
9603
9604 if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat",
9605 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9606 return NULL;
9607
9608 path = PyBytes_AsString(opath);
9609
9610 Py_BEGIN_ALLOW_THREADS
9611 res = fchmodat(dirfd, path, mode, flags);
9612 Py_END_ALLOW_THREADS
9613 Py_DECREF(opath);
9614 if (res < 0)
9615 return posix_error();
9616 Py_RETURN_NONE;
9617}
9618#endif /* HAVE_FCHMODAT */
9619
9620#ifdef HAVE_FCHOWNAT
9621PyDoc_STRVAR(posix_fchownat__doc__,
9622"fchownat(dirfd, path, uid, gid, flags=0)\n\n\
9623Like chown() but if path is relative, it is taken as relative to dirfd.\n\
9624flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9625If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9626is interpreted relative to the current working directory.");
9627
9628static PyObject *
9629posix_fchownat(PyObject *self, PyObject *args)
9630{
9631 PyObject *opath;
9632 int dirfd, res;
9633 long uid, gid;
9634 int flags = 0;
9635 char *path;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009636
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009637 if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat",
9638 &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags))
9639 return NULL;
9640
9641 path = PyBytes_AsString(opath);
9642
9643 Py_BEGIN_ALLOW_THREADS
9644 res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags);
9645 Py_END_ALLOW_THREADS
9646 Py_DECREF(opath);
9647 if (res < 0)
9648 return posix_error();
9649 Py_RETURN_NONE;
9650}
9651#endif /* HAVE_FCHOWNAT */
9652
9653#ifdef HAVE_FSTATAT
9654PyDoc_STRVAR(posix_fstatat__doc__,
9655"fstatat(dirfd, path, flags=0) -> stat result\n\n\
9656Like stat() but if path is relative, it is taken as relative to dirfd.\n\
9657flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9658If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9659is interpreted relative to the current working directory.");
9660
9661static PyObject *
9662posix_fstatat(PyObject *self, PyObject *args)
9663{
9664 PyObject *opath;
9665 char *path;
9666 STRUCT_STAT st;
9667 int dirfd, res, flags = 0;
9668
9669 if (!PyArg_ParseTuple(args, "iO&|i:fstatat",
9670 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9671 return NULL;
9672 path = PyBytes_AsString(opath);
9673
9674 Py_BEGIN_ALLOW_THREADS
9675 res = fstatat(dirfd, path, &st, flags);
9676 Py_END_ALLOW_THREADS
9677 Py_DECREF(opath);
9678 if (res != 0)
9679 return posix_error();
9680
9681 return _pystat_fromstructstat(&st);
9682}
9683#endif
9684
9685#ifdef HAVE_FUTIMESAT
9686PyDoc_STRVAR(posix_futimesat__doc__,
9687"futimesat(dirfd, path, (atime, mtime))\n\
9688futimesat(dirfd, path, None)\n\n\
9689Like utime() but if path is relative, it is taken as relative to dirfd.\n\
9690If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9691is interpreted relative to the current working directory.");
9692
9693static PyObject *
9694posix_futimesat(PyObject *self, PyObject *args)
9695{
9696 PyObject *opath;
9697 char *path;
9698 int res, dirfd;
9699 PyObject* arg;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009700 time_t atime, mtime;
9701 long ausec, musec;
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009702
9703 if (!PyArg_ParseTuple(args, "iO&O:futimesat",
9704 &dirfd, PyUnicode_FSConverter, &opath, &arg))
9705 return NULL;
9706 path = PyBytes_AsString(opath);
9707 if (arg == Py_None) {
9708 /* optional time values not given */
9709 Py_BEGIN_ALLOW_THREADS
9710 res = futimesat(dirfd, path, NULL);
9711 Py_END_ALLOW_THREADS
9712 }
9713 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
9714 PyErr_SetString(PyExc_TypeError,
9715 "futimesat() arg 3 must be a tuple (atime, mtime)");
9716 Py_DECREF(opath);
9717 return NULL;
9718 }
9719 else {
9720 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009721 &atime, &ausec) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009722 Py_DECREF(opath);
9723 return NULL;
9724 }
9725 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009726 &mtime, &musec) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009727 Py_DECREF(opath);
9728 return NULL;
9729 }
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009730
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009731 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009732 {
9733#ifdef HAVE_UTIMENSAT
9734 struct timespec buf[2];
9735 buf[0].tv_sec = atime;
9736 buf[0].tv_nsec = ausec;
9737 buf[1].tv_sec = mtime;
9738 buf[1].tv_nsec = musec;
9739 res = utimensat(dirfd, path, buf, 0);
9740#else
9741 struct timeval buf[2];
9742 buf[0].tv_sec = atime;
9743 buf[0].tv_usec = ausec;
9744 buf[1].tv_sec = mtime;
9745 buf[1].tv_usec = musec;
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009746 res = futimesat(dirfd, path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009747#endif
9748 }
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009749 Py_END_ALLOW_THREADS
9750 }
9751 Py_DECREF(opath);
9752 if (res < 0) {
9753 return posix_error();
9754 }
9755 Py_RETURN_NONE;
9756}
9757#endif
9758
9759#ifdef HAVE_LINKAT
9760PyDoc_STRVAR(posix_linkat__doc__,
9761"linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\
9762Like link() but if srcpath is relative, it is taken as relative to srcfd\n\
9763and if dstpath is relative, it is taken as relative to dstfd.\n\
9764flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\
9765If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\
9766srcpath is interpreted relative to the current working directory. This\n\
9767also applies for dstpath.");
9768
9769static PyObject *
9770posix_linkat(PyObject *self, PyObject *args)
9771{
9772 PyObject *osrc, *odst;
9773 char *src, *dst;
9774 int res, srcfd, dstfd;
9775 int flags = 0;
9776
9777 if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat",
9778 &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags))
9779 return NULL;
9780 src = PyBytes_AsString(osrc);
9781 dst = PyBytes_AsString(odst);
9782 Py_BEGIN_ALLOW_THREADS
9783 res = linkat(srcfd, src, dstfd, dst, flags);
9784 Py_END_ALLOW_THREADS
9785 Py_DECREF(osrc);
9786 Py_DECREF(odst);
9787 if (res < 0)
9788 return posix_error();
9789 Py_RETURN_NONE;
9790}
9791#endif /* HAVE_LINKAT */
9792
9793#ifdef HAVE_MKDIRAT
9794PyDoc_STRVAR(posix_mkdirat__doc__,
9795"mkdirat(dirfd, path, mode=0o777)\n\n\
9796Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\
9797If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9798is interpreted relative to the current working directory.");
9799
9800static PyObject *
9801posix_mkdirat(PyObject *self, PyObject *args)
9802{
9803 int res, dirfd;
9804 PyObject *opath;
9805 char *path;
9806 int mode = 0777;
9807
9808 if (!PyArg_ParseTuple(args, "iO&|i:mkdirat",
9809 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9810 return NULL;
9811 path = PyBytes_AsString(opath);
9812 Py_BEGIN_ALLOW_THREADS
9813 res = mkdirat(dirfd, path, mode);
9814 Py_END_ALLOW_THREADS
9815 Py_DECREF(opath);
9816 if (res < 0)
9817 return posix_error();
9818 Py_RETURN_NONE;
9819}
9820#endif
9821
9822#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
9823PyDoc_STRVAR(posix_mknodat__doc__,
9824"mknodat(dirfd, path, mode=0o600, device=0)\n\n\
9825Like mknod() but if path is relative, it is taken as relative to dirfd.\n\
9826If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9827is interpreted relative to the current working directory.");
9828
9829static PyObject *
9830posix_mknodat(PyObject *self, PyObject *args)
9831{
9832 PyObject *opath;
9833 char *filename;
9834 int mode = 0600;
9835 int device = 0;
9836 int res, dirfd;
9837 if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd,
9838 PyUnicode_FSConverter, &opath, &mode, &device))
9839 return NULL;
9840 filename = PyBytes_AS_STRING(opath);
9841 Py_BEGIN_ALLOW_THREADS
9842 res = mknodat(dirfd, filename, mode, device);
9843 Py_END_ALLOW_THREADS
9844 Py_DECREF(opath);
9845 if (res < 0)
9846 return posix_error();
9847 Py_RETURN_NONE;
9848}
9849#endif
9850
9851#ifdef HAVE_OPENAT
9852PyDoc_STRVAR(posix_openat__doc__,
9853"openat(dirfd, path, flag, mode=0o777) -> fd\n\n\
9854Like open() but if path is relative, it is taken as relative to dirfd.\n\
9855If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9856is interpreted relative to the current working directory.");
9857
9858static PyObject *
9859posix_openat(PyObject *self, PyObject *args)
9860{
9861 PyObject *ofile;
9862 char *file;
9863 int flag, dirfd, fd;
9864 int mode = 0777;
9865
9866 if (!PyArg_ParseTuple(args, "iO&i|i:openat",
9867 &dirfd, PyUnicode_FSConverter, &ofile,
9868 &flag, &mode))
9869 return NULL;
9870 file = PyBytes_AsString(ofile);
9871 Py_BEGIN_ALLOW_THREADS
9872 fd = openat(dirfd, file, flag, mode);
9873 Py_END_ALLOW_THREADS
9874 Py_DECREF(ofile);
9875 if (fd < 0)
9876 return posix_error();
9877 return PyLong_FromLong((long)fd);
9878}
9879#endif
9880
9881#ifdef HAVE_READLINKAT
9882PyDoc_STRVAR(posix_readlinkat__doc__,
9883"readlinkat(dirfd, path) -> path\n\n\
9884Like readlink() but if path is relative, it is taken as relative to dirfd.\n\
9885If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9886is interpreted relative to the current working directory.");
9887
9888static PyObject *
9889posix_readlinkat(PyObject *self, PyObject *args)
9890{
9891 PyObject *v, *opath;
9892 char buf[MAXPATHLEN];
9893 char *path;
9894 int n, dirfd;
9895 int arg_is_unicode = 0;
9896
9897 if (!PyArg_ParseTuple(args, "iO&:readlinkat",
9898 &dirfd, PyUnicode_FSConverter, &opath))
9899 return NULL;
9900 path = PyBytes_AsString(opath);
9901 v = PySequence_GetItem(args, 1);
9902 if (v == NULL) {
9903 Py_DECREF(opath);
9904 return NULL;
9905 }
9906
9907 if (PyUnicode_Check(v)) {
9908 arg_is_unicode = 1;
9909 }
9910 Py_DECREF(v);
9911
9912 Py_BEGIN_ALLOW_THREADS
9913 n = readlinkat(dirfd, path, buf, (int) sizeof buf);
9914 Py_END_ALLOW_THREADS
9915 Py_DECREF(opath);
9916 if (n < 0)
9917 return posix_error();
9918
9919 if (arg_is_unicode)
9920 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
9921 else
9922 return PyBytes_FromStringAndSize(buf, n);
9923}
9924#endif /* HAVE_READLINKAT */
9925
9926#ifdef HAVE_RENAMEAT
9927PyDoc_STRVAR(posix_renameat__doc__,
9928"renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\
9929Like rename() but if oldpath is relative, it is taken as relative to\n\
9930olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\
9931If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\
9932oldpath is interpreted relative to the current working directory. This\n\
9933also applies for newpath.");
9934
9935static PyObject *
9936posix_renameat(PyObject *self, PyObject *args)
9937{
9938 int res;
9939 PyObject *opathold, *opathnew;
9940 char *opath, *npath;
9941 int oldfd, newfd;
9942
9943 if (!PyArg_ParseTuple(args, "iO&iO&:renameat",
9944 &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew))
9945 return NULL;
9946 opath = PyBytes_AsString(opathold);
9947 npath = PyBytes_AsString(opathnew);
9948 Py_BEGIN_ALLOW_THREADS
9949 res = renameat(oldfd, opath, newfd, npath);
9950 Py_END_ALLOW_THREADS
9951 Py_DECREF(opathold);
9952 Py_DECREF(opathnew);
9953 if (res < 0)
9954 return posix_error();
9955 Py_RETURN_NONE;
9956}
9957#endif
9958
9959#if HAVE_SYMLINKAT
9960PyDoc_STRVAR(posix_symlinkat__doc__,
9961"symlinkat(src, dstfd, dst)\n\n\
9962Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\
9963If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\
9964is interpreted relative to the current working directory.");
9965
9966static PyObject *
9967posix_symlinkat(PyObject *self, PyObject *args)
9968{
9969 int res, dstfd;
9970 PyObject *osrc, *odst;
9971 char *src, *dst;
9972
9973 if (!PyArg_ParseTuple(args, "O&iO&:symlinkat",
9974 PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst))
9975 return NULL;
9976 src = PyBytes_AsString(osrc);
9977 dst = PyBytes_AsString(odst);
9978 Py_BEGIN_ALLOW_THREADS
9979 res = symlinkat(src, dstfd, dst);
9980 Py_END_ALLOW_THREADS
9981 Py_DECREF(osrc);
9982 Py_DECREF(odst);
9983 if (res < 0)
9984 return posix_error();
9985 Py_RETURN_NONE;
9986}
9987#endif /* HAVE_SYMLINKAT */
9988
9989#ifdef HAVE_UNLINKAT
9990PyDoc_STRVAR(posix_unlinkat__doc__,
9991"unlinkat(dirfd, path, flags=0)\n\n\
9992Like unlink() but if path is relative, it is taken as relative to dirfd.\n\
9993flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\
9994specified, unlinkat() behaves like rmdir().\n\
9995If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9996is interpreted relative to the current working directory.");
9997
9998static PyObject *
9999posix_unlinkat(PyObject *self, PyObject *args)
10000{
10001 int dirfd, res, flags = 0;
10002 PyObject *opath;
10003 char *path;
10004
10005 if (!PyArg_ParseTuple(args, "iO&|i:unlinkat",
10006 &dirfd, PyUnicode_FSConverter, &opath, &flags))
10007 return NULL;
10008 path = PyBytes_AsString(opath);
10009 Py_BEGIN_ALLOW_THREADS
10010 res = unlinkat(dirfd, path, flags);
10011 Py_END_ALLOW_THREADS
10012 Py_DECREF(opath);
10013 if (res < 0)
10014 return posix_error();
10015 Py_RETURN_NONE;
10016}
10017#endif
10018
10019#ifdef HAVE_UTIMENSAT
10020PyDoc_STRVAR(posix_utimensat__doc__,
10021"utimensat(dirfd, path, (atime_sec, atime_nsec),\n\
10022 (mtime_sec, mtime_nsec), flags)\n\
10023utimensat(dirfd, path, None, None, flags)\n\n\
10024Updates the timestamps of a file with nanosecond precision. If path is\n\
10025relative, it is taken as relative to dirfd.\n\
10026The second form sets atime and mtime to the current time.\n\
10027flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
10028If path is relative and dirfd is the special value AT_FDCWD, then path\n\
10029is interpreted relative to the current working directory.\n\
10030If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
10031current time.\n\
10032If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
10033
10034static PyObject *
10035posix_utimensat(PyObject *self, PyObject *args)
10036{
10037 PyObject *opath;
10038 char *path;
10039 int res, dirfd, flags = 0;
10040 PyObject *atime, *mtime;
10041
10042 struct timespec buf[2];
10043
10044 if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat",
10045 &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
10046 return NULL;
10047 path = PyBytes_AsString(opath);
10048 if (atime == Py_None && mtime == Py_None) {
10049 /* optional time values not given */
10050 Py_BEGIN_ALLOW_THREADS
10051 res = utimensat(dirfd, path, NULL, flags);
10052 Py_END_ALLOW_THREADS
10053 }
10054 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
10055 PyErr_SetString(PyExc_TypeError,
10056 "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)");
10057 Py_DECREF(opath);
10058 return NULL;
10059 }
10060 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
10061 PyErr_SetString(PyExc_TypeError,
10062 "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)");
10063 Py_DECREF(opath);
10064 return NULL;
10065 }
10066 else {
10067 if (!PyArg_ParseTuple(atime, "ll:utimensat",
10068 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
10069 Py_DECREF(opath);
10070 return NULL;
10071 }
10072 if (!PyArg_ParseTuple(mtime, "ll:utimensat",
10073 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
10074 Py_DECREF(opath);
10075 return NULL;
10076 }
10077 Py_BEGIN_ALLOW_THREADS
10078 res = utimensat(dirfd, path, buf, flags);
10079 Py_END_ALLOW_THREADS
10080 }
10081 Py_DECREF(opath);
10082 if (res < 0) {
10083 return posix_error();
10084 }
10085 Py_RETURN_NONE;
10086}
10087#endif
10088
10089#ifdef HAVE_MKFIFOAT
10090PyDoc_STRVAR(posix_mkfifoat__doc__,
10091"mkfifoat(dirfd, path, mode=0o666)\n\n\
10092Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\
10093If path is relative and dirfd is the special value AT_FDCWD, then path\n\
10094is interpreted relative to the current working directory.");
10095
10096static PyObject *
10097posix_mkfifoat(PyObject *self, PyObject *args)
10098{
10099 PyObject *opath;
10100 char *filename;
10101 int mode = 0666;
10102 int res, dirfd;
10103 if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat",
10104 &dirfd, PyUnicode_FSConverter, &opath, &mode))
10105 return NULL;
10106 filename = PyBytes_AS_STRING(opath);
10107 Py_BEGIN_ALLOW_THREADS
10108 res = mkfifoat(dirfd, filename, mode);
10109 Py_END_ALLOW_THREADS
10110 Py_DECREF(opath);
10111 if (res < 0)
10112 return posix_error();
10113 Py_RETURN_NONE;
10114}
10115#endif
10116
Benjamin Peterson9428d532011-09-14 11:45:52 -040010117#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040010118
10119static int
10120try_getxattr(const char *path, const char *name,
10121 ssize_t (*get)(const char *, const char *, void *, size_t),
10122 Py_ssize_t buf_size, PyObject **res)
10123{
10124 PyObject *value;
10125 Py_ssize_t len;
10126
10127 assert(buf_size <= XATTR_SIZE_MAX);
10128 value = PyBytes_FromStringAndSize(NULL, buf_size);
10129 if (!value)
10130 return 0;
10131 Py_BEGIN_ALLOW_THREADS;
10132 len = get(path, name, PyBytes_AS_STRING(value), buf_size);
10133 Py_END_ALLOW_THREADS;
10134 if (len < 0) {
10135 Py_DECREF(value);
10136 if (errno == ERANGE) {
10137 value = NULL;
10138 }
10139 else {
10140 posix_error();
10141 return 0;
10142 }
10143 }
10144 else if (len != buf_size) {
10145 /* Can only shrink. */
10146 _PyBytes_Resize(&value, len);
10147 }
10148 *res = value;
10149 return 1;
10150}
10151
10152static PyObject *
10153getxattr_common(const char *path, PyObject *name_obj,
10154 ssize_t (*get)(const char *, const char *, void *, size_t))
10155{
10156 PyObject *value;
10157 const char *name = PyBytes_AS_STRING(name_obj);
10158
10159 /* Try a small value first. */
10160 if (!try_getxattr(path, name, get, 128, &value))
10161 return NULL;
10162 if (value)
10163 return value;
10164 /* Now the maximum possible one. */
10165 if (!try_getxattr(path, name, get, XATTR_SIZE_MAX, &value))
10166 return NULL;
10167 assert(value);
10168 return value;
10169}
10170
10171PyDoc_STRVAR(posix_getxattr__doc__,
10172"getxattr(path, attr) -> value\n\n\
10173Return the value of extended attribute *name* on *path*.");
10174
10175static PyObject *
10176posix_getxattr(PyObject *self, PyObject *args)
10177{
10178 PyObject *path, *res, *name;
10179
10180 if (!PyArg_ParseTuple(args, "O&O&:getxattr", PyUnicode_FSConverter, &path,
10181 PyUnicode_FSConverter, &name))
10182 return NULL;
10183 res = getxattr_common(PyBytes_AS_STRING(path), name, getxattr);
10184 Py_DECREF(path);
10185 Py_DECREF(name);
10186 return res;
10187}
10188
10189PyDoc_STRVAR(posix_lgetxattr__doc__,
10190"lgetxattr(path, attr) -> value\n\n\
10191Like getxattr but don't follow symlinks.");
10192
10193static PyObject *
10194posix_lgetxattr(PyObject *self, PyObject *args)
10195{
10196 PyObject *path, *res, *name;
10197
10198 if (!PyArg_ParseTuple(args, "O&O&:lgetxattr", PyUnicode_FSConverter, &path,
10199 PyUnicode_FSConverter, &name))
10200 return NULL;
10201 res = getxattr_common(PyBytes_AS_STRING(path), name, lgetxattr);
10202 Py_DECREF(path);
10203 Py_DECREF(name);
10204 return res;
10205}
10206
10207static ssize_t
10208wrap_fgetxattr(const char *path, const char *name, void *value, size_t size)
10209{
10210 /* Hack to share code. */
10211 return fgetxattr((int)(Py_uintptr_t)path, name, value, size);
10212}
10213
10214PyDoc_STRVAR(posix_fgetxattr__doc__,
10215"fgetxattr(fd, attr) -> value\n\n\
10216Like getxattr but operate on a fd instead of a path.");
10217
10218static PyObject *
10219posix_fgetxattr(PyObject *self, PyObject *args)
10220{
10221 PyObject *res, *name;
10222 int fd;
10223
10224 if (!PyArg_ParseTuple(args, "iO&:fgetxattr", &fd, PyUnicode_FSConverter, &name))
10225 return NULL;
10226 res = getxattr_common((const char *)(Py_uintptr_t)fd, name, wrap_fgetxattr);
10227 Py_DECREF(name);
10228 return res;
10229}
10230
10231PyDoc_STRVAR(posix_setxattr__doc__,
10232"setxattr(path, attr, value, flags=0)\n\n\
10233Set extended attribute *attr* on *path* to *value*.");
10234
10235static PyObject *
10236posix_setxattr(PyObject *self, PyObject *args)
10237{
10238 PyObject *path, *name;
10239 Py_buffer data;
10240 int flags = 0, err;
10241
10242 if (!PyArg_ParseTuple(args, "O&O&y*|i:setxattr", PyUnicode_FSConverter,
10243 &path, PyUnicode_FSConverter, &name, &data, &flags))
10244 return NULL;
10245 Py_BEGIN_ALLOW_THREADS;
10246 err = setxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name),
10247 data.buf, data.len, flags);
10248 Py_END_ALLOW_THREADS;
10249 Py_DECREF(path);
10250 Py_DECREF(name);
10251 PyBuffer_Release(&data);
10252 if (err)
10253 return posix_error();
10254 Py_RETURN_NONE;
10255}
10256
10257PyDoc_STRVAR(posix_lsetxattr__doc__,
10258"lsetxattr(path, attr, value, flags=0)\n\n\
10259Like setxattr but don't follow symlinks.");
10260
10261static PyObject *
10262posix_lsetxattr(PyObject *self, PyObject *args)
10263{
10264 PyObject *path, *name;
10265 Py_buffer data;
10266 int flags = 0, err;
10267
10268 if (!PyArg_ParseTuple(args, "O&O&y*|i:lsetxattr", PyUnicode_FSConverter,
10269 &path, PyUnicode_FSConverter, &name, &data, &flags))
10270 return NULL;
10271 Py_BEGIN_ALLOW_THREADS;
10272 err = lsetxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name),
10273 data.buf, data.len, flags);
10274 Py_END_ALLOW_THREADS;
10275 Py_DECREF(path);
10276 Py_DECREF(name);
10277 PyBuffer_Release(&data);
10278 if (err)
10279 return posix_error();
10280 Py_RETURN_NONE;
10281}
10282
10283PyDoc_STRVAR(posix_fsetxattr__doc__,
10284"fsetxattr(fd, attr, value, flags=0)\n\n\
10285Like setxattr but operates on *fd* instead of a path.");
10286
10287static PyObject *
10288posix_fsetxattr(PyObject *self, PyObject *args)
10289{
10290 Py_buffer data;
10291 const char *name;
10292 int fd, flags = 0, err;
10293
10294 if (!PyArg_ParseTuple(args, "iO&y*|i:fsetxattr", &fd, PyUnicode_FSConverter,
10295 &name, &data, &flags))
10296 return NULL;
10297 Py_BEGIN_ALLOW_THREADS;
10298 err = fsetxattr(fd, PyBytes_AS_STRING(name), data.buf, data.len, flags);
10299 Py_END_ALLOW_THREADS;
10300 Py_DECREF(name);
10301 PyBuffer_Release(&data);
10302 if (err)
10303 return posix_error();
10304 Py_RETURN_NONE;
10305}
10306
10307PyDoc_STRVAR(posix_removexattr__doc__,
10308"removexattr(path, attr)\n\n\
10309Remove extended attribute *attr* on *path*.");
10310
10311static PyObject *
10312posix_removexattr(PyObject *self, PyObject *args)
10313{
10314 PyObject *path, *name;
10315 int err;
10316
10317 if (!PyArg_ParseTuple(args, "O&O&:removexattr", PyUnicode_FSConverter, &path,
10318 PyUnicode_FSConverter, &name))
10319 return NULL;
10320 Py_BEGIN_ALLOW_THREADS;
10321 err = removexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name));
10322 Py_END_ALLOW_THREADS;
10323 Py_DECREF(path);
10324 Py_DECREF(name);
10325 if (err)
10326 return posix_error();
10327 Py_RETURN_NONE;
10328}
10329
10330PyDoc_STRVAR(posix_lremovexattr__doc__,
10331"lremovexattr(path, attr)\n\n\
10332Like removexattr but don't follow symlinks.");
10333
10334static PyObject *
10335posix_lremovexattr(PyObject *self, PyObject *args)
10336{
10337 PyObject *path, *name;
10338 int err;
10339
10340 if (!PyArg_ParseTuple(args, "O&O&:lremovexattr", PyUnicode_FSConverter, &path,
10341 PyUnicode_FSConverter, &name))
10342 return NULL;
10343 Py_BEGIN_ALLOW_THREADS;
10344 err = lremovexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name));
10345 Py_END_ALLOW_THREADS;
10346 Py_DECREF(path);
10347 Py_DECREF(name);
10348 if (err)
10349 return posix_error();
10350 Py_RETURN_NONE;
10351}
10352
10353PyDoc_STRVAR(posix_fremovexattr__doc__,
10354"fremovexattr(fd, attr)\n\n\
10355Like removexattr but operates on a file descriptor.");
10356
10357static PyObject *
10358posix_fremovexattr(PyObject *self, PyObject *args)
10359{
10360 PyObject *name;
10361 int fd, err;
10362
10363 if (!PyArg_ParseTuple(args, "iO&:fremovexattr", &fd,
10364 PyUnicode_FSConverter, &name))
10365 return NULL;
10366 Py_BEGIN_ALLOW_THREADS;
10367 err = fremovexattr(fd, PyBytes_AS_STRING(name));
10368 Py_END_ALLOW_THREADS;
10369 Py_DECREF(name);
10370 if (err)
10371 return posix_error();
10372 Py_RETURN_NONE;
10373}
10374
10375static Py_ssize_t
10376try_listxattr(const char *path, ssize_t (*list)(const char *, char *, size_t),
10377 Py_ssize_t buf_size, char **buf)
10378{
10379 Py_ssize_t len;
10380
10381 *buf = PyMem_MALLOC(buf_size);
10382 if (!*buf) {
10383 PyErr_NoMemory();
10384 return -1;
10385 }
10386 Py_BEGIN_ALLOW_THREADS;
10387 len = list(path, *buf, buf_size);
10388 Py_END_ALLOW_THREADS;
10389 if (len < 0) {
10390 PyMem_FREE(*buf);
10391 if (errno != ERANGE)
10392 posix_error();
10393 return -1;
10394 }
10395 return len;
10396}
10397
10398static PyObject *
10399listxattr_common(const char *path, ssize_t (*list)(const char *, char *, size_t))
10400{
10401 PyObject *res, *attr;
10402 Py_ssize_t len, err, start, i;
10403 char *buf;
10404
10405 len = try_listxattr(path, list, 256, &buf);
10406 if (len < 0) {
10407 if (PyErr_Occurred())
10408 return NULL;
10409 len = try_listxattr(path, list, XATTR_LIST_MAX, &buf);
10410 if (len < 0)
10411 return NULL;
10412 }
10413 res = PyList_New(0);
10414 if (!res) {
10415 PyMem_FREE(buf);
10416 return NULL;
10417 }
10418 for (start = i = 0; i < len; i++) {
10419 if (!buf[i]) {
10420 attr = PyUnicode_DecodeFSDefaultAndSize(&buf[start], i - start);
10421 if (!attr) {
10422 Py_DECREF(res);
10423 PyMem_FREE(buf);
10424 return NULL;
10425 }
10426 err = PyList_Append(res, attr);
10427 Py_DECREF(attr);
10428 if (err) {
10429 Py_DECREF(res);
10430 PyMem_FREE(buf);
10431 return NULL;
10432 }
10433 start = i + 1;
10434 }
10435 }
10436 PyMem_FREE(buf);
10437 return res;
10438}
10439
10440PyDoc_STRVAR(posix_listxattr__doc__,
10441"listxattr(path)\n\n\
10442Return a list of extended attributes on *path*.");
10443
10444static PyObject *
10445posix_listxattr(PyObject *self, PyObject *args)
10446{
10447 PyObject *path, *res;
10448
10449 if (!PyArg_ParseTuple(args, "O&:listxattr", PyUnicode_FSConverter, &path))
10450 return NULL;
10451 res = listxattr_common(PyBytes_AS_STRING(path), listxattr);
10452 Py_DECREF(path);
10453 return res;
10454}
10455
10456PyDoc_STRVAR(posix_llistxattr__doc__,
10457"llistxattr(path)\n\n\
10458Like listxattr but don't follow symlinks..");
10459
10460static PyObject *
10461posix_llistxattr(PyObject *self, PyObject *args)
10462{
10463 PyObject *path, *res;
10464
10465 if (!PyArg_ParseTuple(args, "O&:llistxattr", PyUnicode_FSConverter, &path))
10466 return NULL;
10467 res = listxattr_common(PyBytes_AS_STRING(path), llistxattr);
10468 Py_DECREF(path);
10469 return res;
10470}
10471
10472static ssize_t
10473wrap_flistxattr(const char *path, char *buf, size_t len)
10474{
10475 /* Hack to share code. */
10476 return flistxattr((int)(Py_uintptr_t)path, buf, len);
10477}
10478
10479PyDoc_STRVAR(posix_flistxattr__doc__,
10480"flistxattr(path)\n\n\
10481Like flistxattr but operates on a file descriptor.");
10482
10483static PyObject *
10484posix_flistxattr(PyObject *self, PyObject *args)
10485{
10486 long fd;
10487
10488 if (!PyArg_ParseTuple(args, "i:flistxattr", &fd))
10489 return NULL;
10490 return listxattr_common((const char *)(Py_uintptr_t)fd, wrap_flistxattr);
10491}
10492
Benjamin Peterson9428d532011-09-14 11:45:52 -040010493#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040010494
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010495static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +000010496 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010497#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010498 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010499#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010500 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010501#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010502 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010503#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +000010504 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010505#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010506 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010507#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010508#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010509 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010510#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +000010511#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010512 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010513#endif /* HAVE_LCHMOD */
10514#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010515 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010516#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +000010517#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010518 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010519#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010520#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010521 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010522#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +000010523#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +000010524 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +000010525#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010526#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +000010527 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010528#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +000010529#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +000010530 {"getcwd", (PyCFunction)posix_getcwd_unicode,
10531 METH_NOARGS, posix_getcwd__doc__},
10532 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
10533 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +000010534#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010535#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +000010536 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010537#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +000010538 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +000010539#ifdef HAVE_FDOPENDIR
10540 {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__},
10541#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010542 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
10543 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010544#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +000010545 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010546#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010547#ifdef HAVE_GETPRIORITY
10548 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
10549#endif /* HAVE_GETPRIORITY */
10550#ifdef HAVE_SETPRIORITY
10551 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
10552#endif /* HAVE_SETPRIORITY */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010553#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +000010554 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010555#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000010556#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010557 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000010558#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010559 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
10560 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
10561 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010562 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +000010563#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +000010564 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010565#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +000010566#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010567 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +000010568 win_symlink__doc__},
10569#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010570#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +000010571 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010572#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010573 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010574#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010575 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010576#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +000010577 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
10578 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
10579 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010580#ifdef HAVE_FUTIMES
10581 {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__},
10582#endif
10583#ifdef HAVE_LUTIMES
10584 {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__},
10585#endif
10586#ifdef HAVE_FUTIMENS
10587 {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__},
10588#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010589#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +000010590 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010591#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +000010592 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010593#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +000010594 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
10595 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010596#endif /* HAVE_EXECV */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010597#ifdef HAVE_FEXECVE
10598 {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__},
10599#endif
Guido van Rossuma1065681999-01-25 23:20:23 +000010600#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +000010601 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
10602 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010603#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010604 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
10605 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010606#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +000010607#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010608#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +000010609 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010610#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010611#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +000010612 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010613#endif /* HAVE_FORK */
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010614#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010615#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010616 {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__},
10617 {"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 +020010618#endif
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010619#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010620 {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010621#endif
10622#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010623 {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010624#endif
10625#ifdef HAVE_SCHED_RR_GET_INTERVAL
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010626 {"sched_rr_get_interval", posix_sched_rr_get_interval, METH_VARARGS, posix_sched_rr_get_interval__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010627#endif
10628#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010629 {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010630#endif
10631#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010632 {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010633#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010634 {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__},
Benjamin Peterson2740af82011-08-02 17:41:34 -050010635#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010636 {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__},
10637 {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__},
10638#endif
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010639#endif /* HAVE_SCHED_H */
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010640#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +000010641 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010642#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +000010643#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010644 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +000010645#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010646#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010647 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010648#endif /* HAVE_GETEGID */
10649#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010650 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010651#endif /* HAVE_GETEUID */
10652#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010653 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010654#endif /* HAVE_GETGID */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020010655#ifdef HAVE_GETGROUPLIST
10656 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
10657#endif
Fred Drakec9680921999-12-13 16:37:25 +000010658#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010659 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010660#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010661 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010662#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010663 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010664#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010665#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010666 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010667#endif /* HAVE_GETPPID */
10668#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010669 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010670#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +000010671#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010672 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +000010673#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +000010674#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +000010675 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010676#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010677#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +000010678 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010679#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +000010680#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010681 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +000010682#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +000010683#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010684 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
10685 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +000010686 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +000010687#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010688#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010689 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010690#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010691#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010692 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010693#endif /* HAVE_SETEUID */
10694#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010695 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010696#endif /* HAVE_SETEGID */
10697#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010698 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010699#endif /* HAVE_SETREUID */
10700#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010701 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010702#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010703#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010704 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010705#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010706#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010707 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010708#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +000010709#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010710 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000010711#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +000010712#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010713 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +000010714#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010715#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010716 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010717#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010718#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010719 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010720#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010721#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +000010722 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010723#endif /* HAVE_WAIT3 */
10724#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +000010725 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010726#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010727#if defined(HAVE_WAITID) && !defined(__APPLE__)
10728 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
10729#endif
Tim Petersab034fa2002-02-01 11:27:43 +000010730#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010731 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010732#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010733#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010734 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010735#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010736#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010737 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010738#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010739#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010740 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010741#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010742#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010743 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010744#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010745#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010746 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010747#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +000010748 {"open", posix_open, METH_VARARGS, posix_open__doc__},
10749 {"close", posix_close, METH_VARARGS, posix_close__doc__},
10750 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
10751 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
10752 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
10753 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010754#ifdef HAVE_LOCKF
10755 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
10756#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010757 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
10758 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010759#ifdef HAVE_READV
10760 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
10761#endif
10762#ifdef HAVE_PREAD
10763 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
10764#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010765 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010766#ifdef HAVE_WRITEV
10767 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
10768#endif
10769#ifdef HAVE_PWRITE
10770 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
10771#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010772#ifdef HAVE_SENDFILE
10773 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
10774 posix_sendfile__doc__},
10775#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010776 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
10777 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010778#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010779 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010780#endif
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010781#ifdef HAVE_PIPE2
Charles-François Natali368f34b2011-06-06 19:49:47 +020010782 {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__},
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010783#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010784#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +000010785 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010786#endif
Neal Norwitz11690112002-07-30 01:08:28 +000010787#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +000010788 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +000010789#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010790#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +000010791 {"major", posix_major, METH_VARARGS, posix_major__doc__},
10792 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
10793 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010794#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010795#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +000010796 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010797#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010798#ifdef HAVE_TRUNCATE
10799 {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__},
10800#endif
10801#ifdef HAVE_POSIX_FALLOCATE
10802 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
10803#endif
10804#ifdef HAVE_POSIX_FADVISE
10805 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
10806#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010807#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010808 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010809#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010810#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010811 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +000010812#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010813 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010814#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +000010815 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010816#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010817#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010818 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010819#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010820#ifdef HAVE_SYNC
10821 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
10822#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010823#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010824 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010825#endif
Guido van Rossumc9641791998-08-04 15:26:23 +000010826#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010827#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +000010828 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +000010829#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010830#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010831 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010832#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +000010833#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +000010834 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010835#endif /* WIFSTOPPED */
10836#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +000010837 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010838#endif /* WIFSIGNALED */
10839#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +000010840 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010841#endif /* WIFEXITED */
10842#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +000010843 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010844#endif /* WEXITSTATUS */
10845#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010846 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010847#endif /* WTERMSIG */
10848#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010849 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010850#endif /* WSTOPSIG */
10851#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010852#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010853 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010854#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000010855#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010856 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010857#endif
Fred Drakec9680921999-12-13 16:37:25 +000010858#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +000010859 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010860#endif
10861#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010862 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010863#endif
10864#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010865 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010866#endif
10867#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010868 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010869#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010870 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010871#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010872 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +000010873 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +000010874 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -050010875 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010876 {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +000010877#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +000010878#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +000010879 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +000010880#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010881 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010882 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010883 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010884 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +000010885 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010886 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010887#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010888 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010889#endif
10890#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010891 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010892#endif
10893#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010894 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010895#endif
10896#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010897 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010898#endif
10899
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010900/* posix *at family of functions */
10901#ifdef HAVE_FACCESSAT
10902 {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__},
10903#endif
10904#ifdef HAVE_FCHMODAT
10905 {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__},
10906#endif /* HAVE_FCHMODAT */
10907#ifdef HAVE_FCHOWNAT
10908 {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__},
10909#endif /* HAVE_FCHOWNAT */
10910#ifdef HAVE_FSTATAT
10911 {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__},
10912#endif
10913#ifdef HAVE_FUTIMESAT
10914 {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__},
10915#endif
10916#ifdef HAVE_LINKAT
10917 {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__},
10918#endif /* HAVE_LINKAT */
10919#ifdef HAVE_MKDIRAT
10920 {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__},
10921#endif
10922#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
10923 {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__},
10924#endif
10925#ifdef HAVE_OPENAT
10926 {"openat", posix_openat, METH_VARARGS, posix_openat__doc__},
10927#endif
10928#ifdef HAVE_READLINKAT
10929 {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__},
10930#endif /* HAVE_READLINKAT */
10931#ifdef HAVE_RENAMEAT
10932 {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__},
10933#endif
10934#if HAVE_SYMLINKAT
10935 {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__},
10936#endif /* HAVE_SYMLINKAT */
10937#ifdef HAVE_UNLINKAT
10938 {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
10939#endif
10940#ifdef HAVE_UTIMENSAT
10941 {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__},
10942#endif
10943#ifdef HAVE_MKFIFOAT
10944 {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},
10945#endif
Benjamin Peterson9428d532011-09-14 11:45:52 -040010946#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040010947 {"setxattr", posix_setxattr, METH_VARARGS, posix_setxattr__doc__},
10948 {"lsetxattr", posix_lsetxattr, METH_VARARGS, posix_lsetxattr__doc__},
10949 {"fsetxattr", posix_fsetxattr, METH_VARARGS, posix_fsetxattr__doc__},
10950 {"getxattr", posix_getxattr, METH_VARARGS, posix_getxattr__doc__},
10951 {"lgetxattr", posix_lgetxattr, METH_VARARGS, posix_lgetxattr__doc__},
10952 {"fgetxattr", posix_fgetxattr, METH_VARARGS, posix_fgetxattr__doc__},
10953 {"removexattr", posix_removexattr, METH_VARARGS, posix_removexattr__doc__},
10954 {"lremovexattr", posix_lremovexattr, METH_VARARGS, posix_lremovexattr__doc__},
10955 {"fremovexattr", posix_fremovexattr, METH_VARARGS, posix_fremovexattr__doc__},
10956 {"listxattr", posix_listxattr, METH_VARARGS, posix_listxattr__doc__},
10957 {"llistxattr", posix_llistxattr, METH_VARARGS, posix_llistxattr__doc__},
10958 {"flistxattr", posix_flistxattr, METH_VARARGS, posix_flistxattr__doc__},
10959#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010960 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010961};
10962
10963
Barry Warsaw4a342091996-12-19 23:50:02 +000010964static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000010965ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +000010966{
Victor Stinner8c62be82010-05-06 00:08:46 +000010967 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +000010968}
10969
Guido van Rossumd48f2521997-12-05 22:19:34 +000010970#if defined(PYOS_OS2)
10971/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +000010972static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +000010973{
10974 APIRET rc;
10975 ULONG values[QSV_MAX+1];
10976 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +000010977 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +000010978
10979 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +000010980 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010981 Py_END_ALLOW_THREADS
10982
10983 if (rc != NO_ERROR) {
10984 os2_error(rc);
10985 return -1;
10986 }
10987
Fred Drake4d1e64b2002-04-15 19:40:07 +000010988 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
10989 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
10990 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
10991 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
10992 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
10993 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
10994 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010995
10996 switch (values[QSV_VERSION_MINOR]) {
10997 case 0: ver = "2.00"; break;
10998 case 10: ver = "2.10"; break;
10999 case 11: ver = "2.11"; break;
11000 case 30: ver = "3.00"; break;
11001 case 40: ver = "4.00"; break;
11002 case 50: ver = "5.00"; break;
11003 default:
Tim Peters885d4572001-11-28 20:27:42 +000011004 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +000011005 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +000011006 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +000011007 ver = &tmp[0];
11008 }
11009
11010 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +000011011 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +000011012 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011013
11014 /* Add Indicator of Which Drive was Used to Boot the System */
11015 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
11016 tmp[1] = ':';
11017 tmp[2] = '\0';
11018
Fred Drake4d1e64b2002-04-15 19:40:07 +000011019 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +000011020}
11021#endif
11022
Brian Curtin52173d42010-12-02 18:29:18 +000011023#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000011024static int
Brian Curtin52173d42010-12-02 18:29:18 +000011025enable_symlink()
11026{
11027 HANDLE tok;
11028 TOKEN_PRIVILEGES tok_priv;
11029 LUID luid;
11030 int meth_idx = 0;
11031
11032 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000011033 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000011034
11035 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000011036 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000011037
11038 tok_priv.PrivilegeCount = 1;
11039 tok_priv.Privileges[0].Luid = luid;
11040 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
11041
11042 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
11043 sizeof(TOKEN_PRIVILEGES),
11044 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000011045 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000011046
Brian Curtin3b4499c2010-12-28 14:31:47 +000011047 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
11048 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000011049}
11050#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
11051
Barry Warsaw4a342091996-12-19 23:50:02 +000011052static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000011053all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +000011054{
Guido van Rossum94f6f721999-01-06 18:42:14 +000011055#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011056 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011057#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011058#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011059 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011060#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011061#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011062 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011063#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011064#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011065 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011066#endif
Fred Drakec9680921999-12-13 16:37:25 +000011067#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011068 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000011069#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011070#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011071 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011072#endif
Fred Drake106c1a02002-04-23 15:58:02 +000011073#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000011074 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000011075#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000011076#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +000011077 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011078#endif
Fred Drake106c1a02002-04-23 15:58:02 +000011079#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +000011080 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000011081#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000011082#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000011083 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011084#endif
11085#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000011086 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011087#endif
11088#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +000011089 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011090#endif
11091#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +000011092 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011093#endif
11094#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011095 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011096#endif
11097#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +000011098 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011099#endif
11100#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011101 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011102#endif
11103#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011104 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011105#endif
11106#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011107 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011108#endif
11109#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +000011110 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011111#endif
11112#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000011113 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011114#endif
11115#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +000011116 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011117#endif
11118#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011119 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011120#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000011121#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +000011122 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011123#endif
11124#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +000011125 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011126#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011127#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000011128 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011129#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +000011130#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011131 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011132#endif
11133#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011134 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011135#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011136#ifdef PRIO_PROCESS
11137 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
11138#endif
11139#ifdef PRIO_PGRP
11140 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
11141#endif
11142#ifdef PRIO_USER
11143 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
11144#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020011145#ifdef O_CLOEXEC
11146 if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1;
11147#endif
Antoine Pitrouf65132d2011-02-25 23:25:17 +000011148/* posix - constants for *at functions */
11149#ifdef AT_SYMLINK_NOFOLLOW
11150 if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;
11151#endif
11152#ifdef AT_EACCESS
11153 if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1;
11154#endif
11155#ifdef AT_FDCWD
11156 if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1;
11157#endif
11158#ifdef AT_REMOVEDIR
11159 if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1;
11160#endif
11161#ifdef AT_SYMLINK_FOLLOW
11162 if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1;
11163#endif
11164#ifdef UTIME_NOW
11165 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
11166#endif
11167#ifdef UTIME_OMIT
11168 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
11169#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011170
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011171
Tim Peters5aa91602002-01-30 05:46:57 +000011172/* MS Windows */
11173#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011174 /* Don't inherit in child processes. */
11175 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011176#endif
11177#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000011178 /* Optimize for short life (keep in memory). */
11179 /* MS forgot to define this one with a non-underscore form too. */
11180 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011181#endif
11182#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000011183 /* Automatically delete when last handle is closed. */
11184 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011185#endif
11186#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000011187 /* Optimize for random access. */
11188 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011189#endif
11190#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011191 /* Optimize for sequential access. */
11192 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011193#endif
11194
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011195/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011196#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011197 /* Send a SIGIO signal whenever input or output
11198 becomes available on file descriptor */
11199 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011200#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011201#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011202 /* Direct disk access. */
11203 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011204#endif
11205#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000011206 /* Must be a directory. */
11207 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011208#endif
11209#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000011210 /* Do not follow links. */
11211 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011212#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011213#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011214 /* Do not update the access time. */
11215 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011216#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000011217
Victor Stinner8c62be82010-05-06 00:08:46 +000011218 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011219#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011220 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011221#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011222#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011223 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011224#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011225#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011226 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011227#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011228#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000011229 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011230#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011231#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +000011232 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011233#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011234#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +000011235 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011236#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011237#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000011238 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011239#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011240#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +000011241 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011242#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011243#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011244 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011245#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011246#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000011247 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011248#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011249#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000011250 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011251#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011252#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011253 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011254#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011255#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +000011256 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011257#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011258#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +000011259 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011260#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011261#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +000011262 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011263#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011264#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011265 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011266#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011267#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +000011268 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011269#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011270
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011271 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011272#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011273 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011274#endif /* ST_RDONLY */
11275#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011276 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011277#endif /* ST_NOSUID */
11278
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011279 /* FreeBSD sendfile() constants */
11280#ifdef SF_NODISKIO
11281 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
11282#endif
11283#ifdef SF_MNOWAIT
11284 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
11285#endif
11286#ifdef SF_SYNC
11287 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
11288#endif
11289
Ross Lagerwall7807c352011-03-17 20:20:30 +020011290 /* constants for posix_fadvise */
11291#ifdef POSIX_FADV_NORMAL
11292 if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1;
11293#endif
11294#ifdef POSIX_FADV_SEQUENTIAL
11295 if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1;
11296#endif
11297#ifdef POSIX_FADV_RANDOM
11298 if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1;
11299#endif
11300#ifdef POSIX_FADV_NOREUSE
11301 if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1;
11302#endif
11303#ifdef POSIX_FADV_WILLNEED
11304 if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1;
11305#endif
11306#ifdef POSIX_FADV_DONTNEED
11307 if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1;
11308#endif
11309
11310 /* constants for waitid */
11311#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
11312 if (ins(d, "P_PID", (long)P_PID)) return -1;
11313 if (ins(d, "P_PGID", (long)P_PGID)) return -1;
11314 if (ins(d, "P_ALL", (long)P_ALL)) return -1;
11315#endif
11316#ifdef WEXITED
11317 if (ins(d, "WEXITED", (long)WEXITED)) return -1;
11318#endif
11319#ifdef WNOWAIT
11320 if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1;
11321#endif
11322#ifdef WSTOPPED
11323 if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1;
11324#endif
11325#ifdef CLD_EXITED
11326 if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1;
11327#endif
11328#ifdef CLD_DUMPED
11329 if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1;
11330#endif
11331#ifdef CLD_TRAPPED
11332 if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1;
11333#endif
11334#ifdef CLD_CONTINUED
11335 if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1;
11336#endif
11337
11338 /* constants for lockf */
11339#ifdef F_LOCK
11340 if (ins(d, "F_LOCK", (long)F_LOCK)) return -1;
11341#endif
11342#ifdef F_TLOCK
11343 if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1;
11344#endif
11345#ifdef F_ULOCK
11346 if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1;
11347#endif
11348#ifdef F_TEST
11349 if (ins(d, "F_TEST", (long)F_TEST)) return -1;
11350#endif
11351
11352 /* constants for futimens */
11353#ifdef UTIME_NOW
11354 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
11355#endif
11356#ifdef UTIME_OMIT
11357 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
11358#endif
11359
Guido van Rossum246bc171999-02-01 23:54:31 +000011360#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011361#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +000011362 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
11363 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
11364 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
11365 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
11366 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
11367 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
11368 if (ins(d, "P_PM", (long)P_PM)) return -1;
11369 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
11370 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
11371 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
11372 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
11373 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
11374 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
11375 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
11376 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
11377 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
11378 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
11379 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
11380 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
11381 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011382#else
Victor Stinner8c62be82010-05-06 00:08:46 +000011383 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
11384 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
11385 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
11386 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
11387 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000011388#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011389#endif
Guido van Rossum246bc171999-02-01 23:54:31 +000011390
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011391#ifdef HAVE_SCHED_H
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011392 if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011393 if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1;
11394 if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1;
11395#ifdef SCHED_SPORADIC
11396 if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1;
11397#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011398#ifdef SCHED_BATCH
11399 if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1;
11400#endif
11401#ifdef SCHED_IDLE
11402 if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1;
11403#endif
11404#ifdef SCHED_RESET_ON_FORK
11405 if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1;
11406#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011407#ifdef SCHED_SYS
11408 if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1;
11409#endif
11410#ifdef SCHED_IA
11411 if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1;
11412#endif
11413#ifdef SCHED_FSS
11414 if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1;
11415#endif
11416#ifdef SCHED_FX
11417 if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1;
11418#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011419#endif
11420
Benjamin Peterson9428d532011-09-14 11:45:52 -040011421#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040011422 if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1;
11423 if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1;
11424 if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1;
11425#endif
11426
Guido van Rossumd48f2521997-12-05 22:19:34 +000011427#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000011428 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011429#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011430 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000011431}
11432
11433
Tim Peters5aa91602002-01-30 05:46:57 +000011434#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011435#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011436#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011437
11438#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011439#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011440#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011441
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011442#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000011443#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011444#define MODNAME "posix"
11445#endif
11446
Martin v. Löwis1a214512008-06-11 05:26:20 +000011447static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000011448 PyModuleDef_HEAD_INIT,
11449 MODNAME,
11450 posix__doc__,
11451 -1,
11452 posix_methods,
11453 NULL,
11454 NULL,
11455 NULL,
11456 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000011457};
11458
11459
Mark Hammondfe51c6d2002-08-02 02:27:13 +000011460PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000011461INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000011462{
Victor Stinner8c62be82010-05-06 00:08:46 +000011463 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +000011464
Brian Curtin52173d42010-12-02 18:29:18 +000011465#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000011466 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000011467#endif
11468
Victor Stinner8c62be82010-05-06 00:08:46 +000011469 m = PyModule_Create(&posixmodule);
11470 if (m == NULL)
11471 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000011472
Victor Stinner8c62be82010-05-06 00:08:46 +000011473 /* Initialize environ dictionary */
11474 v = convertenviron();
11475 Py_XINCREF(v);
11476 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
11477 return NULL;
11478 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000011479
Victor Stinner8c62be82010-05-06 00:08:46 +000011480 if (all_ins(m))
11481 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000011482
Victor Stinner8c62be82010-05-06 00:08:46 +000011483 if (setup_confname_tables(m))
11484 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000011485
Victor Stinner8c62be82010-05-06 00:08:46 +000011486 Py_INCREF(PyExc_OSError);
11487 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000011488
Benjamin Peterson2740af82011-08-02 17:41:34 -050011489#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011490 if (PyType_Ready(&cpu_set_type) < 0)
11491 return NULL;
11492 Py_INCREF(&cpu_set_type);
11493 PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type);
Benjamin Peterson2740af82011-08-02 17:41:34 -050011494#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011495
Guido van Rossumb3d39562000-01-31 18:41:26 +000011496#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000011497 if (posix_putenv_garbage == NULL)
11498 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000011499#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011500
Victor Stinner8c62be82010-05-06 00:08:46 +000011501 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020011502#if defined(HAVE_WAITID) && !defined(__APPLE__)
11503 waitid_result_desc.name = MODNAME ".waitid_result";
11504 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
11505#endif
11506
Victor Stinner8c62be82010-05-06 00:08:46 +000011507 stat_result_desc.name = MODNAME ".stat_result";
11508 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
11509 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
11510 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
11511 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
11512 structseq_new = StatResultType.tp_new;
11513 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011514
Victor Stinner8c62be82010-05-06 00:08:46 +000011515 statvfs_result_desc.name = MODNAME ".statvfs_result";
11516 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011517#ifdef NEED_TICKS_PER_SECOND
11518# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000011519 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011520# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000011521 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011522# else
Victor Stinner8c62be82010-05-06 00:08:46 +000011523 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011524# endif
11525#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011526
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050011527#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011528 sched_param_desc.name = MODNAME ".sched_param";
11529 PyStructSequence_InitType(&SchedParamType, &sched_param_desc);
11530 SchedParamType.tp_new = sched_param_new;
11531#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011532 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020011533#if defined(HAVE_WAITID) && !defined(__APPLE__)
11534 Py_INCREF((PyObject*) &WaitidResultType);
11535 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
11536#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011537 Py_INCREF((PyObject*) &StatResultType);
11538 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
11539 Py_INCREF((PyObject*) &StatVFSResultType);
11540 PyModule_AddObject(m, "statvfs_result",
11541 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011542
11543#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011544 Py_INCREF(&SchedParamType);
11545 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011546#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011547 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011548
11549#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000011550 /*
11551 * Step 2 of weak-linking support on Mac OS X.
11552 *
11553 * The code below removes functions that are not available on the
11554 * currently active platform.
11555 *
11556 * This block allow one to use a python binary that was build on
11557 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
11558 * OSX 10.4.
11559 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000011560#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011561 if (fstatvfs == NULL) {
11562 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
11563 return NULL;
11564 }
11565 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011566#endif /* HAVE_FSTATVFS */
11567
11568#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011569 if (statvfs == NULL) {
11570 if (PyObject_DelAttrString(m, "statvfs") == -1) {
11571 return NULL;
11572 }
11573 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011574#endif /* HAVE_STATVFS */
11575
11576# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000011577 if (lchown == NULL) {
11578 if (PyObject_DelAttrString(m, "lchown") == -1) {
11579 return NULL;
11580 }
11581 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011582#endif /* HAVE_LCHOWN */
11583
11584
11585#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +000011586 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011587
Guido van Rossumb6775db1994-08-01 11:34:53 +000011588}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011589
11590#ifdef __cplusplus
11591}
11592#endif