2001-04-28 00:41:53 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2004-01-08 16:18:22 +08:00
|
|
|
| PHP Version 5 |
|
2001-04-28 00:41:53 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 20:51:34 +08:00
|
|
|
| Copyright (c) 1997-2006 The PHP Group |
|
2001-04-28 00:41:53 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 20:51:34 +08:00
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
2001-04-28 00:41:53 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-25 13:05:06 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
2006-01-01 20:51:34 +08:00
|
|
|
| http://www.php.net/license/3_01.txt |
|
2001-04-28 00:41:53 +08:00
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Daniel Beulshausen <daniel@php4win.de> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <io.h>
|
|
|
|
#include <process.h>
|
2001-08-07 21:06:23 +08:00
|
|
|
#include <time.h>
|
2001-04-28 00:41:53 +08:00
|
|
|
|
2003-06-25 13:05:06 +08:00
|
|
|
#define TSRM_INCLUDE_FULL_WINDOWS_HEADERS
|
|
|
|
|
2001-04-28 00:41:53 +08:00
|
|
|
#include "TSRM.h"
|
|
|
|
|
|
|
|
#ifdef TSRM_WIN32
|
2003-12-22 23:01:05 +08:00
|
|
|
|
2001-04-28 00:41:53 +08:00
|
|
|
#include "tsrm_win32.h"
|
|
|
|
|
|
|
|
#ifdef ZTS
|
|
|
|
static ts_rsrc_id win32_globals_id;
|
|
|
|
#else
|
|
|
|
static tsrm_win32_globals win32_globals;
|
|
|
|
#endif
|
|
|
|
|
2001-07-27 18:08:26 +08:00
|
|
|
static void tsrm_win32_ctor(tsrm_win32_globals *globals TSRMLS_DC)
|
2001-04-28 02:50:35 +08:00
|
|
|
{
|
2001-04-28 00:41:53 +08:00
|
|
|
globals->process = NULL;
|
2001-08-07 21:06:23 +08:00
|
|
|
globals->shm = NULL;
|
2001-04-28 00:41:53 +08:00
|
|
|
globals->process_size = 0;
|
2001-08-07 21:06:23 +08:00
|
|
|
globals->shm_size = 0;
|
2001-07-02 04:08:21 +08:00
|
|
|
globals->comspec = _strdup((GetVersion()<0x80000000)?"cmd.exe":"command.com");
|
2001-04-28 00:41:53 +08:00
|
|
|
}
|
|
|
|
|
2001-07-27 18:08:26 +08:00
|
|
|
static void tsrm_win32_dtor(tsrm_win32_globals *globals TSRMLS_DC)
|
2001-04-28 02:50:35 +08:00
|
|
|
{
|
2001-08-07 21:06:23 +08:00
|
|
|
shm_pair *ptr;
|
|
|
|
|
|
|
|
if (globals->process) {
|
2001-04-28 00:41:53 +08:00
|
|
|
free(globals->process);
|
|
|
|
}
|
2001-08-07 21:06:23 +08:00
|
|
|
|
|
|
|
if (globals->shm) {
|
|
|
|
for (ptr = globals->shm; ptr < (globals->shm + globals->shm_size); ptr++) {
|
|
|
|
UnmapViewOfFile(ptr->addr);
|
|
|
|
CloseHandle(ptr->segment);
|
|
|
|
UnmapViewOfFile(ptr->descriptor);
|
|
|
|
CloseHandle(ptr->info);
|
|
|
|
}
|
|
|
|
free(globals->shm);
|
|
|
|
}
|
|
|
|
|
2001-07-02 04:08:21 +08:00
|
|
|
free(globals->comspec);
|
2001-04-28 00:41:53 +08:00
|
|
|
}
|
|
|
|
|
2001-04-28 02:50:35 +08:00
|
|
|
TSRM_API void tsrm_win32_startup(void)
|
|
|
|
{
|
2001-04-28 00:41:53 +08:00
|
|
|
#ifdef ZTS
|
2001-07-27 18:08:26 +08:00
|
|
|
ts_allocate_id(&win32_globals_id, sizeof(tsrm_win32_globals), (ts_allocate_ctor)tsrm_win32_ctor, (ts_allocate_ctor)tsrm_win32_dtor);
|
2001-04-28 00:41:53 +08:00
|
|
|
#else
|
2001-07-27 18:08:26 +08:00
|
|
|
tsrm_win32_ctor(&win32_globals TSRMLS_CC);
|
2001-04-28 00:41:53 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-04-28 02:50:35 +08:00
|
|
|
TSRM_API void tsrm_win32_shutdown(void)
|
|
|
|
{
|
2001-04-28 00:41:53 +08:00
|
|
|
#ifndef ZTS
|
2001-07-27 18:08:26 +08:00
|
|
|
tsrm_win32_dtor(&win32_globals TSRMLS_CC);
|
2001-04-28 00:41:53 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-06-25 13:05:06 +08:00
|
|
|
TSRM_API int tsrm_win32_access(const char *pathname, int mode)
|
|
|
|
{
|
|
|
|
if (mode == 1 /*X_OK*/) {
|
2006-12-21 17:01:10 +08:00
|
|
|
#if 1
|
|
|
|
/* This code is not supported by Windows 98,
|
|
|
|
* but we don't support it anymore */
|
|
|
|
DWORD type;
|
|
|
|
|
|
|
|
return GetBinaryType(pathname, &type)?0:-1;
|
|
|
|
#else
|
|
|
|
SHFILEINFO sfi;
|
|
|
|
|
2003-06-25 13:05:06 +08:00
|
|
|
return access(pathname, 0) == 0 &&
|
|
|
|
SHGetFileInfo(pathname, 0, &sfi, sizeof(SHFILEINFO), SHGFI_EXETYPE) != 0 ? 0 : -1;
|
2006-12-21 17:01:10 +08:00
|
|
|
#endif
|
2003-06-25 13:05:06 +08:00
|
|
|
} else {
|
|
|
|
return access(pathname, mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-07 21:06:23 +08:00
|
|
|
static process_pair *process_get(FILE *stream TSRMLS_DC)
|
2001-04-28 02:50:35 +08:00
|
|
|
{
|
2001-08-07 21:06:23 +08:00
|
|
|
process_pair *ptr;
|
|
|
|
process_pair *newptr;
|
2001-04-28 00:41:53 +08:00
|
|
|
|
2001-04-28 02:50:35 +08:00
|
|
|
for (ptr = TWG(process); ptr < (TWG(process) + TWG(process_size)); ptr++) {
|
2001-07-10 00:44:40 +08:00
|
|
|
if (ptr->stream == stream) {
|
2001-04-28 00:41:53 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-04-28 02:50:35 +08:00
|
|
|
if (ptr < (TWG(process) + TWG(process_size))) {
|
2001-04-28 00:41:53 +08:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2001-08-07 21:06:23 +08:00
|
|
|
newptr = (process_pair*)realloc((void*)TWG(process), (TWG(process_size)+1)*sizeof(process_pair));
|
2001-04-28 02:50:35 +08:00
|
|
|
if (newptr == NULL) {
|
2001-04-28 00:41:53 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
TWG(process) = newptr;
|
|
|
|
ptr = newptr + TWG(process_size);
|
|
|
|
TWG(process_size)++;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2001-08-07 21:06:23 +08:00
|
|
|
static shm_pair *shm_get(int key, void *addr)
|
|
|
|
{
|
|
|
|
shm_pair *ptr;
|
|
|
|
shm_pair *newptr;
|
|
|
|
TSRMLS_FETCH();
|
|
|
|
|
|
|
|
for (ptr = TWG(shm); ptr < (TWG(shm) + TWG(shm_size)); ptr++) {
|
|
|
|
if (!ptr->descriptor) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!addr && ptr->descriptor->shm_perm.key == key) {
|
|
|
|
break;
|
|
|
|
} else if (ptr->addr == addr) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ptr < (TWG(shm) + TWG(shm_size))) {
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
newptr = (shm_pair*)realloc((void*)TWG(shm), (TWG(shm_size)+1)*sizeof(shm_pair));
|
|
|
|
if (newptr == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
TWG(shm) = newptr;
|
|
|
|
ptr = newptr + TWG(shm_size);
|
|
|
|
TWG(shm_size)++;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2001-07-10 00:44:40 +08:00
|
|
|
static HANDLE dupHandle(HANDLE fh, BOOL inherit) {
|
|
|
|
HANDLE copy, self = GetCurrentProcess();
|
|
|
|
if (!DuplicateHandle(self, fh, self, ©, 0, inherit, DUPLICATE_SAME_ACCESS|DUPLICATE_CLOSE_SOURCE)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2001-08-05 09:34:40 +08:00
|
|
|
TSRM_API FILE *popen(const char *command, const char *type)
|
2002-10-29 15:32:52 +08:00
|
|
|
{
|
|
|
|
return popen_ex(command, type, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
TSRM_API FILE *popen_ex(const char *command, const char *type, const char *cwd, char *env)
|
2001-04-28 02:50:35 +08:00
|
|
|
{
|
2001-04-28 00:41:53 +08:00
|
|
|
FILE *stream = NULL;
|
|
|
|
int fno, str_len = strlen(type), read, mode;
|
|
|
|
STARTUPINFO startup;
|
|
|
|
PROCESS_INFORMATION process;
|
|
|
|
SECURITY_ATTRIBUTES security;
|
|
|
|
HANDLE in, out;
|
2001-07-02 04:08:21 +08:00
|
|
|
char *cmd;
|
2001-08-07 21:06:23 +08:00
|
|
|
process_pair *proc;
|
2001-07-28 18:46:11 +08:00
|
|
|
TSRMLS_FETCH();
|
2001-04-28 00:41:53 +08:00
|
|
|
|
|
|
|
security.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
|
|
security.bInheritHandle = TRUE;
|
|
|
|
security.lpSecurityDescriptor = NULL;
|
|
|
|
|
2001-04-28 02:50:35 +08:00
|
|
|
if (!str_len || !CreatePipe(&in, &out, &security, 2048L)) {
|
2001-04-28 00:41:53 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&startup, 0, sizeof(STARTUPINFO));
|
2001-04-28 02:50:35 +08:00
|
|
|
memset(&process, 0, sizeof(PROCESS_INFORMATION));
|
2001-04-28 00:41:53 +08:00
|
|
|
|
|
|
|
startup.cb = sizeof(STARTUPINFO);
|
|
|
|
startup.dwFlags = STARTF_USESTDHANDLES;
|
|
|
|
startup.hStdError = GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
|
|
|
|
read = (type[0] == 'r') ? TRUE : FALSE;
|
|
|
|
mode = ((str_len == 2) && (type[1] == 'b')) ? O_BINARY : O_TEXT;
|
|
|
|
|
|
|
|
|
2001-04-28 02:50:35 +08:00
|
|
|
if (read) {
|
2001-07-11 23:10:56 +08:00
|
|
|
in = dupHandle(in, FALSE);
|
2001-04-28 00:41:53 +08:00
|
|
|
startup.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
startup.hStdOutput = out;
|
2001-04-28 02:50:35 +08:00
|
|
|
} else {
|
2001-07-11 23:10:56 +08:00
|
|
|
out = dupHandle(out, FALSE);
|
2001-04-28 00:41:53 +08:00
|
|
|
startup.hStdInput = in;
|
|
|
|
startup.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
}
|
|
|
|
|
2001-07-08 04:00:35 +08:00
|
|
|
cmd = (char*)malloc(strlen(command)+strlen(TWG(comspec))+sizeof(" /c "));
|
2001-07-02 04:08:21 +08:00
|
|
|
sprintf(cmd, "%s /c %s", TWG(comspec), command);
|
2002-10-29 15:32:52 +08:00
|
|
|
if (!CreateProcess(NULL, cmd, &security, &security, security.bInheritHandle, NORMAL_PRIORITY_CLASS, env, cwd, &startup, &process)) {
|
2001-04-28 00:41:53 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-07-02 04:08:21 +08:00
|
|
|
free(cmd);
|
2001-04-28 00:41:53 +08:00
|
|
|
|
|
|
|
CloseHandle(process.hThread);
|
2001-08-05 09:34:40 +08:00
|
|
|
proc = process_get(NULL TSRMLS_CC);
|
2001-04-28 00:41:53 +08:00
|
|
|
|
2001-04-28 02:50:35 +08:00
|
|
|
if (read) {
|
2001-04-28 00:41:53 +08:00
|
|
|
fno = _open_osfhandle((long)in, _O_RDONLY | mode);
|
|
|
|
CloseHandle(out);
|
2001-04-28 02:50:35 +08:00
|
|
|
} else {
|
2001-04-28 00:41:53 +08:00
|
|
|
fno = _open_osfhandle((long)out, _O_WRONLY | mode);
|
|
|
|
CloseHandle(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
stream = _fdopen(fno, type);
|
|
|
|
proc->prochnd = process.hProcess;
|
|
|
|
proc->stream = stream;
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2001-08-05 09:34:40 +08:00
|
|
|
TSRM_API int pclose(FILE *stream)
|
2001-04-28 02:50:35 +08:00
|
|
|
{
|
2001-04-28 00:41:53 +08:00
|
|
|
DWORD termstat = 0;
|
2001-08-07 21:06:23 +08:00
|
|
|
process_pair *process;
|
2001-08-05 09:34:40 +08:00
|
|
|
TSRMLS_FETCH();
|
2001-04-28 00:41:53 +08:00
|
|
|
|
2001-08-05 09:34:40 +08:00
|
|
|
if ((process = process_get(stream TSRMLS_CC)) == NULL) {
|
2001-04-28 00:41:53 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fflush(process->stream);
|
|
|
|
fclose(process->stream);
|
|
|
|
|
2001-07-10 00:44:40 +08:00
|
|
|
WaitForSingleObject(process->prochnd, INFINITE);
|
2001-04-28 00:41:53 +08:00
|
|
|
GetExitCodeProcess(process->prochnd, &termstat);
|
|
|
|
process->stream = NULL;
|
|
|
|
CloseHandle(process->prochnd);
|
|
|
|
|
|
|
|
return termstat;
|
|
|
|
}
|
2001-04-28 02:50:35 +08:00
|
|
|
|
2001-08-07 21:06:23 +08:00
|
|
|
TSRM_API int shmget(int key, int size, int flags)
|
|
|
|
{
|
|
|
|
shm_pair *shm;
|
|
|
|
char shm_segment[26], shm_info[29];
|
|
|
|
HANDLE shm_handle, info_handle;
|
|
|
|
BOOL created = FALSE;
|
|
|
|
|
|
|
|
if (size < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(shm_segment, "TSRM_SHM_SEGMENT:%d", key);
|
|
|
|
sprintf(shm_info, "TSRM_SHM_DESCRIPTOR:%d", key);
|
|
|
|
|
|
|
|
shm_handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, shm_segment);
|
|
|
|
info_handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, shm_info);
|
|
|
|
|
|
|
|
if ((!shm_handle && !info_handle)) {
|
|
|
|
if (flags & IPC_EXCL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (flags & IPC_CREAT) {
|
|
|
|
shm_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, shm_segment);
|
|
|
|
info_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(shm->descriptor), shm_info);
|
|
|
|
created = TRUE;
|
|
|
|
}
|
|
|
|
if ((!shm_handle || !info_handle)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
shm = shm_get(key, NULL);
|
|
|
|
shm->segment = shm_handle;
|
|
|
|
shm->info = info_handle;
|
|
|
|
shm->descriptor = MapViewOfFileEx(shm->info, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL);
|
|
|
|
|
|
|
|
if (created) {
|
|
|
|
shm->descriptor->shm_perm.key = key;
|
|
|
|
shm->descriptor->shm_segsz = size;
|
|
|
|
shm->descriptor->shm_ctime = time(NULL);
|
|
|
|
shm->descriptor->shm_cpid = getpid();
|
|
|
|
shm->descriptor->shm_perm.mode = flags;
|
|
|
|
|
|
|
|
shm->descriptor->shm_perm.cuid = shm->descriptor->shm_perm.cgid= 0;
|
|
|
|
shm->descriptor->shm_perm.gid = shm->descriptor->shm_perm.uid = 0;
|
|
|
|
shm->descriptor->shm_atime = shm->descriptor->shm_dtime = 0;
|
|
|
|
shm->descriptor->shm_lpid = shm->descriptor->shm_nattch = 0;
|
|
|
|
shm->descriptor->shm_perm.mode = shm->descriptor->shm_perm.seq = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shm->descriptor->shm_perm.key != key || size > shm->descriptor->shm_segsz ) {
|
|
|
|
CloseHandle(shm->segment);
|
|
|
|
UnmapViewOfFile(shm->descriptor);
|
|
|
|
CloseHandle(shm->info);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
TSRM_API void *shmat(int key, const void *shmaddr, int flags)
|
|
|
|
{
|
|
|
|
shm_pair *shm = shm_get(key, NULL);
|
|
|
|
|
|
|
|
if (!shm->segment) {
|
|
|
|
return (void*)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
shm->descriptor->shm_atime = time(NULL);
|
|
|
|
shm->descriptor->shm_lpid = getpid();
|
|
|
|
shm->descriptor->shm_nattch++;
|
|
|
|
|
|
|
|
shm->addr = MapViewOfFileEx(shm->segment, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL);
|
|
|
|
|
|
|
|
return shm->addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
TSRM_API int shmdt(const void *shmaddr)
|
|
|
|
{
|
|
|
|
shm_pair *shm = shm_get(0, (void*)shmaddr);
|
|
|
|
|
|
|
|
if (!shm->segment) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
shm->descriptor->shm_dtime = time(NULL);
|
|
|
|
shm->descriptor->shm_lpid = getpid();
|
|
|
|
shm->descriptor->shm_nattch--;
|
|
|
|
|
|
|
|
return UnmapViewOfFile(shm->addr) ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
TSRM_API int shmctl(int key, int cmd, struct shmid_ds *buf) {
|
|
|
|
shm_pair *shm = shm_get(key, NULL);
|
|
|
|
|
|
|
|
if (!shm->segment) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case IPC_STAT:
|
2001-08-07 21:29:51 +08:00
|
|
|
memcpy(buf, shm->descriptor, sizeof(struct shmid_ds));
|
2001-08-07 21:06:23 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case IPC_SET:
|
|
|
|
shm->descriptor->shm_ctime = time(NULL);
|
|
|
|
shm->descriptor->shm_perm.uid = buf->shm_perm.uid;
|
|
|
|
shm->descriptor->shm_perm.gid = buf->shm_perm.gid;
|
|
|
|
shm->descriptor->shm_perm.mode = buf->shm_perm.mode;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case IPC_RMID:
|
|
|
|
if (shm->descriptor->shm_nattch < 1) {
|
|
|
|
shm->descriptor->shm_perm.key = -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2003-09-30 17:48:53 +08:00
|
|
|
|
|
|
|
TSRM_API char *realpath(char *orig_path, char *buffer)
|
|
|
|
{
|
2003-12-02 04:18:55 +08:00
|
|
|
int ret = GetFullPathName(orig_path, _MAX_PATH, buffer, NULL);
|
|
|
|
if(!ret || ret > _MAX_PATH) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return buffer;
|
2003-09-30 17:48:53 +08:00
|
|
|
}
|
2003-12-02 04:18:55 +08:00
|
|
|
|
2003-06-25 13:05:06 +08:00
|
|
|
#endif
|