blob: 469a66a9156f706c569305d27ac0da6245b63d15 [file] [log] [blame]
// Copyright 2023 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef NINJA_STDIO_REDIRECTION_H_
#define NINJA_STDIO_REDIRECTION_H_
#include <stdio.h>
#include <string>
#include "ipc_handle.h"
/// Multiple classes related to redirecting stdio streams.
/// A class used to manage redirection of stdin/stdout/stderr
/// between two processes, using an IpcHandle for communication.
///
/// IMPORTANT: THIS DOES NOT WORK ON WINDOWS. Due to Win32
/// technical limitations (i.e. DuplicateHandle() will always
/// fail to duplicate console handlers from another process,
/// which happens during ReceiveStandardDescriptors).
///
/// Usage is:
/// 1) Create instance, passing a reference to the handle.
///
/// 2) On the client, call SendStandardDescriptors() to send
/// standard descriptors to the server. This does not change
/// the current process.
///
/// 3) On the server, call ReceiveStandardDescriptors() to
/// receive them from the handle, and modify the process'
/// current standard descriptors to use them.
///
/// 4) The destructor will restore the previous standard
/// descriptors, in the case where ReceiveStandardDescriptors()
/// was called.
///
class StdioRedirector {
public:
StdioRedirector(IpcHandle& connection);
~StdioRedirector();
bool SendStandardDescriptors(std::string* err);
bool ReceiveStandardDescriptors(std::string* err);
protected:
IpcHandle& connection_;
IpcHandle old_stdin_;
IpcHandle old_stdout_;
IpcHandle old_stderr_;
};
#endif // NINJA_STDIO_REDIRECTION_H_