haiku/src/tools/fs_shell/external_commands_unix.cpp
Ingo Weinhold 17ebe2b021 Reimplemented the communication with the FS shell for Unixish systems (now
including also Haiku) using FIFOs instead of Unix sockets. Advantages:
* Multiple FS shells can run concurrently, since they no longer need a unique
  address.
* Killing/aborting the build_haiku_image script will automatically tear down
  the FS shell as well, so there shouldn't be any stray FS shell processes
  anymore. Hopefully also fixes #5498.

So far only tested under Haiku.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40097 a95241bf-73f2-0310-859d-f6bbb57e9c96
2011-01-04 01:06:56 +00:00

76 lines
1.3 KiB
C++

/*
* Copyright 2005-2010, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License.
*/
#include "external_commands.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
static FILE*
get_input()
{
static FILE* sInput = fdopen(3, "r");
return sInput;
}
static FILE*
get_output()
{
static FILE* sOutput = fdopen(4, "w");
return sOutput;
}
bool
FSShell::get_external_command(char* buffer, int size)
{
// get the input stream
FILE* in = get_input();
if (in == NULL) {
fprintf(stderr, "Error: Failed to open command input: %s\n",
strerror(errno));
return false;
}
while (true) {
// read a command line
if (fgets(buffer, size, in) != NULL)
return true;
// when interrupted, try again
if (errno != EINTR)
return false;
}
}
void
FSShell::reply_to_external_command(int result)
{
// get the output stream
FILE* out = get_output();
if (out == NULL) {
fprintf(stderr, "Error: Failed to open command output: %s\n",
strerror(errno));
return;
}
if (fprintf(out, "%d\n", result) < 0 || fflush(out) == EOF) {
fprintf(stderr, "Error: Failed to write command reply to output reply: "
"%s\n", strerror(errno));
}
}
void
FSShell::external_command_cleanup()
{
// The file will be closed automatically when the team exits.
}