Re: sync(3)

2004-10-27 Thread Christopher Faylor
On Wed, Oct 27, 2004 at 04:36:17AM +0200, Reini Urban wrote:
Why is this a bad idea?

It's a very limited implementation of what sync is supposed to do but
maybe it's better than nothing.

A slightly more robust method would be to implement an internal cygwin
signal which could be sent to every cygwin process telling it to run
code like the below.

Of course, that isn't foolproof either since it doesn't affect
non-cygwin processes.

Do you have an assignment with Red Hat?  If so, I'll check this in.

cgf

2004-10-27  Reini Urban  [EMAIL PROTECTED]

   * syscalls.cc (sync): Implement it via cygheap-fdtab and 
   FlushFileBuffers. Better than a noop.

Index: syscalls.cc
===
RCS file: /cvs/src/src/winsup/cygwin/syscalls.cc,v
retrieving revision 1.345
diff -u -b -r1.345 syscalls.cc
--- syscalls.cc3 Sep 2004 01:53:12 -   1.345
+++ syscalls.cc27 Oct 2004 02:30:01 -
@@ -1082,6 +1082,24 @@
 extern C void
 sync ()
 {
+  int err = 0;
+  cygheap-fdtab.lock ();
+
+  fhandler_base *fh;
+  for (int i = 0; i  (int) cygheap-fdtab.size; i++)
+if ((fh = cygheap-fdtab[i]) != NULL)
+  {
+#ifdef DEBUGGING
+  debug_printf (syncing fd %d, i);
+#endif
+  if (FlushFileBuffers (fh-get_handle ()) == 0)
+{
+  __seterrno ();
+  err++;
+}
+  }
+  cygheap-fdtab.unlock ();
+  return err ? 1 : 0;
 }
 
 /* Cygwin internal */


Re: sync(3)

2004-10-27 Thread Reini Urban
Christopher Faylor schrieb:
On Wed, Oct 27, 2004 at 04:36:17AM +0200, Reini Urban wrote:
Why is this a bad idea?
It's a very limited implementation of what sync is supposed to do but
maybe it's better than nothing.
A slightly more robust method would be to implement an internal cygwin
signal which could be sent to every cygwin process telling it to run
code like the below.
A signal looks better.
Maybe just to its master process, and all its subprocesses and threads?
I didn't check what fd's are actually stored in this heap.
On linux it should just ensure to flush the master inode block.
(makes sense for ext2 probably).
For NTFS Volumes this code would be okay as I read at MSDN.
Wonder what FAT will do. postgresql luckily uses fsync().
Maybe I should check which of our apps actually use sync(3).
exim out of my head.
Of course, that isn't foolproof either since it doesn't affect
non-cygwin processes.
Warning: I didn't test it. Maybe it would be better if someone else 
would copy-paste it from close_all_files() as I did and test it.
I just had no guts yet to build my own dll.

Do you have an assignment with Red Hat?  If so, I'll check this in.
Not yet, but I'll send a letter this week.
2004-10-27  Reini Urban  [EMAIL PROTECTED]
	* syscalls.cc (sync): Implement it via cygheap-fdtab and 
	FlushFileBuffers. Better than a noop.

Index: syscalls.cc
===
RCS file: /cvs/src/src/winsup/cygwin/syscalls.cc,v
retrieving revision 1.345
diff -u -b -r1.345 syscalls.cc
--- syscalls.cc 3 Sep 2004 01:53:12 -   1.345
+++ syscalls.cc 27 Oct 2004 02:30:01 -
@@ -1082,6 +1082,24 @@
extern C void
sync ()
{
+  int err = 0;
+  cygheap-fdtab.lock ();
+
+  fhandler_base *fh;
+  for (int i = 0; i  (int) cygheap-fdtab.size; i++)
+if ((fh = cygheap-fdtab[i]) != NULL)
+  {
+#ifdef DEBUGGING
+   debug_printf (syncing fd %d, i);
+#endif
+   if (FlushFileBuffers (fh-get_handle ()) == 0)
+ {
+   __seterrno ();
+   err++;
+ }
+  }
+  cygheap-fdtab.unlock ();
+  return err ? 1 : 0;
}
/* Cygwin internal */
--
Reini Urban
http://xarch.tu-graz.ac.at/home/rurban/


[Patch] Deimpersonate while accessing HKLM

2004-10-27 Thread Pierre A. Humblet
This patch should fix the chdir problem reported by Jason Tishler.
It deimpersonates while reading the mounts and cygdrive in HKLM.

For ease of initialization, the unused cygheap-user tokens are
now set to NO_IMPERSONATION (instead of INVALID_HANDLE_VALUE), 
which is #defined as NULL.
If the argument of cygwin_set_impersonation_token() is
INVALID_HANDLE_VALUE, it is changed to NO_IMPERSONATION.

Pierre

2004-10-28  Pierre Humblet [EMAIL PROTECTED]

