Fixed external command related code. Increased the command line length to

100 KB.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20929 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold
2007-04-30 17:02:13 +00:00
parent aeb215184f
commit 6c19139d0e
3 changed files with 35 additions and 20 deletions
+18 -14
View File
@@ -11,8 +11,10 @@
#include "external_commands.h" #include "external_commands.h"
#include "fs_shell_command_beos.h" #include "fs_shell_command_beos.h"
static port_id sReplyPort = -1; static port_id sReplyPort = -1;
static port_id static port_id
get_command_port() get_command_port()
{ {
@@ -29,52 +31,54 @@ get_command_port()
bool bool
get_external_command(char *input, int len) FSShell::get_external_command(char *input, int len)
{ {
// get/create the port // get/create the port
port_id port = get_command_port(); port_id port = get_command_port();
if (port < 0) { if (port < 0) {
fprintf(stderr, "Failed to create command port: %s\n", strerror(port)); fprintf(stderr, "Error: Failed to create command port: %s\n",
strerror(port));
return false; return false;
} }
while (true) { while (true) {
// read a message // read a message
external_command_message message; char _message[sizeof(external_command_message) + kMaxCommandLength];
external_command_message* message = (external_command_message*)_message;
ssize_t bytesRead; ssize_t bytesRead;
do { do {
int32 code; int32 code;
bytesRead = read_port(port, &code, &message, sizeof(message)); bytesRead = read_port(port, &code, message, sizeof(_message));
} while (bytesRead == B_INTERRUPTED); } while (bytesRead == B_INTERRUPTED);
if (bytesRead < 0) { if (bytesRead < 0) {
fprintf(stderr, "Reading from port failed: %s\n", fprintf(stderr, "Error: Reading from port failed: %s\n",
strerror(bytesRead)); strerror(bytesRead));
return false; return false;
} }
// get the len of the command // get the len of the command
int commandLen = (char*)&message + bytesRead - message.command; int commandLen = _message + bytesRead - message->command;
if (commandLen <= 1) { if (commandLen <= 1) {
fprintf(stderr, "No command given.\n"); fprintf(stderr, "Error: No command given.\n");
continue; continue;
} }
if (commandLen > len) { if (commandLen > len) {
fprintf(stderr, "Command too long. Ignored.\n"); fprintf(stderr, "Error: Command too long. Ignored.\n");
continue; continue;
} }
// copy the command // copy the command
memcpy(input, message.command, commandLen); memcpy(input, message->command, commandLen);
input[len - 1] = '\0'; // always NULL-terminate input[len - 1] = '\0'; // always NULL-terminate
sReplyPort = message.reply_port; sReplyPort = message->reply_port;
return true; return true;
} }
} }
void void
reply_to_external_command(int result) FSShell::reply_to_external_command(int result)
{ {
if (sReplyPort >= 0) { if (sReplyPort >= 0) {
// prepare the message // prepare the message
@@ -89,15 +93,15 @@ reply_to_external_command(int result)
sReplyPort = -1; sReplyPort = -1;
if (error != B_OK) { if (error != B_OK) {
fprintf(stderr, "Failed to send command result to reply port: %s\n", fprintf(stderr, "Error: Failed to send command result to reply "
strerror(error)); "port: %s\n", strerror(error));
} }
} }
} }
void void
external_command_cleanup() FSShell::external_command_cleanup()
{ {
// The port will be deleted automatically when the team exits. // The port will be deleted automatically when the team exits.
} }
+12 -4
View File
@@ -16,8 +16,15 @@
bool bool
send_external_command(const char *command, int *result) send_external_command(const char *command, int *result)
{ {
external_command_message message; int commandLen = strlen(command);
strncpy(message.command, command, sizeof(message.command)); if (commandLen > kMaxCommandLength) {
fprintf(stderr, "Error: Command line too long.\n");
return false;
}
char _message[sizeof(external_command_message) + kMaxCommandLength];
external_command_message* message = (external_command_message*)_message;
strcpy(message->command, command);
// find the command port // find the command port
port_id commandPort = find_port(kFSShellCommandPort); port_id commandPort = find_port(kFSShellCommandPort);
@@ -33,12 +40,13 @@ send_external_command(const char *command, int *result)
strerror(replyPort)); strerror(replyPort));
return false; return false;
} }
message.reply_port = replyPort; message->reply_port = replyPort;
// send the command message // send the command message
status_t error; status_t error;
do { do {
error = write_port(commandPort, 0, &message, sizeof(message)); error = write_port(commandPort, 0, message,
sizeof(external_command_message) + commandLen);
} while (error == B_INTERRUPTED); } while (error == B_INTERRUPTED);
if (error != B_OK) { if (error != B_OK) {
+5 -2
View File
@@ -5,12 +5,15 @@
#ifndef FS_SHELL_COMMAND_BEOS_H #ifndef FS_SHELL_COMMAND_BEOS_H
#define FS_SHELL_COMMAND_BEOS_H #define FS_SHELL_COMMAND_BEOS_H
#include <OS.h>
static const char *kFSShellCommandPort = "fs shell command port"; static const char *kFSShellCommandPort = "fs shell command port";
static const int kMaxCommandLength = 102400;
struct external_command_message { struct external_command_message {
port_id reply_port; port_id reply_port;
char command[20480]; char command[1];
// TODO: Increase command size and transmit only as much as necessary.
}; };
struct external_command_reply { struct external_command_reply {