Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.0 KiB

  1. /******************************************************************************\
  2. * This is a part of the Microsoft Source Code Samples.
  3. * Copyright 1992 - 1997 Microsoft Corporation.
  4. * All rights reserved.
  5. * This source code is only intended as a supplement to
  6. * Microsoft Development Tools and/or WinHelp documentation.
  7. * See these sources for detailed information regarding the
  8. * Microsoft samples programs.
  9. \******************************************************************************/
  10. /*++
  11. Copyright 1992 - 1997 Microsoft Corporation
  12. Module Name:
  13. SrvChild.c
  14. Abstract:
  15. The server component of Remote. It spawns a child process
  16. and redirects the stdin/stdout/stderr of child to itself.
  17. Waits for connections from clients - passing the
  18. output of child process to client and the input from clients
  19. to child process.
  20. Author:
  21. Rajivendra Nath 2-Jan-1992
  22. Dave Hart 30 May 1997 split from Server.c
  23. Environment:
  24. Console App. User mode.
  25. Revision History:
  26. --*/
  27. #include <precomp.h>
  28. #include "Remote.h"
  29. #include "Server.h"
  30. VOID
  31. FASTCALL
  32. StartChildOutPipeRead(
  33. VOID
  34. )
  35. {
  36. ReadChildOverlapped.OffsetHigh =
  37. ReadChildOverlapped.Offset = 0;
  38. if ( ! ReadFileEx(
  39. hReadChildOutput,
  40. ReadChildBuffer,
  41. sizeof(ReadChildBuffer) - 1, // allow for null term
  42. &ReadChildOverlapped,
  43. ReadChildOutputCompleted
  44. )) {
  45. if (INVALID_HANDLE_VALUE != hWriteChildStdIn) {
  46. CancelIo( hWriteChildStdIn );
  47. CloseHandle( hWriteChildStdIn );
  48. hWriteChildStdIn = INVALID_HANDLE_VALUE;
  49. }
  50. }
  51. }
  52. VOID
  53. WINAPI
  54. ReadChildOutputCompleted(
  55. DWORD dwError,
  56. DWORD cbRead,
  57. LPOVERLAPPED lpO
  58. )
  59. {
  60. UNREFERENCED_PARAMETER(lpO);
  61. //
  62. // We can get called after hWriteTempFile
  63. // is closed after the child has exited.
  64. //
  65. if (! dwError &&
  66. INVALID_HANDLE_VALUE != hWriteTempFile) {
  67. //
  68. // Start a write to the temp file.
  69. //
  70. ReadChildOverlapped.OffsetHigh = 0;
  71. ReadChildOverlapped.Offset = dwWriteFilePointer;
  72. if ( ! WriteFileEx(
  73. hWriteTempFile,
  74. ReadChildBuffer,
  75. cbRead,
  76. &ReadChildOverlapped,
  77. WriteTempFileCompleted
  78. )) {
  79. dwError = GetLastError();
  80. if (ERROR_DISK_FULL == dwError) {
  81. printf("Remote: disk full writing temp file %s, exiting\n", SaveFileName);
  82. if (INVALID_HANDLE_VALUE != hWriteChildStdIn) {
  83. CancelIo( hWriteChildStdIn );
  84. CloseHandle( hWriteChildStdIn );
  85. hWriteChildStdIn = INVALID_HANDLE_VALUE;
  86. }
  87. } else {
  88. ErrorExit("WriteFileEx for temp file failed.");
  89. }
  90. }
  91. }
  92. }
  93. VOID
  94. WINAPI
  95. WriteTempFileCompleted(
  96. DWORD dwError,
  97. DWORD cbWritten,
  98. LPOVERLAPPED lpO
  99. )
  100. {
  101. UNREFERENCED_PARAMETER(lpO);
  102. if (dwError) {
  103. if (ERROR_DISK_FULL == dwError) {
  104. printf("Remote: disk full writing temp file %s, exiting\n", SaveFileName);
  105. if (INVALID_HANDLE_VALUE != hWriteChildStdIn) {
  106. CancelIo( hWriteChildStdIn );
  107. CloseHandle( hWriteChildStdIn );
  108. hWriteChildStdIn = INVALID_HANDLE_VALUE;
  109. }
  110. return;
  111. } else {
  112. SetLastError(dwError);
  113. ErrorExit("WriteTempFileCompleted may need work");
  114. }
  115. }
  116. dwWriteFilePointer += cbWritten;
  117. TRACE(CHILD, ("Wrote %d bytes to temp file\n", cbWritten));
  118. StartServerToClientFlow();
  119. //
  120. // Start another read against the child input.
  121. //
  122. StartChildOutPipeRead();
  123. }