Debian Codegraph @

HercStudio/NamedPipe.cpp

raw symbols SCIP
1 /*
2 * File: NamedPipe.cpp
3 *
4 * Author: Jacob Dekel
5 * Created on: Aug 7, 2009
6 *
7 * Copyright (c) 2009-2013 Jacob Dekel
8 * $Id$
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25 #include "HerculesStudio.h"
26 #include "SystemUtils.h"
27 #include "NamedPipe.h"
28
29 #include <QMutexLocker>
30
31 #include <string>
32 #include <sstream>
33 #include <iostream>
34
35 #include <cstdio>
36 #include <fcntl.h>
37 #include <cstdlib>
38 #include <cerrno>
39 #ifndef hFramework
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <unistd.h>
43 #endif
44
45 #define MAXIMUM_SETS 20
46
47 NamedPipe NamedPipe::sInstance;
48
49 NamedPipe::NamedPipe()
50 {
51
52 }
53
54 NamedPipe& NamedPipe::getInstance()
55 {
56 return sInstance;
57 }
58
59 NamedPipe::~NamedPipe()
60 {
61
62 }
63
64 int NamedPipe::recover()
65 {
66 #ifndef hFramework
67 int set = 0;
68 mRecovery = false;
69
70 char * home = getenv("HOME");
71 std::string hercstudioDir = home;
72 hercstudioDir += "/.hercstudio";
73 std::string setPath;
74
75 int rc = mkdir(hercstudioDir.c_str(), 0777);
76 if (rc<0 && errno != EEXIST)
77 {
78 perror(hercstudioDir.c_str());
79 hOutDebug(1,"rc=" << errno);
80 return -1;
81 }
82
83 for (int pipeSet=0; pipeSet<MAXIMUM_SETS; pipeSet++)
84 {
85 std::stringstream ss;
86 ss << hercstudioDir << "/run" << pipeSet;
87 setPath = ss.str();
88
89 if (!SystemUtils::fileExists(setPath))
90 {
91 hOutDebug(2,"setPath=" << setPath << ",run=" << pipeSet);
92 set = pipeSet;
93 break; // set does not exist - we can use this number
94 }
95
96 int hercPid = 0;
97 int studioPid = 0;
98 std::string pidPath = setPath + "/pid";
99 FILE * fstr = fopen(pidPath.c_str(),"r");
100 if (fstr != NULL)
101 {
102 rc = fscanf(fstr,"%d",&hercPid);
103 if (fscanf(fstr,"%d",&studioPid))
104 fclose(fstr);
105 }
106
107 if (!SystemUtils::processIsRunning(studioPid))
108 {
109 if (!SystemUtils::processIsRunning(hercPid))
110 {
111 delDir(setPath);
112 set = pipeSet;
113 break; // set was cleared - use it
114 }
115 else
116 {
117 // recovery - takeover
118 hOutDebug(1,"Hercules process " << hercPid << " is running");
119 mRecovery = true;
120 mSetPath = setPath;
121 mHerculesPid = hercPid;
122 break;
123 }
124 }
125 }
126
127 hOutDebug(1, "going to work with set " << set);
128 mSetPath = setPath;
129 if (!mRecovery && mkdir(mSetPath.c_str(), 0777)<0)
130 {
131 perror(hercstudioDir.c_str());
132 hOutDebug(2,"rc=" << errno);
133 return -1;
134 }
135
136 generatePid(getpid(),0);
137 umask(0);
138
139 mFifo0 = mSetPath + "/fifo0";
140 hOutDebug(5,"fifo0=" << mFifo0);
141 if (!mRecovery && mknod(mFifo0.c_str(), S_IFIFO|0666, 0) != 0)
142 {
143 perror("fifo0");
144 return -1;
145 }
146
147 mFifo1 = mSetPath + "/fifo1";
148 if (!mRecovery && mknod(mFifo1.c_str(), S_IFIFO|0666, 0) != 0)
149 {
150 perror("fifo1");// TODO: merge with NamedPipe (same function!)
151 return -1;
152 }
153
154 mFifo2 = mSetPath + "/fifo2";
155 if (!mRecovery && mknod(mFifo2.c_str(), S_IFIFO|0666, 0) != 0)
156 {
157 perror("fifo2");
158 return -1;
159 }
160 return mRecovery ? 1 : 0;
161 #else
162 return 0;
163 #endif
164 }
165
166 int NamedPipe::delDir(std::string& dir)
167 {
168 std::string command = "rm -rf " + dir;
169 return system(command.c_str()); // TODO: temp
170 }
171
172 void NamedPipe::generatePid(int studioPid, int herculesPid)
173 {
174 std::string pidPath = mSetPath + "/pid";
175 hOutDebug(1,"generatePid " << studioPid << "," << herculesPid);
176 FILE * fPid = fopen(pidPath.c_str(),"w");
177 if (fPid == NULL)
178 {
179 perror(pidPath.c_str());
180 return;
181 }
182 fprintf(fPid,"%d\n",herculesPid);
183 fprintf(fPid,"%d\n",studioPid);
184 fclose(fPid);
185 }
186
187 int NamedPipe::getHerculesPid()
188 {
189 return mHerculesPid;
190 }
191
192 #ifndef hFramework
193 FILE * NamedPipe::getHerculesStdin()
194 {
195 int fd = open(mFifo0.c_str(),
196 O_RDONLY | O_NONBLOCK);
197 if (fd == -1) perror("fifo0 fd");
198 return fdopen(fd,"r");
199 }
200 #endif
201 FILE * NamedPipe::getHerculesCommandsFile()
202 {
203 if (mStdinOutput == NULL)
204 {
205 if (mStdinOutput == NULL)
206 mStdinOutput = fopen(mFifo0.c_str(),"w");
207 }
208 return mStdinOutput;
209 }
210
211 FILE * NamedPipe::getHerculesStdout()
212 {
213 if (mStdout == NULL)
214 {
215 if (mStdout == NULL)
216 mStdout = fopen(mFifo1.c_str(),"w");
217 }
218 return mStdout;
219 }
220
221 QFile& NamedPipe::getHerculesLogfile()
222 {
223 if (mStdoutInput == NULL)
224 {
225 mStdoutInput = new QFile(mFifo1.c_str());
226 mStdoutInput->open(QIODevice::ReadOnly);
227 }
228 return *mStdoutInput;
229 }
230
231 FILE * NamedPipe::getHerculesStderr()
232 {
233 if (mStderr == NULL)
234 {
235 mStderr = fopen(mFifo2.c_str(),"w");
236 }
237 return mStderr;
238 }
239
240 QFile& NamedPipe::getHerculesStatus()
241 {
242 if (mStderrInput == NULL)
243 {
244 mStderrInput = new QFile(mFifo2.c_str());
245 mStderrInput->open(QIODevice::ReadOnly);
246 }
247
248 return *mStderrInput;
249 }
250
251 std::string NamedPipe::getHerculesCommandline()
252 {
253 std::stringstream procPath;
254 procPath << "/proc/" << mHerculesPid << "/cmdline";
255 FILE * f = fopen(procPath.str().c_str(),"r");
256 if (f==NULL) return "";
257 char buff[255];
258 int len = fread(buff,sizeof(char), 255,f);
259 if (len == -1)
260 {
261 fclose(f);
262 return "";
263 }
264 for (int i=0; i<len; i++)
265 if (buff[i] == '\0') buff[i] = ' ';
266 std::string ret = buff;
267 return ret;
268 }