* path.cc (mount_info::from_registry): Deimpersonate while
accessing HKLM.
(mount_info::read_cygdrive_info_from_registry): Ditto.
* cygheap.h: Define NO_IMPERSONATION.
(cygheap_user::issetuid): Replace INVALID_HANDLE_VALUE by 
NO_IMPERSONATION.
(cygheap_user::has_impersonation_tokens): Ditto.
(cygheap_user::close_impersonation_tokens): Ditto.
* uinfo.cc (uinfo_init): Ditto.
* syscalls.cc (seteuid32): Ditto.
* security.cc (set_impersonation_token): Ditto.

   Index: path.cc
===
RCS file: /cvs/src/src/winsup/cygwin/path.cc,v
retrieving revision 1.324
diff -u -p -r1.324 path.cc
--- path.cc 6 Oct 2004 01:33:39 -   1.324
+++ path.cc 27 Oct 2004 23:43:26 -
@@ -1802,11 +1802,13 @@ mount_info::from_registry ()
   read_mounts (r);

   /* Then read mounts from system-wide mount table. */
+  cygheap-user.deimpersonate ();
   reg_key r1 (HKEY_LOCAL_MACHINE, KEY_READ, SOFTWARE,
  CYGWIN_INFO_CYGNUS_REGISTRY_NAME, CYGWIN_REGNAME,
  CYGWIN_INFO_CYGWIN_MOUNT_REGISTRY_NAME,
  NULL);
   read_mounts (r1);
+  cygheap-user.reimpersonate ();
 }

 /* add_reg_mount: Add mount item to registry.  Return zero on success,
@@ -1922,16 +1924,16 @@ mount_info::read_cygdrive_info_from_regi
 {
   /* reg_key for user path prefix in HKEY_CURRENT_USER. */
   reg_key r;
-
+  /* First read cygdrive from user's registry. */
   if (r.get_string (CYGWIN_INFO_CYGDRIVE_PREFIX, cygdrive, sizeof (cygdrive), ) != 
0)
 {
-  /* Didn't find the user path prefix so check the system path prefix. */
-
-  /* reg_key for system path prefix in HKEY_LOCAL_MACHINE.  */
+  /* Then read cygdrive from system-wide registry. */
+  cygheap-user.deimpersonate ();
   reg_key r2 (HKEY_LOCAL_MACHINE, KEY_READ, SOFTWARE,
 CYGWIN_INFO_CYGNUS_REGISTRY_NAME, CYGWIN_REGNAME,
 CYGWIN_INFO_CYGWIN_MOUNT_REGISTRY_NAME,
 NULL);
+  cygheap-user.reimpersonate ();

   if (r2.get_string (CYGWIN_INFO_CYGDRIVE_PREFIX, cygdrive,
  sizeof (cygdrive), ))
Index: cygheap.h
===
RCS file: /cvs/src/src/winsup/cygwin/cygheap.h,v
retrieving revision 1.90
diff -b -u -p -r1.90 cygheap.h
--- cygheap.h   7 Oct 2004 21:28:57 -   1.90
+++ cygheap.h   28 Oct 2004 00:09:18 -
@@ -169,7 +169,8 @@ public:
   PSID sid () { return effec_cygsid; }
   PSID saved_sid () { return saved_cygsid; }
   const char *ontherange (homebodies what, struct passwd * = NULL);
-  bool issetuid () const { return current_token != INVALID_HANDLE_VALUE; }
+#define NO_IMPERSONATION NULL
+  bool issetuid () const { return current_token != NO_IMPERSONATION; }
   HANDLE token () { return current_token; }
   void deimpersonate ()
   {
@@ -183,26 +184,26 @@ public:
   system_printf (ImpersonateLoggedOnUser: %E);
   }
   bool has_impersonation_tokens ()
-{ return external_token != INVALID_HANDLE_VALUE
-|| internal_token != INVALID_HANDLE_VALUE
-|| current_token != INVALID_HANDLE_VALUE; }
+{ return external_token != NO_IMPERSONATION
+|| internal_token != NO_IMPERSONATION
+|| current_token != NO_IMPERSONATION; }
   void close_impersonation_tokens ()
   {
-if (current_token != INVALID_HANDLE_VALUE)
+if (current_token != NO_IMPERSONATION)
   {
if( current_token != external_token  current_token != internal_token)
  CloseHandle (current_token);
-   current_token = INVALID_HANDLE_VALUE;
+   current_token = NO_IMPERSONATION;
   }
-if (external_token != INVALID_HANDLE_VALUE)
+if (external_token != NO_IMPERSONATION)
   {
CloseHandle (external_token);
-   external_token = INVALID_HANDLE_VALUE;
+   external_token = NO_IMPERSONATION;
   }
-if (internal_token != INVALID_HANDLE_VALUE)
+if (internal_token != NO_IMPERSONATION)
   {
CloseHandle (internal_token);
-   internal_token = INVALID_HANDLE_VALUE;
+   internal_token = NO_IMPERSONATION;
   }
   }
   const char *cygheap_user::test_uid (char *, const char *, size_t)
Index: uinfo.cc
===
RCS file: /cvs/src/src/winsup/cygwin/uinfo.cc,v
retrieving revision 1.131
diff -b -u -p -r1.131 uinfo.cc
--- uinfo.cc7 Oct 2004 21:28:57 -   1.131
+++ uinfo.cc28 Oct