4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "threadfork.h"
26 #include <sys/types.h>
35 ThreadFork::ThreadFork()
39 pthread_mutexattr_t attr;
40 pthread_mutexattr_init(&attr);
41 pthread_mutex_init(&start_lock, &attr);
45 arguments = new char*[MAX_ARGS];
52 ThreadFork::~ThreadFork()
57 // if(stdin_fd) fclose(stdin_fd);
62 // pthread_join(tid, 0);
64 for(int i = 0; i < total_arguments; i++)
65 delete [] arguments[i];
67 pthread_mutex_destroy(&start_lock);
71 void* ThreadFork::entrypoint(void *ptr)
73 ThreadFork *threadfork = (ThreadFork*)ptr;
78 void ThreadFork::start_command(const char *command_line, int pipe_stdin)
80 this->command_line = command_line;
81 this->pipe_stdin = pipe_stdin;
84 pthread_attr_init(&attr);
87 stdin_fd = popen(command_line, "w");
89 // pthread_mutex_lock(&start_lock);
92 // pthread_create(&tid, &attr, entrypoint, this);
93 // pthread_mutex_lock(&start_lock);
94 // pthread_mutex_unlock(&start_lock);
98 void ThreadFork::run()
103 char argument[BCTEXTLEN];
104 char command_line[BCTEXTLEN];
107 strcpy(command_line, this->command_line);
111 // Set up arguments for exec
114 while(*ptr != ' ' && *ptr != 0)
116 *path_ptr++ = *ptr++;
120 arguments[total_arguments] = new char[strlen(path) + 1];
121 strcpy(arguments[total_arguments], path);
124 arguments[total_arguments] = 0;
129 argument_ptr = argument;
130 while(*ptr != ' ' && *ptr != 0)
132 *argument_ptr++ = *ptr++;
136 arguments[total_arguments] = new char[strlen(argument) + 1];
137 strcpy(arguments[total_arguments], argument);
139 arguments[total_arguments] = 0;
154 stdin_fd = fdopen(filedes[1], "w");
157 pthread_mutex_unlock(&start_lock);
166 if(pipe_stdin) dup2(filedes[0], fileno(stdin));
170 execvp(path, arguments);
180 pthread_mutex_unlock(&start_lock);
183 if(waitpid(pid, &return_value, WUNTRACED) == -1)
185 perror("ThreadFork::run: waitpid");
193 FILE* ThreadFork::get_stdin()