2000-09-03 12:16:35 +08:00
|
|
|
/* cygheap.cc: Cygwin heap manager.
|
|
|
|
|
2002-06-05 12:01:43 +08:00
|
|
|
Copyright 2000, 2001, 2002 Red Hat, Inc.
|
2000-09-03 12:16:35 +08:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
2001-05-05 04:39:38 +08:00
|
|
|
#include <string.h>
|
2000-09-03 12:16:35 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
2000-10-02 10:26:04 +08:00
|
|
|
#include <stdlib.h>
|
2001-07-27 03:22:24 +08:00
|
|
|
#include "security.h"
|
2001-04-19 05:10:15 +08:00
|
|
|
#include "fhandler.h"
|
2001-09-10 00:52:37 +08:00
|
|
|
#include "path.h"
|
2001-10-01 12:10:07 +08:00
|
|
|
#include "dtable.h"
|
2001-10-16 07:39:33 +08:00
|
|
|
#include "cygerrno.h"
|
2000-09-03 12:16:35 +08:00
|
|
|
#include "cygheap.h"
|
2001-07-17 11:41:52 +08:00
|
|
|
#include "child_info.h"
|
2000-09-03 12:16:35 +08:00
|
|
|
#include "heap.h"
|
2000-10-02 10:26:04 +08:00
|
|
|
#include "sync.h"
|
2001-01-28 13:51:15 +08:00
|
|
|
#include "shared_info.h"
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2000-11-14 13:53:32 +08:00
|
|
|
init_cygheap NO_COPY *cygheap;
|
2002-10-15 04:25:52 +08:00
|
|
|
void NO_COPY *cygheap_max;
|
2000-09-30 12:54:11 +08:00
|
|
|
|
2002-10-15 04:25:52 +08:00
|
|
|
static NO_COPY muto *cygheap_protect;
|
2000-10-02 10:26:04 +08:00
|
|
|
|
2001-07-17 11:41:52 +08:00
|
|
|
struct cygheap_entry
|
|
|
|
{
|
|
|
|
int type;
|
|
|
|
struct cygheap_entry *next;
|
|
|
|
char data[0];
|
|
|
|
};
|
|
|
|
|
2001-09-06 11:39:18 +08:00
|
|
|
#define NBUCKETS (sizeof (cygheap->buckets) / sizeof (cygheap->buckets[0]))
|
2001-07-17 11:41:52 +08:00
|
|
|
#define N0 ((_cmalloc_entry *) NULL)
|
|
|
|
#define to_cmalloc(s) ((_cmalloc_entry *) (((char *) (s)) - (int) (N0->data)))
|
|
|
|
|
2001-08-05 05:10:52 +08:00
|
|
|
#define CFMAP_OPTIONS (SEC_RESERVE | PAGE_READWRITE)
|
|
|
|
#define MVMAP_OPTIONS (FILE_MAP_WRITE)
|
|
|
|
|
2001-07-17 11:41:52 +08:00
|
|
|
extern "C" {
|
|
|
|
static void __stdcall _cfree (void *ptr) __attribute__((regparm(1)));
|
|
|
|
}
|
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
inline static void
|
|
|
|
init_cheap ()
|
|
|
|
{
|
2001-08-31 13:06:14 +08:00
|
|
|
cygheap = (init_cygheap *) VirtualAlloc ((void *) &_cygheap_start, CYGHEAPSIZE, MEM_RESERVE, PAGE_NOACCESS);
|
|
|
|
if (!cygheap)
|
2001-08-20 23:58:16 +08:00
|
|
|
{
|
2001-08-31 13:06:14 +08:00
|
|
|
MEMORY_BASIC_INFORMATION m;
|
|
|
|
if (!VirtualQuery ((LPCVOID) &_cygheap_start, &m, sizeof m))
|
|
|
|
system_printf ("couldn't get memory info, %E");
|
2002-07-03 11:20:50 +08:00
|
|
|
system_printf ("Couldn't reserve space for cygwin's heap, %E");
|
|
|
|
api_fatal ("AllocationBase %p, BaseAddress %p, RegionSize %p, State %p\n",
|
|
|
|
m.AllocationBase, m.BaseAddress, m.RegionSize, m.State);
|
2001-08-20 23:58:16 +08:00
|
|
|
}
|
2002-08-07 09:22:56 +08:00
|
|
|
cygheap_max = cygheap;
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
|
|
|
|
2002-10-15 04:25:52 +08:00
|
|
|
static void dup_now (void *, child_info *, unsigned) __attribute__ ((regparm(3)));
|
2001-09-14 08:49:00 +08:00
|
|
|
static void
|
|
|
|
dup_now (void *newcygheap, child_info *ci, unsigned n)
|
|
|
|
{
|
|
|
|
if (!VirtualAlloc (newcygheap, n, MEM_COMMIT, PAGE_READWRITE))
|
2002-10-09 12:08:05 +08:00
|
|
|
api_fatal ("couldn't allocate new cygwin heap %p, %d for child, %E",
|
|
|
|
newcygheap, n);
|
2001-09-14 08:49:00 +08:00
|
|
|
memcpy (newcygheap, cygheap, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *__stdcall
|
|
|
|
cygheap_setup_for_child (child_info *ci, bool dup_later)
|
2001-07-17 11:41:52 +08:00
|
|
|
{
|
|
|
|
void *newcygheap;
|
|
|
|
cygheap_protect->acquire ();
|
|
|
|
unsigned n = (char *) cygheap_max - (char *) cygheap;
|
2002-10-09 12:08:05 +08:00
|
|
|
unsigned size = CYGHEAPSIZE;
|
|
|
|
if (size < n)
|
|
|
|
size = n + (128 * 1024);
|
2001-07-17 11:41:52 +08:00
|
|
|
ci->cygheap_h = CreateFileMapping (INVALID_HANDLE_VALUE, &sec_none,
|
2002-10-09 12:08:05 +08:00
|
|
|
CFMAP_OPTIONS, 0, size, NULL);
|
|
|
|
if (!ci->cygheap_h)
|
|
|
|
api_fatal ("Couldn't create heap for child, size %d, %E", CYGHEAPSIZE);
|
2001-08-05 05:10:52 +08:00
|
|
|
newcygheap = MapViewOfFileEx (ci->cygheap_h, MVMAP_OPTIONS, 0, 0, 0, NULL);
|
2002-07-14 04:00:27 +08:00
|
|
|
ProtectHandle1INH (ci->cygheap_h, passed_cygheap_h);
|
2001-09-14 08:49:00 +08:00
|
|
|
if (!dup_later)
|
|
|
|
dup_now (newcygheap, ci, n);
|
2001-07-17 11:41:52 +08:00
|
|
|
cygheap_protect->release ();
|
2001-09-14 08:49:00 +08:00
|
|
|
ci->cygheap = cygheap;
|
|
|
|
ci->cygheap_max = cygheap_max;
|
|
|
|
return newcygheap;
|
2001-07-17 11:41:52 +08:00
|
|
|
}
|
|
|
|
|
2001-08-05 05:10:52 +08:00
|
|
|
void __stdcall
|
2001-09-14 08:49:00 +08:00
|
|
|
cygheap_setup_for_child_cleanup (void *newcygheap, child_info *ci,
|
2001-11-05 14:09:15 +08:00
|
|
|
bool dup_it_now)
|
2001-08-05 05:10:52 +08:00
|
|
|
{
|
2001-09-14 08:49:00 +08:00
|
|
|
if (dup_it_now)
|
|
|
|
{
|
|
|
|
/* NOTE: There is an assumption here that cygheap_max has not changed
|
2001-11-05 14:09:15 +08:00
|
|
|
between the time that cygheap_setup_for_child was called and now.
|
2001-09-14 08:49:00 +08:00
|
|
|
Make sure that this is a correct assumption. */
|
|
|
|
cygheap_protect->acquire ();
|
|
|
|
dup_now (newcygheap, ci, (char *) cygheap_max - (char *) cygheap);
|
|
|
|
cygheap_protect->release ();
|
|
|
|
}
|
|
|
|
UnmapViewOfFile (newcygheap);
|
2001-08-05 05:10:52 +08:00
|
|
|
ForceCloseHandle1 (ci->cygheap_h, passed_cygheap_h);
|
|
|
|
}
|
|
|
|
|
2001-07-17 11:41:52 +08:00
|
|
|
/* Called by fork or spawn to reallocate cygwin heap */
|
|
|
|
void __stdcall
|
2002-06-16 05:59:32 +08:00
|
|
|
cygheap_fixup_in_child (bool execed)
|
2001-07-17 11:41:52 +08:00
|
|
|
{
|
2002-06-16 05:59:32 +08:00
|
|
|
cygheap = child_proc_info->cygheap;
|
|
|
|
cygheap_max = child_proc_info->cygheap_max;
|
* Makefile.in: Build wincap.o.
* wincap.cc: New file.
* wincap.h: Ditto.
* autoload.cc: Add dynamic load statement for `CreateHardLinkA'.
* dcrt0.cc (os_being_run): Eliminated.
(osname): Ditto.
(iswinnt): Ditto.
(set_os_type): Ditto.
(dll_crt0_1): Call wincap.init() instead of set_os_type().
(_dll_crt0): Ditto.
* environ.cc (set_chunksize): New function.
(parse_thing): `forkchunk' setting now invokes function `set_chunksize'.
* fork.cc (chunksize): Eliminated. Moved to be member of wincap.
* host_dependent.h: Removed.
* syscalls.cc (_link): Try using `CreateHardLinkA' first, if available.
* cygheap.cc, dcrt0.cc, delqueue.cc, dir.cc,
environ.cc, fhandler.cc, fhandler.h, fhandler_console.cc,
fhandler_mem.cc, fork.cc, mmap.cc, net.cc, pinfo.cc, pinfo.h,
security.cc, syscalls.cc, sysconf.cc, syslog.cc, thread.cc,
times.cc, tty.cc, uinfo.cc, uname.cc, winsup.h: Use new wincap
capability check throughout.
* winsup.h: Include wincap.h. Eliminate extern declarations of
`os_being_run' and `iswinnt'. Eliminate `os_type" definition.
* include/cygwin/version.h: Bump version to 1.3.4.
2001-09-13 01:46:37 +08:00
|
|
|
void *addr = !wincap.map_view_of_file_ex_sucks () ? cygheap : NULL;
|
2001-08-05 05:10:52 +08:00
|
|
|
void *newaddr;
|
2001-08-31 13:06:14 +08:00
|
|
|
|
2002-06-16 05:59:32 +08:00
|
|
|
newaddr = MapViewOfFileEx (child_proc_info->cygheap_h, MVMAP_OPTIONS, 0, 0, 0, addr);
|
2001-08-31 13:06:14 +08:00
|
|
|
if (newaddr != cygheap)
|
2001-08-05 05:10:52 +08:00
|
|
|
{
|
2001-08-31 13:06:14 +08:00
|
|
|
if (!newaddr)
|
2002-06-16 05:59:32 +08:00
|
|
|
newaddr = MapViewOfFileEx (child_proc_info->cygheap_h, MVMAP_OPTIONS, 0, 0, 0, NULL);
|
2001-08-05 05:10:52 +08:00
|
|
|
DWORD n = (DWORD) cygheap_max - (DWORD) cygheap;
|
|
|
|
/* Reserve cygwin heap in same spot as parent */
|
|
|
|
if (!VirtualAlloc (cygheap, CYGHEAPSIZE, MEM_RESERVE, PAGE_NOACCESS))
|
2002-07-03 11:20:50 +08:00
|
|
|
{
|
|
|
|
MEMORY_BASIC_INFORMATION m;
|
|
|
|
memset (&m, 0, sizeof m);
|
|
|
|
if (!VirtualQuery ((LPCVOID) cygheap, &m, sizeof m))
|
|
|
|
system_printf ("couldn't get memory info, %E");
|
|
|
|
|
|
|
|
system_printf ("Couldn't reserve space for cygwin's heap (%p <%p>) in child, %E", cygheap, newaddr);
|
|
|
|
api_fatal ("m.AllocationBase %p, m.BaseAddress %p, m.RegionSize %p, m.State %p\n",
|
|
|
|
m.AllocationBase, m.BaseAddress, m.RegionSize, m.State);
|
|
|
|
}
|
2001-08-05 05:10:52 +08:00
|
|
|
|
|
|
|
/* Allocate same amount of memory as parent */
|
|
|
|
if (!VirtualAlloc (cygheap, n, MEM_COMMIT, PAGE_READWRITE))
|
2001-08-31 13:06:14 +08:00
|
|
|
api_fatal ("Couldn't allocate space for child's heap %p, size %d, %E",
|
|
|
|
cygheap, n);
|
2001-08-05 05:10:52 +08:00
|
|
|
memcpy (cygheap, newaddr, n);
|
|
|
|
UnmapViewOfFile (newaddr);
|
|
|
|
}
|
2001-07-17 11:41:52 +08:00
|
|
|
|
2002-06-16 05:59:32 +08:00
|
|
|
ForceCloseHandle1 (child_proc_info->cygheap_h, passed_cygheap_h);
|
2001-08-05 05:10:52 +08:00
|
|
|
|
2001-07-17 11:41:52 +08:00
|
|
|
cygheap_init ();
|
2002-07-30 09:31:51 +08:00
|
|
|
debug_fixup_after_fork_exec ();
|
2001-07-17 11:41:52 +08:00
|
|
|
|
|
|
|
if (execed)
|
|
|
|
{
|
2001-09-10 03:06:50 +08:00
|
|
|
cygheap->heapbase = NULL; /* We can allocate the heap anywhere */
|
2001-07-17 11:41:52 +08:00
|
|
|
/* Walk the allocated memory chain looking for orphaned memory from
|
|
|
|
previous execs */
|
|
|
|
for (_cmalloc_entry *rvc = cygheap->chain; rvc; rvc = rvc->prev)
|
|
|
|
{
|
|
|
|
cygheap_entry *ce = (cygheap_entry *) rvc->data;
|
2001-09-07 02:06:27 +08:00
|
|
|
if (!rvc->ptr || rvc->b >= NBUCKETS || ce->type <= HEAP_1_START)
|
2001-07-17 11:41:52 +08:00
|
|
|
continue;
|
|
|
|
else if (ce->type < HEAP_1_MAX)
|
|
|
|
ce->type += HEAP_1_MAX; /* Mark for freeing after next exec */
|
|
|
|
else
|
|
|
|
_cfree (ce); /* Marked by parent for freeing in child */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
#define pagetrunc(x) ((void *) (((DWORD) (x)) & ~(4096 - 1)))
|
2000-09-30 12:54:11 +08:00
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
static void *__stdcall
|
|
|
|
_csbrk (int sbs)
|
|
|
|
{
|
2002-08-07 09:20:59 +08:00
|
|
|
void *prebrk = cygheap_max;
|
|
|
|
void *prebrka = pagetrunc (prebrk);
|
|
|
|
(char *) cygheap_max += sbs;
|
|
|
|
if (!sbs || (prebrk != prebrka && prebrka == pagetrunc (cygheap_max)))
|
|
|
|
/* nothing to do */;
|
|
|
|
else if (!VirtualAlloc (prebrk, (DWORD) sbs, MEM_COMMIT, PAGE_READWRITE))
|
2002-10-09 12:08:05 +08:00
|
|
|
{
|
|
|
|
malloc_printf ("couldn't commit memory for cygwin heap, %E");
|
|
|
|
__seterrno ();
|
|
|
|
(char *) cygheap_max -= sbs;
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-09-30 12:54:11 +08:00
|
|
|
|
2002-07-14 04:00:27 +08:00
|
|
|
return prebrk;
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
|
|
|
|
2000-11-14 13:53:32 +08:00
|
|
|
extern "C" void __stdcall
|
|
|
|
cygheap_init ()
|
|
|
|
{
|
2002-02-23 03:33:41 +08:00
|
|
|
new_muto (cygheap_protect);
|
2002-08-07 09:20:59 +08:00
|
|
|
if (!cygheap)
|
|
|
|
{
|
|
|
|
init_cheap ();
|
|
|
|
(void) _csbrk (sizeof (*cygheap));
|
|
|
|
}
|
2001-04-19 05:10:15 +08:00
|
|
|
if (!cygheap->fdtab)
|
|
|
|
cygheap->fdtab.init ();
|
2000-11-14 13:53:32 +08:00
|
|
|
}
|
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
/* Copyright (C) 1997, 2000 DJ Delorie */
|
|
|
|
|
2000-10-17 07:55:58 +08:00
|
|
|
static void *_cmalloc (int size) __attribute ((regparm(1)));
|
|
|
|
static void *__stdcall _crealloc (void *ptr, int size) __attribute ((regparm(2)));
|
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
static void *__stdcall
|
|
|
|
_cmalloc (int size)
|
|
|
|
{
|
2000-09-30 12:54:11 +08:00
|
|
|
_cmalloc_entry *rvc;
|
2001-07-19 01:05:34 +08:00
|
|
|
unsigned b, sz;
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2000-10-17 07:55:58 +08:00
|
|
|
/* Calculate "bit bucket" and size as a power of two. */
|
2001-07-19 01:05:34 +08:00
|
|
|
for (b = 3, sz = 8; sz && sz < (size + sizeof (_cmalloc_entry));
|
|
|
|
b++, sz <<= 1)
|
2000-10-17 07:55:58 +08:00
|
|
|
continue;
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2000-10-02 10:26:04 +08:00
|
|
|
cygheap_protect->acquire ();
|
2001-09-06 11:39:18 +08:00
|
|
|
if (cygheap->buckets[b])
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
2001-09-06 11:39:18 +08:00
|
|
|
rvc = (_cmalloc_entry *) cygheap->buckets[b];
|
|
|
|
cygheap->buckets[b] = rvc->ptr;
|
2000-09-30 12:54:11 +08:00
|
|
|
rvc->b = b;
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
2000-10-02 10:26:04 +08:00
|
|
|
else
|
|
|
|
{
|
2000-10-17 07:55:58 +08:00
|
|
|
size = sz + sizeof (_cmalloc_entry);
|
2000-10-02 10:26:04 +08:00
|
|
|
rvc = (_cmalloc_entry *) _csbrk (size);
|
2002-10-09 12:08:05 +08:00
|
|
|
if (!rvc)
|
|
|
|
{
|
|
|
|
cygheap_protect->release ();
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2000-10-02 10:26:04 +08:00
|
|
|
rvc->b = b;
|
2000-11-14 13:53:32 +08:00
|
|
|
rvc->prev = cygheap->chain;
|
|
|
|
cygheap->chain = rvc;
|
2000-10-02 10:26:04 +08:00
|
|
|
}
|
|
|
|
cygheap_protect->release ();
|
2000-09-30 12:54:11 +08:00
|
|
|
return rvc->data;
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __stdcall
|
|
|
|
_cfree (void *ptr)
|
|
|
|
{
|
2000-10-02 10:26:04 +08:00
|
|
|
cygheap_protect->acquire ();
|
2000-09-30 12:54:11 +08:00
|
|
|
_cmalloc_entry *rvc = to_cmalloc (ptr);
|
|
|
|
DWORD b = rvc->b;
|
2001-09-06 11:39:18 +08:00
|
|
|
rvc->ptr = cygheap->buckets[b];
|
|
|
|
cygheap->buckets[b] = (char *) rvc;
|
2000-10-02 10:26:04 +08:00
|
|
|
cygheap_protect->release ();
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
|
|
|
|
2001-05-08 23:16:49 +08:00
|
|
|
static void *__stdcall _crealloc (void *ptr, int size) __attribute__((regparm(2)));
|
2000-09-03 12:16:35 +08:00
|
|
|
static void *__stdcall
|
|
|
|
_crealloc (void *ptr, int size)
|
|
|
|
{
|
2000-09-05 11:16:28 +08:00
|
|
|
void *newptr;
|
|
|
|
if (ptr == NULL)
|
|
|
|
newptr = _cmalloc (size);
|
|
|
|
else
|
|
|
|
{
|
2000-10-17 07:55:58 +08:00
|
|
|
int oldsize = 1 << to_cmalloc (ptr)->b;
|
2000-09-05 11:16:28 +08:00
|
|
|
if (size <= oldsize)
|
|
|
|
return ptr;
|
|
|
|
newptr = _cmalloc (size);
|
|
|
|
memcpy (newptr, ptr, oldsize);
|
|
|
|
_cfree (ptr);
|
|
|
|
}
|
2000-09-03 12:16:35 +08:00
|
|
|
return newptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End Copyright (C) 1997 DJ Delorie */
|
|
|
|
|
2002-09-23 08:31:31 +08:00
|
|
|
#define sizeof_cygheap(n) ((n) + sizeof (cygheap_entry))
|
2000-09-03 12:16:35 +08:00
|
|
|
|
|
|
|
#define N ((cygheap_entry *) NULL)
|
|
|
|
#define tocygheap(s) ((cygheap_entry *) (((char *) (s)) - (int) (N->data)))
|
|
|
|
|
2000-10-17 07:55:58 +08:00
|
|
|
inline static void *
|
2000-09-03 12:16:35 +08:00
|
|
|
creturn (cygheap_types x, cygheap_entry * c, int len)
|
|
|
|
{
|
|
|
|
if (!c)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
c->type = x;
|
2001-02-02 12:32:02 +08:00
|
|
|
char *cend = ((char *) c + sizeof (*c) + len);
|
|
|
|
if (cygheap_max < cend)
|
|
|
|
cygheap_max = cend;
|
2000-10-02 10:26:04 +08:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 12:16:35 +08:00
|
|
|
return (void *) c->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void *__stdcall
|
|
|
|
cmalloc (cygheap_types x, DWORD n)
|
|
|
|
{
|
|
|
|
cygheap_entry *c;
|
2000-10-02 10:26:04 +08:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 12:16:35 +08:00
|
|
|
c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
|
|
|
|
if (!c)
|
|
|
|
system_printf ("cmalloc returned NULL");
|
|
|
|
return creturn (x, c, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void *__stdcall
|
|
|
|
crealloc (void *s, DWORD n)
|
|
|
|
{
|
2000-10-02 10:26:04 +08:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 12:16:35 +08:00
|
|
|
if (s == NULL)
|
|
|
|
return cmalloc (HEAP_STR, n); // kludge
|
|
|
|
|
|
|
|
assert (!inheap (s));
|
|
|
|
cygheap_entry *c = tocygheap (s);
|
2000-09-30 12:54:11 +08:00
|
|
|
cygheap_types t = (cygheap_types) c->type;
|
2000-09-03 12:16:35 +08:00
|
|
|
c = (cygheap_entry *) _crealloc (c, sizeof_cygheap (n));
|
|
|
|
if (!c)
|
|
|
|
system_printf ("crealloc returned NULL");
|
|
|
|
return creturn (t, c, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void __stdcall
|
|
|
|
cfree (void *s)
|
|
|
|
{
|
|
|
|
assert (!inheap (s));
|
|
|
|
(void) _cfree (tocygheap (s));
|
2000-10-02 10:26:04 +08:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
|
|
|
|
2002-06-28 04:44:27 +08:00
|
|
|
extern "C" void __stdcall
|
|
|
|
cfree_and_set (char *&s, char *what)
|
|
|
|
{
|
|
|
|
if (s && s != almost_null)
|
|
|
|
cfree (s);
|
|
|
|
s = what;
|
|
|
|
}
|
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
extern "C" void *__stdcall
|
|
|
|
ccalloc (cygheap_types x, DWORD n, DWORD size)
|
|
|
|
{
|
|
|
|
cygheap_entry *c;
|
2000-10-02 10:26:04 +08:00
|
|
|
MALLOC_CHECK;
|
2001-09-14 05:15:34 +08:00
|
|
|
n *= size;
|
|
|
|
c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
|
2000-09-03 12:16:35 +08:00
|
|
|
if (c)
|
2001-09-14 05:15:34 +08:00
|
|
|
memset (c->data, 0, n);
|
2000-09-03 12:16:35 +08:00
|
|
|
if (!c)
|
|
|
|
system_printf ("ccalloc returned NULL");
|
|
|
|
return creturn (x, c, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" char *__stdcall
|
|
|
|
cstrdup (const char *s)
|
|
|
|
{
|
2000-10-02 10:26:04 +08:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 12:16:35 +08:00
|
|
|
char *p = (char *) cmalloc (HEAP_STR, strlen (s) + 1);
|
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
strcpy (p, s);
|
2000-10-02 10:26:04 +08:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 12:16:35 +08:00
|
|
|
return p;
|
|
|
|
}
|
2000-09-27 13:12:09 +08:00
|
|
|
|
|
|
|
extern "C" char *__stdcall
|
|
|
|
cstrdup1 (const char *s)
|
|
|
|
{
|
2000-10-02 10:26:04 +08:00
|
|
|
MALLOC_CHECK;
|
2000-09-27 13:12:09 +08:00
|
|
|
char *p = (char *) cmalloc (HEAP_1_STR, strlen (s) + 1);
|
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
strcpy (p, s);
|
2000-10-02 10:26:04 +08:00
|
|
|
MALLOC_CHECK;
|
2000-09-27 13:12:09 +08:00
|
|
|
return p;
|
|
|
|
}
|
2000-11-15 08:13:09 +08:00
|
|
|
|
2001-09-10 00:52:37 +08:00
|
|
|
bool
|
|
|
|
init_cygheap::etc_changed ()
|
|
|
|
{
|
|
|
|
bool res = 0;
|
|
|
|
|
|
|
|
if (!etc_changed_h)
|
|
|
|
{
|
|
|
|
path_conv pwd ("/etc");
|
|
|
|
etc_changed_h = FindFirstChangeNotification (pwd, FALSE,
|
|
|
|
FILE_NOTIFY_CHANGE_LAST_WRITE);
|
|
|
|
if (etc_changed_h == INVALID_HANDLE_VALUE)
|
|
|
|
system_printf ("Can't open /etc for checking, %E", (char *) pwd,
|
2001-11-05 14:09:15 +08:00
|
|
|
etc_changed_h);
|
2001-09-10 00:52:37 +08:00
|
|
|
else if (!DuplicateHandle (hMainProc, etc_changed_h, hMainProc,
|
2001-11-05 14:09:15 +08:00
|
|
|
&etc_changed_h, 0, TRUE,
|
2001-09-10 00:52:37 +08:00
|
|
|
DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
|
|
|
|
{
|
|
|
|
system_printf ("Can't inherit /etc handle, %E", (char *) pwd,
|
2001-11-05 14:09:15 +08:00
|
|
|
etc_changed_h);
|
2001-09-10 00:52:37 +08:00
|
|
|
etc_changed_h = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (etc_changed_h != INVALID_HANDLE_VALUE
|
|
|
|
&& WaitForSingleObject (etc_changed_h, 0) == WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
(void) FindNextChangeNotification (etc_changed_h);
|
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-06-03 10:31:16 +08:00
|
|
|
void
|
|
|
|
cygheap_root::set (const char *posix, const char *native)
|
2000-11-15 08:13:09 +08:00
|
|
|
{
|
2001-08-07 08:01:42 +08:00
|
|
|
if (*posix == '/' && posix[1] == '\0')
|
|
|
|
{
|
|
|
|
if (m)
|
|
|
|
{
|
|
|
|
cfree (m);
|
|
|
|
m = NULL;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2001-06-03 10:31:16 +08:00
|
|
|
if (!m)
|
|
|
|
m = (struct cygheap_root_mount_info *) ccalloc (HEAP_MOUNT, 1, sizeof (*m));
|
|
|
|
strcpy (m->posix_path, posix);
|
|
|
|
m->posix_pathlen = strlen (posix);
|
|
|
|
if (m->posix_pathlen >= 1 && m->posix_path[m->posix_pathlen - 1] == '/')
|
|
|
|
m->posix_path[--m->posix_pathlen] = '\0';
|
|
|
|
|
|
|
|
strcpy (m->native_path, native);
|
|
|
|
m->native_pathlen = strlen (native);
|
|
|
|
if (m->native_pathlen >= 1 && m->native_path[m->native_pathlen - 1] == '\\')
|
|
|
|
m->native_path[--m->native_pathlen] = '\0';
|
2000-11-15 08:13:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cygheap_user::~cygheap_user ()
|
|
|
|
{
|
2001-01-17 20:47:47 +08:00
|
|
|
#if 0
|
2000-11-15 08:13:09 +08:00
|
|
|
if (pname)
|
|
|
|
cfree (pname);
|
|
|
|
if (plogsrv)
|
2002-06-13 19:35:11 +08:00
|
|
|
cfree (plogsrv - 2);
|
2000-11-15 08:13:09 +08:00
|
|
|
if (pdomain)
|
|
|
|
cfree (pdomain);
|
|
|
|
if (psid)
|
|
|
|
cfree (psid);
|
2001-01-17 20:47:47 +08:00
|
|
|
#endif
|
2000-11-15 08:13:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cygheap_user::set_name (const char *new_name)
|
|
|
|
{
|
2002-06-27 03:39:05 +08:00
|
|
|
bool allocated = !!pname;
|
2002-06-27 10:29:57 +08:00
|
|
|
|
2002-06-27 03:39:05 +08:00
|
|
|
if (allocated)
|
2002-06-27 10:29:57 +08:00
|
|
|
{
|
|
|
|
if (strcasematch (new_name, pname))
|
|
|
|
return;
|
|
|
|
cfree (pname);
|
|
|
|
}
|
2002-06-27 03:39:05 +08:00
|
|
|
|
2000-11-15 08:13:09 +08:00
|
|
|
pname = cstrdup (new_name ? new_name : "");
|
2002-06-27 03:39:05 +08:00
|
|
|
if (!allocated)
|
|
|
|
return; /* Initializing. Don't bother with other stuff. */
|
|
|
|
|
2002-06-28 04:44:27 +08:00
|
|
|
cfree_and_set (homedrive);
|
|
|
|
cfree_and_set (homepath);
|
|
|
|
cfree_and_set (plogsrv);
|
|
|
|
cfree_and_set (pdomain);
|
2002-06-29 10:36:08 +08:00
|
|
|
cfree_and_set (pwinname);
|
2000-11-15 08:13:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
cygheap_user::set_sid (PSID new_sid)
|
|
|
|
{
|
2002-06-15 02:01:21 +08:00
|
|
|
if (new_sid)
|
2000-11-15 08:13:09 +08:00
|
|
|
{
|
2002-06-15 02:01:21 +08:00
|
|
|
if (!psid)
|
2002-06-21 13:01:17 +08:00
|
|
|
psid = cmalloc (HEAP_STR, MAX_SID_LEN);
|
2000-11-15 08:13:09 +08:00
|
|
|
if (psid)
|
2002-06-15 02:01:21 +08:00
|
|
|
return CopySid (MAX_SID_LEN, psid, new_sid);
|
2000-11-15 08:13:09 +08:00
|
|
|
}
|
2002-06-15 02:01:21 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
cygheap_user::set_orig_sid ()
|
|
|
|
{
|
|
|
|
if (psid)
|
2000-11-15 08:13:09 +08:00
|
|
|
{
|
2002-06-15 02:01:21 +08:00
|
|
|
if (!orig_psid) orig_psid = cmalloc (HEAP_STR, MAX_SID_LEN);
|
|
|
|
if (orig_psid)
|
|
|
|
return CopySid (MAX_SID_LEN, orig_psid, psid);
|
2000-11-15 08:13:09 +08:00
|
|
|
}
|
2002-06-15 02:01:21 +08:00
|
|
|
return FALSE;
|
2000-11-15 08:13:09 +08:00
|
|
|
}
|