2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <sys/types.h>
37 using namespace gnash
;
42 LogFile
& dbglogfile
= LogFile::getDefaultInstance();
43 static CRcInitFile
& crcfile
= CRcInitFile::getDefaultInstance();
47 // GNASH_REPORT_FUNCTION;
52 // GNASH_REPORT_FUNCTION;
58 // GNASH_REPORT_FUNCTION;
59 log_unimpl("%s", __PRETTY_FUNCTION__
);
64 Proc::getDefaultInstance()
66 // GNASH_REPORT_FUNCTION;
73 Proc::startCGI(const string
&filespec
, boost::uint16_t port
)
75 // GNASH_REPORT_FUNCTION;
76 return startCGI(filespec
, false, port
);
80 Proc::startCGI(const string
&filespec
)
82 // GNASH_REPORT_FUNCTION;
83 return startCGI(filespec
, false, 0);
87 Proc::startCGI(const string
&filespec
, bool outflag
)
90 return startCGI(filespec
, outflag
, 0);
94 Proc::startCGI(const string
&filespec
, bool outflag
, boost::uint16_t port
)
96 // GNASH_REPORT_FUNCTION;
97 struct stat procstats
;
101 _output
[filespec
] = outflag
;
104 if (crcfile
.getCgiRoot().size() > 0) {
105 path
= crcfile
.getCgiRoot().c_str();
106 log_debug (_("Document Root for CGI files is: %s"), path
);
108 // Yes, I know this is a hack.
109 path
= "/var/www/html/cygnal/cgi-bin";
111 // string path = filespec;
115 log_debug("Starting \"%s\"", path
);
117 // See if the file actually exists, otherwise we can't spawn it
118 if (stat(path
.c_str(), &procstats
) == -1) {
119 log_error("Invalid filespec for CGI: \"%s\"", path
);
120 // perror(filespec.c_str());
124 // setup a command line. By default, argv[0] is the name of the process
125 cmd_line
[0] = new char(filespec
.size()+1);
126 strcpy(cmd_line
[0], filespec
.c_str());
128 // If the parent has verbosity on, chances are the child should too.
129 // if (dbglogfile.getVerbosity() > 0) {
130 cmd_line
[1] = new char(3);
131 strcpy(cmd_line
[1], "-n");
132 cmd_line
[2] = new char(4);
133 strcpy(cmd_line
[2], "-vv");
137 // When running multiple cgis, we prefer to specify the port it's using.
139 cmd_line
[3] = new char(3);
140 strcpy(cmd_line
[3], "-p");
141 cmd_line
[4] = new char(10);
142 sprintf(cmd_line
[4], "%d", port
);
147 // fork ourselves silly
150 // boost::mutex::scoped_lock lock(_mutex);
152 // childpid is a positive integer, if we are the parent, and fork() worked
154 _pids
[filespec
] = childpid
;
158 // childpid is -1, if the fork failed, so print out an error message
159 if (childpid
== -1) {
161 perror(filespec
.c_str());
165 // If we are the child, exec the new process, then go away
167 // Turn off all output, if requested
168 if (outflag
== false) {
170 open("/dev/null", O_WRONLY
);
172 open("/dev/null", O_WRONLY
);
174 // Start the desired executable
175 execv(path
.c_str(), cmd_line
);
176 perror(path
.c_str());
184 Proc::findCGI(const string
&filespec
)
186 // GNASH_REPORT_FUNCTION;
187 log_debug("Finding \"%s\"", filespec
);
188 boost::mutex::scoped_lock
lock(_mutex
);
190 return _pids
[filespec
];
196 // GNASH_REPORT_FUNCTION;
197 log_unimpl("%s", __PRETTY_FUNCTION__
);
198 boost::mutex::scoped_lock
lock(_mutex
);
204 Proc::stopCGI(const string
&filespec
)
206 // GNASH_REPORT_FUNCTION;
207 log_debug("Stopping \"%s\"", filespec
);
209 boost::mutex::scoped_lock
lock(_mutex
);
210 pid_t pid
= _pids
[filespec
];
212 if (kill (pid
, SIGQUIT
) == -1) {
220 Proc::setOutput(const string
&filespec
, bool outflag
)
222 // GNASH_REPORT_FUNCTION;
223 boost::mutex::scoped_lock
lock(_mutex
);
224 _output
[filespec
] = outflag
;
230 Proc::getOutput(const string
&filespec
)
232 // GNASH_REPORT_FUNCTION;
233 boost::mutex::scoped_lock
lock(_mutex
);
235 return _output
[filespec
];
239 Proc::connectCGI (const string
&host
, boost::uint16_t port
)
241 // GNASH_REPORT_FUNCTION;
242 return createClient(host
, port
);
246 } // end of cygnal namespace