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