| 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"cxx . . $ `<file>/HercStudio/HerculesStudio.h`/ |
| 26 | #include "SystemUtils.h"cxx . . $ `<file>/HercUtilities/SystemUtils.h`/ |
| 27 | #include "NamedPipe.h"cxx . . $ `<file>/HercStudio/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_SETScxx . . $ `HercStudio/NamedPipe.cpp:45:9`! 20 |
| 46 | |
| 47 | NamedPipecxx . . $ NamedPipe# NamedPipecxx . . $ NamedPipe#::sInstancecxx . . $ NamedPipe#sInstance.; |
| 48 | |
| 49 | NamedPipecxx . . $ NamedPipe#::NamedPipecxx . . $ NamedPipe#NamedPipe(49f6e7a06ebc5aa8).() |
| 50 | { |
| 51 | |
| 52 | } |
| 53 | |
| 54 | NamedPipecxx . . $ NamedPipe#& NamedPipecxx . . $ NamedPipe#::getInstancecxx . . $ NamedPipe#getInstance(464fad7f4e589b3).() |
| 55 | { |
| 56 | return sInstancecxx . . $ NamedPipe#sInstance.; |
| 57 | } |
| 58 | |
| 59 | NamedPipecxx . . $ NamedPipe#::~cxx . . $ NamedPipe#`~NamedPipe`(49f6e7a06ebc5aa8).NamedPipecxx . . $ NamedPipe#() |
| 60 | { |
| 61 | |
| 62 | } |
| 63 | |
| 64 | int NamedPipecxx . . $ NamedPipe#::recovercxx . . $ NamedPipe#recover(b126dc7c1de90089).() |
| 65 | { |
| 66 | #ifndef hFramework |
| 67 | int set = 0; |
| 68 | mRecoverycxx . . $ NamedPipe#mRecovery. = false; |
| 69 | |
| 70 | char * home = getenvcxx . . $ getenv(a8b8e32347182212).("HOME"); |
| 71 | stdcxx . . $ std/::stringcxx . . $ std/string# hercstudioDircxx . . $ std/__cxx11/basic_string#basic_string(3bc0f77cdfbaa67f). = homecxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).; |
| 72 | hercstudioDir +=cxx . . $ std/__cxx11/basic_string#`operator+=`(978ca1133b5f6e74). "/.hercstudio"; |
| 73 | stdcxx . . $ std/::stringcxx . . $ std/string# setPathcxx . . $ std/__cxx11/basic_string#basic_string(49f6e7a06ebc5aa8).; |
| 74 | |
| 75 | int rc = mkdir(hercstudioDir.c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).(), 0777); |
| 76 | if (rc<0 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EEXISTcxx . . $ `<external>/8987895939074593727/errno-base.h:21:9`!) |
| 77 | { |
| 78 | perror(hercstudioDir.c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).()); |
| 79 | hOutDebugcxx . . $ `HercStudio/HerculesStudio.h:49:9`!(1,"rc=" << errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | for (int pipeSet=0; pipeSet<MAXIMUM_SETScxx . . $ `HercStudio/NamedPipe.cpp:45:9`!; pipeSet++) |
| 84 | { |
| 85 | stdcxx . . $ std/::stringstreamcxx . . $ std/stringstream# sscxx . . $ std/__cxx11/basic_stringstream#basic_stringstream(49f6e7a06ebc5aa8).; |
| 86 | ss <<cxx . . $ std/`operator<<`(88df5bef833f7f83). hercstudioDir <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "/run" <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). pipeSet; |
| 87 | setPath =cxx . . $ std/__cxx11/basic_string#`operator=`(706c69e8c742c906). ss.strcxx . . $ std/__cxx11/basic_stringstream#str(d33e1a6fd36255f7).(); |
| 88 | |
| 89 | if (!SystemUtilscxx . . $ SystemUtils#::fileExistscxx . . $ SystemUtils#fileExists(7c222eea44a24d43).(setPath)) |
| 90 | { |
| 91 | hOutDebugcxx . . $ `HercStudio/HerculesStudio.h:49:9`!(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 | stdcxx . . $ std/::stringcxx . . $ std/string# pidPath = setPathcxx . . $ std/__cxx11/basic_string#basic_string(3bc0f77cdfbaa67f). +cxx . . $ std/operator+(40fc1db9226233c6). "/pid"; |
| 99 | FILEcxx . . $ FILE# * fstr = fopen(pidPath.c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).(),"r"); |
| 100 | if (fstr != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) |
| 101 | { |
| 102 | rc = fscanf(fstr,"%d",&hercPid); |
| 103 | if (fscanf(fstr,"%d",&studioPid)) |
| 104 | fclose(fstr); |
| 105 | } |
| 106 | |
| 107 | if (!SystemUtilscxx . . $ SystemUtils#::processIsRunningcxx . . $ SystemUtils#processIsRunning(83c267b20bac841a).(studioPid)) |
| 108 | { |
| 109 | if (!SystemUtilscxx . . $ SystemUtils#::processIsRunningcxx . . $ SystemUtils#processIsRunning(83c267b20bac841a).(hercPid)) |
| 110 | { |
| 111 | delDircxx . . $ NamedPipe#delDir(9b8588921b9c6c2f).(setPath); |
| 112 | set = pipeSet; |
| 113 | break; // set was cleared - use it |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | // recovery - takeover |
| 118 | hOutDebugcxx . . $ `HercStudio/HerculesStudio.h:49:9`!(1,"Hercules process " << hercPid << " is running"); |
| 119 | mRecoverycxx . . $ NamedPipe#mRecovery. = true; |
| 120 | mSetPathcxx . . $ NamedPipe#mSetPath. =cxx . . $ std/__cxx11/basic_string#`operator=`(c0b47be1fddc0e75). setPath; |
| 121 | mHerculesPidcxx . . $ NamedPipe#mHerculesPid. = hercPid; |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | hOutDebugcxx . . $ `HercStudio/HerculesStudio.h:49:9`!(1, "going to work with set " << set); |
| 128 | mSetPathcxx . . $ NamedPipe#mSetPath. =cxx . . $ std/__cxx11/basic_string#`operator=`(c0b47be1fddc0e75). setPath; |
| 129 | if (!mRecoverycxx . . $ NamedPipe#mRecovery. && mkdir(mSetPathcxx . . $ NamedPipe#mSetPath..c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).(), 0777)<0) |
| 130 | { |
| 131 | perror(hercstudioDir.c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).()); |
| 132 | hOutDebugcxx . . $ `HercStudio/HerculesStudio.h:49:9`!(2,"rc=" << errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 133 | return -1; |
| 134 | } |
| 135 | |
| 136 | generatePidcxx . . $ NamedPipe#generatePid(3e454b8ccf442868).(getpid(),0); |
| 137 | umask(0); |
| 138 | |
| 139 | mFifo0cxx . . $ NamedPipe#mFifo0. =cxx . . $ std/__cxx11/basic_string#`operator=`(706c69e8c742c906). mSetPathcxx . . $ NamedPipe#mSetPath. +cxx . . $ std/operator+(40fc1db9226233c6). "/fifo0"; |
| 140 | hOutDebugcxx . . $ `HercStudio/HerculesStudio.h:49:9`!(5,"fifo0=" << mFifo0); |
| 141 | if (!mRecoverycxx . . $ NamedPipe#mRecovery. && mknod(mFifo0cxx . . $ NamedPipe#mFifo0..c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).(), S_IFIFOcxx . . $ `<external>/531885356569766532/stat.h:110:11`!|0666, 0) != 0) |
| 142 | { |
| 143 | perror("fifo0"); |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | mFifo1cxx . . $ NamedPipe#mFifo1. =cxx . . $ std/__cxx11/basic_string#`operator=`(706c69e8c742c906). mSetPathcxx . . $ NamedPipe#mSetPath. +cxx . . $ std/operator+(40fc1db9226233c6). "/fifo1"; |
| 148 | if (!mRecoverycxx . . $ NamedPipe#mRecovery. && mknod(mFifo1cxx . . $ NamedPipe#mFifo1..c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).(), S_IFIFOcxx . . $ `<external>/531885356569766532/stat.h:110:11`!|0666, 0) != 0) |
| 149 | { |
| 150 | perror("fifo1");// TODO: merge with NamedPipe (same function!) |
| 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | mFifo2cxx . . $ NamedPipe#mFifo2. =cxx . . $ std/__cxx11/basic_string#`operator=`(706c69e8c742c906). mSetPathcxx . . $ NamedPipe#mSetPath. +cxx . . $ std/operator+(40fc1db9226233c6). "/fifo2"; |
| 155 | if (!mRecoverycxx . . $ NamedPipe#mRecovery. && mknod(mFifo2cxx . . $ NamedPipe#mFifo2..c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).(), S_IFIFOcxx . . $ `<external>/531885356569766532/stat.h:110:11`!|0666, 0) != 0) |
| 156 | { |
| 157 | perror("fifo2"); |
| 158 | return -1; |
| 159 | } |
| 160 | return mRecoverycxx . . $ NamedPipe#mRecovery. ? 1 : 0; |
| 161 | #else |
| 162 | return 0; |
| 163 | #endif |
| 164 | } |
| 165 | |
| 166 | int NamedPipecxx . . $ NamedPipe#::delDircxx . . $ NamedPipe#delDir(9b8588921b9c6c2f).(stdcxx . . $ std/::stringcxx . . $ std/string#& dir) |
| 167 | { |
| 168 | stdcxx . . $ std/::stringcxx . . $ std/string# command = "rm -rf "cxx . . $ std/__cxx11/basic_string#basic_string(3bc0f77cdfbaa67f). +cxx . . $ std/operator+(42c652dfebe55ad7). dir; |
| 169 | return systemcxx . . $ system(5c4306d8553b3696).(command.c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).()); // TODO: temp |
| 170 | } |
| 171 | |
| 172 | void NamedPipecxx . . $ NamedPipe#::generatePidcxx . . $ NamedPipe#generatePid(3e454b8ccf442868).(int studioPid, int herculesPid) |
| 173 | { |
| 174 | stdcxx . . $ std/::stringcxx . . $ std/string# pidPath = mSetPathcxx . . $ std/__cxx11/basic_string#basic_string(3bc0f77cdfbaa67f). +cxx . . $ std/operator+(40fc1db9226233c6). "/pid"; |
| 175 | hOutDebugcxx . . $ `HercStudio/HerculesStudio.h:49:9`!(1,"generatePid " << studioPid << "," << herculesPid); |
| 176 | FILEcxx . . $ FILE# * fPid = fopen(pidPath.c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).(),"w"); |
| 177 | if (fPid == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) |
| 178 | { |
| 179 | perror(pidPath.c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).()); |
| 180 | return; |
| 181 | } |
| 182 | fprintf(fPid,"%d\n",herculesPid); |
| 183 | fprintf(fPid,"%d\n",studioPid); |
| 184 | fclose(fPid); |
| 185 | } |
| 186 | |
| 187 | int NamedPipecxx . . $ NamedPipe#::getHerculesPidcxx . . $ NamedPipe#getHerculesPid(b126dc7c1de90089).() |
| 188 | { |
| 189 | return mHerculesPidcxx . . $ NamedPipe#mHerculesPid.; |
| 190 | } |
| 191 | |
| 192 | #ifndef hFramework |
| 193 | FILEcxx . . $ FILE# * NamedPipecxx . . $ NamedPipe#::getHerculesStdincxx . . $ NamedPipe#getHerculesStdin(3637adb0f06093ef).() |
| 194 | { |
| 195 | int fd = open(mFifo0cxx . . $ NamedPipe#mFifo0..c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).(), |
| 196 | O_RDONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:43:9`! | O_NONBLOCKcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:62:10`!); |
| 197 | if (fd == -1) perror("fifo0 fd"); |
| 198 | return fdopen(fd,"r"); |
| 199 | } |
| 200 | #endif |
| 201 | FILEcxx . . $ FILE# * NamedPipecxx . . $ NamedPipe#::getHerculesCommandsFilecxx . . $ NamedPipe#getHerculesCommandsFile(3637adb0f06093ef).() |
| 202 | { |
| 203 | if (mStdinOutputcxx . . $ NamedPipe#mStdinOutput. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) |
| 204 | { |
| 205 | if (mStdinOutputcxx . . $ NamedPipe#mStdinOutput. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) |
| 206 | mStdinOutputcxx . . $ NamedPipe#mStdinOutput. = fopen(mFifo0cxx . . $ NamedPipe#mFifo0..c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).(),"w"); |
| 207 | } |
| 208 | return mStdinOutputcxx . . $ NamedPipe#mStdinOutput.; |
| 209 | } |
| 210 | |
| 211 | FILEcxx . . $ FILE# * NamedPipecxx . . $ NamedPipe#::getHerculesStdoutcxx . . $ NamedPipe#getHerculesStdout(3637adb0f06093ef).() |
| 212 | { |
| 213 | if (mStdoutcxx . . $ NamedPipe#mStdout. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) |
| 214 | { |
| 215 | if (mStdoutcxx . . $ NamedPipe#mStdout. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) |
| 216 | mStdoutcxx . . $ NamedPipe#mStdout. = fopen(mFifo1cxx . . $ NamedPipe#mFifo1..c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).(),"w"); |
| 217 | } |
| 218 | return mStdoutcxx . . $ NamedPipe#mStdout.; |
| 219 | } |
| 220 | |
| 221 | QFilecxx . . $ QFile#& NamedPipecxx . . $ NamedPipe#::getHerculesLogfilecxx . . $ NamedPipe#getHerculesLogfile(b409ea4a0265da19).() |
| 222 | { |
| 223 | if (mStdoutInputcxx . . $ NamedPipe#mStdoutInput. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) |
| 224 | { |
| 225 | mStdoutInputcxx . . $ NamedPipe#mStdoutInput. = new QFilecxx . . $ QFile#(mFifo1cxx . . $ QString#QString(85c52e162fed56f9)..c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).()); |
| 226 | mStdoutInputcxx . . $ NamedPipe#mStdoutInput.->opencxx . . $ QFile#open(dca164408be960c8).(QIODevicecxx . . $ QFlags#QFlags(9b289cee16747614).::ReadOnlycxx . . $ QIODevice#ReadOnly.); |
| 227 | } |
| 228 | return *mStdoutInputcxx . . $ NamedPipe#mStdoutInput.; |
| 229 | } |
| 230 | |
| 231 | FILEcxx . . $ FILE# * NamedPipecxx . . $ NamedPipe#::getHerculesStderrcxx . . $ NamedPipe#getHerculesStderr(3637adb0f06093ef).() |
| 232 | { |
| 233 | if (mStderrcxx . . $ NamedPipe#mStderr. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) |
| 234 | { |
| 235 | mStderrcxx . . $ NamedPipe#mStderr. = fopen(mFifo2cxx . . $ NamedPipe#mFifo2..c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).(),"w"); |
| 236 | } |
| 237 | return mStderrcxx . . $ NamedPipe#mStderr.; |
| 238 | } |
| 239 | |
| 240 | QFilecxx . . $ QFile#& NamedPipecxx . . $ NamedPipe#::getHerculesStatuscxx . . $ NamedPipe#getHerculesStatus(b409ea4a0265da19).() |
| 241 | { |
| 242 | if (mStderrInputcxx . . $ NamedPipe#mStderrInput. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) |
| 243 | { |
| 244 | mStderrInputcxx . . $ NamedPipe#mStderrInput. = new QFilecxx . . $ QFile#(mFifo2cxx . . $ QString#QString(85c52e162fed56f9)..c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).()); |
| 245 | mStderrInputcxx . . $ NamedPipe#mStderrInput.->opencxx . . $ QFile#open(dca164408be960c8).(QIODevicecxx . . $ QFlags#QFlags(9b289cee16747614).::ReadOnlycxx . . $ QIODevice#ReadOnly.); |
| 246 | } |
| 247 | |
| 248 | return *mStderrInputcxx . . $ NamedPipe#mStderrInput.; |
| 249 | } |
| 250 | |
| 251 | stdcxx . . $ std/::stringcxx . . $ std/string# NamedPipecxx . . $ NamedPipe#::getHerculesCommandlinecxx . . $ NamedPipe#getHerculesCommandline(efd28698952f529a).() |
| 252 | { |
| 253 | stdcxx . . $ std/::stringstreamcxx . . $ std/stringstream# procPathcxx . . $ std/__cxx11/basic_stringstream#basic_stringstream(49f6e7a06ebc5aa8).; |
| 254 | procPath <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "/proc/" <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). mHerculesPidcxx . . $ NamedPipe#mHerculesPid. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "/cmdline"; |
| 255 | FILEcxx . . $ FILE# * f = fopen(procPath.strcxx . . $ std/__cxx11/basic_stringstream#str(d33e1a6fd36255f7).().c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).(),"r"); |
| 256 | if (f==NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) returncxx . . $ std/__cxx11/basic_string#basic_string(3bc0f77cdfbaa67f). ""cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).; |
| 257 | char buff[255]; |
| 258 | int len = fread(buff,sizeof(char), 255,f); |
| 259 | if (len == -1) |
| 260 | { |
| 261 | fclose(f); |
| 262 | returncxx . . $ std/__cxx11/basic_string#basic_string(3bc0f77cdfbaa67f). ""cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).; |
| 263 | } |
| 264 | for (int i=0; i<len; i++) |
| 265 | if (buff[i] == '\0') buff[i] = ' '; |
| 266 | stdcxx . . $ std/::stringcxx . . $ std/string# retcxx . . $ std/__cxx11/basic_string#basic_string(3bc0f77cdfbaa67f). = buffcxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).; |
| 267 | return retcxx . . $ std/__cxx11/basic_string#basic_string(3bc0f77cdfbaa67f).; |
| 268 | } |