summaryrefslogtreecommitdiff
path: root/TSRM/tsrm_win32.c
diff options
context:
space:
mode:
Diffstat (limited to 'TSRM/tsrm_win32.c')
-rw-r--r--TSRM/tsrm_win32.c135
1 files changed, 97 insertions, 38 deletions
diff --git a/TSRM/tsrm_win32.c b/TSRM/tsrm_win32.c
index 0fec4a0e9a..5854046cad 100644
--- a/TSRM/tsrm_win32.c
+++ b/TSRM/tsrm_win32.c
@@ -1,6 +1,6 @@
/*
+----------------------------------------------------------------------+
- | PHP Version 5 |
+ | PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2016 The PHP Group |
+----------------------------------------------------------------------+
@@ -40,16 +40,19 @@ static ts_rsrc_id win32_globals_id;
static tsrm_win32_globals win32_globals;
#endif
-static void tsrm_win32_ctor(tsrm_win32_globals *globals TSRMLS_DC)
+static void tsrm_win32_ctor(tsrm_win32_globals *globals)
{
+#ifdef ZTS
+TSRMLS_CACHE_UPDATE();
+#endif
globals->process = NULL;
globals->shm = NULL;
globals->process_size = 0;
globals->shm_size = 0;
- globals->comspec = _strdup((GetVersion()<0x80000000)?"cmd.exe":"command.com");
+ globals->comspec = _strdup("cmd.exe");
/* Set it to INVALID_HANDLE_VALUE
- * It will be initialized correctly in tsrm_win32_access or set to
+ * It will be initialized correctly in tsrm_win32_access or set to
* NULL if no impersonation has been done.
* the impersonated token can't be set here as the impersonation
* will happen later, in fcgi_accept_request (or whatever is the
@@ -59,7 +62,7 @@ static void tsrm_win32_ctor(tsrm_win32_globals *globals TSRMLS_DC)
globals->impersonation_token_sid = NULL;
}
-static void tsrm_win32_dtor(tsrm_win32_globals *globals TSRMLS_DC)
+static void tsrm_win32_dtor(tsrm_win32_globals *globals)
{
shm_pair *ptr;
@@ -92,30 +95,32 @@ TSRM_API void tsrm_win32_startup(void)
#ifdef ZTS
ts_allocate_id(&win32_globals_id, sizeof(tsrm_win32_globals), (ts_allocate_ctor)tsrm_win32_ctor, (ts_allocate_ctor)tsrm_win32_dtor);
#else
- tsrm_win32_ctor(&win32_globals TSRMLS_CC);
+ tsrm_win32_ctor(&win32_globals);
#endif
}
TSRM_API void tsrm_win32_shutdown(void)
{
#ifndef ZTS
- tsrm_win32_dtor(&win32_globals TSRMLS_CC);
+ tsrm_win32_dtor(&win32_globals);
#endif
}
-char * tsrm_win32_get_path_sid_key(const char *pathname TSRMLS_DC)
+char * tsrm_win32_get_path_sid_key(const char *pathname)
{
PSID pSid = TWG(impersonation_token_sid);
- DWORD sid_len = pSid ? GetLengthSid(pSid) : 0;
TCHAR *ptcSid = NULL;
char *bucket_key = NULL;
+ size_t ptc_sid_len, pathname_len;
+
+ pathname_len = strlen(pathname);
if (!pSid) {
- bucket_key = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlen(pathname) + 1);
+ bucket_key = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, pathname_len + 1);
if (!bucket_key) {
return NULL;
}
- memcpy(bucket_key, pathname, strlen(pathname));
+ memcpy(bucket_key, pathname, pathname_len);
return bucket_key;
}
@@ -123,14 +128,16 @@ char * tsrm_win32_get_path_sid_key(const char *pathname TSRMLS_DC)
return NULL;
}
- bucket_key = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlen(pathname) + strlen(ptcSid) + 1);
+
+ ptc_sid_len = strlen(ptcSid);
+ bucket_key = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, pathname_len + ptc_sid_len + 1);
if (!bucket_key) {
LocalFree(ptcSid);
return NULL;
}
- memcpy(bucket_key, ptcSid, strlen(ptcSid));
- memcpy(bucket_key + strlen(ptcSid), pathname, strlen(pathname) + 1);
+ memcpy(bucket_key, ptcSid, ptc_sid_len);
+ memcpy(bucket_key + ptc_sid_len, pathname, pathname_len + 1);
LocalFree(ptcSid);
return bucket_key;
@@ -139,11 +146,8 @@ char * tsrm_win32_get_path_sid_key(const char *pathname TSRMLS_DC)
PSID tsrm_win32_get_token_sid(HANDLE hToken)
{
- BOOL bSuccess = FALSE;
DWORD dwLength = 0;
PTOKEN_USER pTokenUser = NULL;
- PSID sid;
- PSID *ppsid = &sid;
DWORD sid_len;
PSID pResultSid = NULL;
@@ -190,7 +194,7 @@ Finished:
return NULL;
}
-TSRM_API int tsrm_win32_access(const char *pathname, int mode TSRMLS_DC)
+TSRM_API int tsrm_win32_access(const char *pathname, int mode)
{
time_t t;
HANDLE thread_token = NULL;
@@ -204,7 +208,6 @@ TSRM_API int tsrm_win32_access(const char *pathname, int mode TSRMLS_DC)
BYTE * psec_desc = NULL;
BOOL fAccess = FALSE;
- BOOL bucket_key_alloc = FALSE;
realpath_cache_bucket * bucket = NULL;
char * real_path = NULL;
@@ -214,7 +217,7 @@ TSRM_API int tsrm_win32_access(const char *pathname, int mode TSRMLS_DC)
} else {
if(!IS_ABSOLUTE_PATH(pathname, strlen(pathname)+1)) {
real_path = (char *)malloc(MAX_PATH);
- if(tsrm_realpath(pathname, real_path TSRMLS_CC) == NULL) {
+ if(tsrm_realpath(pathname, real_path) == NULL) {
goto Finished;
}
pathname = real_path;
@@ -242,7 +245,6 @@ TSRM_API int tsrm_win32_access(const char *pathname, int mode TSRMLS_DC)
was impersonating already, this function uses that impersonation context.
*/
if(!OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, TRUE, &thread_token)) {
- DWORD err = GetLastError();
if (GetLastError() == ERROR_NO_TOKEN) {
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &thread_token)) {
TWG(impersonation_token) = NULL;
@@ -279,14 +281,14 @@ TSRM_API int tsrm_win32_access(const char *pathname, int mode TSRMLS_DC)
if (CWDG(realpath_cache_size_limit)) {
t = time(0);
- bucket = realpath_cache_lookup(pathname, strlen(pathname), t TSRMLS_CC);
+ bucket = realpath_cache_lookup(pathname, (int)strlen(pathname), t);
if(bucket == NULL && real_path == NULL) {
/* We used the pathname directly. Call tsrm_realpath */
/* so that entry is created in realpath cache */
real_path = (char *)malloc(MAX_PATH);
- if(tsrm_realpath(pathname, real_path TSRMLS_CC) != NULL) {
+ if(tsrm_realpath(pathname, real_path) != NULL) {
pathname = real_path;
- bucket = realpath_cache_lookup(pathname, strlen(pathname), t TSRMLS_CC);
+ bucket = realpath_cache_lookup(pathname, (int)strlen(pathname), t);
}
}
}
@@ -381,7 +383,7 @@ Finished:
}
-static process_pair *process_get(FILE *stream TSRMLS_DC)
+static process_pair *process_get(FILE *stream)
{
process_pair *ptr;
process_pair *newptr;
@@ -411,7 +413,6 @@ 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) {
@@ -436,6 +437,7 @@ static shm_pair *shm_get(int key, void *addr)
TWG(shm) = newptr;
ptr = newptr + TWG(shm_size);
TWG(shm_size)++;
+ memset(ptr, 0, sizeof(*ptr));
return ptr;
}
@@ -449,15 +451,14 @@ static HANDLE dupHandle(HANDLE fh, BOOL inherit) {
TSRM_API FILE *popen(const char *command, const char *type)
{
- TSRMLS_FETCH();
- return popen_ex(command, type, NULL, NULL TSRMLS_CC);
+ return popen_ex(command, type, NULL, NULL);
}
-TSRM_API FILE *popen_ex(const char *command, const char *type, const char *cwd, char *env TSRMLS_DC)
+TSRM_API FILE *popen_ex(const char *command, const char *type, const char *cwd, char *env)
{
FILE *stream = NULL;
- int fno, type_len = strlen(type), read, mode;
+ int fno, type_len, read, mode;
STARTUPINFO startup;
PROCESS_INFORMATION process;
SECURITY_ATTRIBUTES security;
@@ -477,7 +478,7 @@ TSRM_API FILE *popen_ex(const char *command, const char *type, const char *cwd,
}
/*The following two checks can be removed once we drop XP support */
- type_len = strlen(type);
+ type_len = (int)strlen(type);
if (type_len <1 || type_len > 2) {
return NULL;
}
@@ -551,7 +552,7 @@ TSRM_API FILE *popen_ex(const char *command, const char *type, const char *cwd,
}
CloseHandle(process.hThread);
- proc = process_get(NULL TSRMLS_CC);
+ proc = process_get(NULL);
if (read) {
fno = _open_osfhandle((tsrm_intptr_t)in, _O_RDONLY | mode);
@@ -571,9 +572,8 @@ TSRM_API int pclose(FILE *stream)
{
DWORD termstat = 0;
process_pair *process;
- TSRMLS_FETCH();
- if ((process = process_get(stream TSRMLS_CC)) == NULL) {
+ if ((process = process_get(stream)) == NULL) {
return 0;
}
@@ -599,19 +599,25 @@ TSRM_API int shmget(int key, int size, int flags)
return -1;
}
- sprintf(shm_segment, "TSRM_SHM_SEGMENT:%d", key);
- sprintf(shm_info, "TSRM_SHM_DESCRIPTOR:%d", key);
+ snprintf(shm_segment, sizeof(shm_segment), "TSRM_SHM_SEGMENT:%d", key);
+ snprintf(shm_info, sizeof(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 (!shm_handle && !info_handle) {
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)) {
+ if (!shm_handle || !info_handle) {
+ if (shm_handle) {
+ CloseHandle(shm_handle);
+ }
+ if (info_handle) {
+ CloseHandle(info_handle);
+ }
return -1;
}
} else {
@@ -627,6 +633,11 @@ TSRM_API int shmget(int key, int size, int flags)
}
shm = shm_get(key, NULL);
+ if (!shm) {
+ CloseHandle(shm_handle);
+ CloseHandle(info_handle);
+ return -1;
+ }
shm->segment = shm_handle;
shm->info = info_handle;
shm->descriptor = MapViewOfFileEx(shm->info, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL);
@@ -738,4 +749,52 @@ TSRM_API char *realpath(char *orig_path, char *buffer)
return buffer;
}
+#if HAVE_UTIME
+static zend_always_inline void UnixTimeToFileTime(time_t t, LPFILETIME pft) /* {{{ */
+{
+ // Note that LONGLONG is a 64-bit value
+ LONGLONG ll;
+
+ ll = Int32x32To64(t, 10000000) + 116444736000000000;
+ pft->dwLowDateTime = (DWORD)ll;
+ pft->dwHighDateTime = ll >> 32;
+}
+/* }}} */
+
+TSRM_API int win32_utime(const char *filename, struct utimbuf *buf) /* {{{ */
+{
+ FILETIME mtime, atime;
+ HANDLE hFile;
+
+ hFile = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL,
+ OPEN_ALWAYS, FILE_FLAG_BACKUP_SEMANTICS, NULL);
+
+ /* OPEN_ALWAYS mode sets the last error to ERROR_ALREADY_EXISTS but
+ the CreateFile operation succeeds */
+ if (GetLastError() == ERROR_ALREADY_EXISTS) {
+ SetLastError(0);
+ }
+
+ if ( hFile == INVALID_HANDLE_VALUE ) {
+ return -1;
+ }
+
+ if (!buf) {
+ SYSTEMTIME st;
+ GetSystemTime(&st);
+ SystemTimeToFileTime(&st, &mtime);
+ atime = mtime;
+ } else {
+ UnixTimeToFileTime(buf->modtime, &mtime);
+ UnixTimeToFileTime(buf->actime, &atime);
+ }
+ if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
+ CloseHandle(hFile);
+ return -1;
+ }
+ CloseHandle(hFile);
+ return 1;
+}
+/* }}} */
+#endif
#endif