TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tdelauncher.cpp 44KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  1. /*
  2. This file is part of the KDE libraries
  3. Copyright (c) 1999 Waldo Bastian <bastian@kde.org>
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License version 2 as published by the Free Software Foundation.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public License
  12. along with this library; see the file COPYING.LIB. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  14. Boston, MA 02110-1301, USA.
  15. */
  16. #ifdef HAVE_CONFIG_H
  17. #include <config.h>
  18. #endif
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <errno.h>
  23. #include <signal.h>
  24. #include <sys/time.h>
  25. #include <tqfile.h>
  26. #include <tdeconfig.h>
  27. #include <kdebug.h>
  28. #include <klibloader.h>
  29. #include <tdelocale.h>
  30. #include <tdeprotocolmanager.h>
  31. #include <kprotocolinfo.h>
  32. #include <krun.h>
  33. #include <kstandarddirs.h>
  34. #include <tdetempfile.h>
  35. #include <kurl.h>
  36. #if defined Q_WS_X11 && ! defined K_WS_QTONLY
  37. #include <tdestartupinfo.h> // schroder
  38. #endif
  39. #include "tdeio/global.h"
  40. #include "tdeio/connection.h"
  41. #include "tdeio/slaveinterface.h"
  42. #include "tdelauncher.h"
  43. #include "tdelauncher_cmds.h"
  44. //#if defined Q_WS_X11 && ! defined K_WS_QTONLY
  45. #ifdef Q_WS_X11
  46. //#undef K_WS_QTONLY
  47. #include <X11/Xlib.h> // schroder
  48. #endif
  49. // Dispose slaves after being idle for SLAVE_MAX_IDLE seconds
  50. #define SLAVE_MAX_IDLE 30
  51. using namespace TDEIO;
  52. template class TQPtrList<TDELaunchRequest>;
  53. template class TQPtrList<IdleSlave>;
  54. IdleSlave::IdleSlave(TDESocket *socket)
  55. {
  56. mConn.init(socket);
  57. mConn.connect(this, TQT_SLOT(gotInput()));
  58. mConn.send( CMD_SLAVE_STATUS );
  59. mPid = 0;
  60. mBirthDate = time(0);
  61. mOnHold = false;
  62. }
  63. void
  64. IdleSlave::gotInput()
  65. {
  66. int cmd;
  67. TQByteArray data;
  68. if (mConn.read( &cmd, data) == -1)
  69. {
  70. // Communication problem with slave.
  71. kdError(7016) << "SlavePool: No communication with slave." << endl;
  72. delete this;
  73. }
  74. else if (cmd == MSG_SLAVE_ACK)
  75. {
  76. delete this;
  77. }
  78. else if (cmd != MSG_SLAVE_STATUS)
  79. {
  80. kdError(7016) << "SlavePool: Unexpected data from slave." << endl;
  81. delete this;
  82. }
  83. else
  84. {
  85. TQDataStream stream( data, IO_ReadOnly );
  86. pid_t pid;
  87. TQCString protocol;
  88. TQString host;
  89. TQ_INT8 b;
  90. stream >> pid >> protocol >> host >> b;
  91. // Overload with (bool) onHold, (KURL) url.
  92. if (!stream.atEnd())
  93. {
  94. KURL url;
  95. stream >> url;
  96. mOnHold = true;
  97. mUrl = url;
  98. }
  99. mPid = pid;
  100. mConnected = (b != 0);
  101. mProtocol = protocol;
  102. mHost = host;
  103. emit statusUpdate(this);
  104. }
  105. }
  106. void
  107. IdleSlave::connect(const TQString &app_socket)
  108. {
  109. TQByteArray data;
  110. TQDataStream stream( data, IO_WriteOnly);
  111. stream << app_socket;
  112. mConn.send( CMD_SLAVE_CONNECT, data );
  113. // Timeout!
  114. }
  115. void
  116. IdleSlave::reparseConfiguration()
  117. {
  118. mConn.send( CMD_REPARSECONFIGURATION );
  119. }
  120. bool
  121. IdleSlave::match(const TQString &protocol, const TQString &host, bool connected)
  122. {
  123. if (mOnHold) return false;
  124. if (protocol != mProtocol) return false;
  125. if (host.isEmpty()) return true;
  126. if (host != mHost) return false;
  127. if (!connected) return true;
  128. if (!mConnected) return false;
  129. return true;
  130. }
  131. bool
  132. IdleSlave::onHold(const KURL &url)
  133. {
  134. if (!mOnHold) return false;
  135. return (url == mUrl);
  136. }
  137. int
  138. IdleSlave::age(time_t now)
  139. {
  140. return (int) difftime(now, mBirthDate);
  141. }
  142. TDELauncher::TDELauncher(int _tdeinitSocket, bool new_startup)
  143. // : TDEApplication( false, false ), // No Styles, No GUI
  144. : TDEApplication( false, true ), // TQClipboard tries to construct a QWidget so a GUI is technically needed, even though it is not used
  145. DCOPObject("tdelauncher"),
  146. tdeinitSocket(_tdeinitSocket), mAutoStart( new_startup ),
  147. dontBlockReading(false), newStartup( new_startup )
  148. {
  149. #ifdef Q_WS_X11
  150. mCached_dpy = NULL;
  151. #endif
  152. connect(&mAutoTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(slotAutoStart()));
  153. requestList.setAutoDelete(true);
  154. mSlaveWaitRequest.setAutoDelete(true);
  155. dcopClient()->setNotifications( true );
  156. connect(dcopClient(), TQT_SIGNAL( applicationRegistered( const TQCString &)),
  157. this, TQT_SLOT( slotAppRegistered( const TQCString &)));
  158. dcopClient()->connectDCOPSignal( "DCOPServer", "", "terminateKDE()",
  159. objId(), "terminateKDE()", false );
  160. TQString prefix = locateLocal("socket", "tdelauncher");
  161. KTempFile domainname(prefix, TQString::fromLatin1(".slave-socket"));
  162. if (domainname.status() != 0)
  163. {
  164. // Sever error!
  165. tqDebug("TDELauncher: Fatal error, can't create tempfile!");
  166. ::exit(1);
  167. }
  168. mPoolSocketName = domainname.name();
  169. #ifdef __CYGWIN__
  170. domainname.close();
  171. domainname.unlink();
  172. #endif
  173. mPoolSocket = new TDEServerSocket(static_cast<const char*>(TQFile::encodeName(mPoolSocketName)));
  174. connect(mPoolSocket, TQT_SIGNAL(accepted( TDESocket *)),
  175. TQT_SLOT(acceptSlave(TDESocket *)));
  176. connect(&mTimer, TQT_SIGNAL(timeout()), TQT_SLOT(idleTimeout()));
  177. tdeinitNotifier = new TQSocketNotifier(tdeinitSocket, TQSocketNotifier::Read);
  178. connect(tdeinitNotifier, TQT_SIGNAL( activated( int )),
  179. this, TQT_SLOT( slotKDEInitData( int )));
  180. tdeinitNotifier->setEnabled( true );
  181. lastRequest = 0;
  182. bProcessingQueue = false;
  183. mSlaveDebug = getenv("TDE_SLAVE_DEBUG_WAIT");
  184. if (!mSlaveDebug.isEmpty())
  185. {
  186. tqWarning("Klauncher running in slave-debug mode for slaves of protocol '%s'", mSlaveDebug.data());
  187. }
  188. mSlaveValgrind = getenv("TDE_SLAVE_VALGRIND");
  189. if (!mSlaveValgrind.isEmpty())
  190. {
  191. mSlaveValgrindSkin = getenv("TDE_SLAVE_VALGRIND_SKIN");
  192. tqWarning("Klauncher running slaves through valgrind for slaves of protocol '%s'", mSlaveValgrind.data());
  193. }
  194. tdelauncher_header request_header;
  195. request_header.cmd = LAUNCHER_OK;
  196. request_header.arg_length = 0;
  197. write(tdeinitSocket, &request_header, sizeof(request_header));
  198. }
  199. TDELauncher::~TDELauncher()
  200. {
  201. close();
  202. }
  203. void TDELauncher::close()
  204. {
  205. if (!mPoolSocketName.isEmpty())
  206. {
  207. TQCString filename = TQFile::encodeName(mPoolSocketName);
  208. unlink(filename.data());
  209. }
  210. #if defined Q_WS_X11 && ! defined K_WS_QTONLY
  211. //#ifdef Q_WS_X11
  212. if( mCached_dpy != NULL )
  213. XCloseDisplay( mCached_dpy );
  214. #endif
  215. }
  216. void
  217. TDELauncher::destruct(int exit_code)
  218. {
  219. if (kapp) ((TDELauncher*)kapp)->close();
  220. // We don't delete kapp here, that's intentional.
  221. ::exit(exit_code);
  222. }
  223. bool
  224. TDELauncher::process(const TQCString &fun, const TQByteArray &data,
  225. TQCString &replyType, TQByteArray &replyData)
  226. {
  227. if ((fun == "exec_blind(TQCString,TQValueList<TQCString>)")
  228. || (fun == "exec_blind(TQCString,TQValueList<TQCString>,TQValueList<TQCString>,TQCString)"))
  229. {
  230. TQDataStream stream(data, IO_ReadOnly);
  231. replyType = "void";
  232. TQCString name;
  233. TQValueList<TQCString> arg_list;
  234. TQCString startup_id = "0";
  235. TQValueList<TQCString> envs;
  236. stream >> name >> arg_list;
  237. if( fun == "exec_blind(TQCString,TQValueList<TQCString>,TQValueList<TQCString>,TQCString)" )
  238. stream >> envs >> startup_id;
  239. kdDebug(7016) << "TDELauncher: Got exec_blind('" << name << "', ...)" << endl;
  240. exec_blind( name, arg_list, envs, startup_id);
  241. return true;
  242. }
  243. if ((fun == "start_service_by_name(TQString,TQStringList)") ||
  244. (fun == "start_service_by_desktop_path(TQString,TQStringList)")||
  245. (fun == "start_service_by_desktop_name(TQString,TQStringList)")||
  246. (fun == "tdeinit_exec(TQString,TQStringList)") ||
  247. (fun == "tdeinit_exec_wait(TQString,TQStringList)") ||
  248. (fun == "start_service_by_name(TQString,TQStringList,TQValueList<TQCString>,TQCString)") ||
  249. (fun == "start_service_by_desktop_path(TQString,TQStringList,TQValueList<TQCString>,TQCString)")||
  250. (fun == "start_service_by_desktop_name(TQString,TQStringList,TQValueList<TQCString>,TQCString)") ||
  251. (fun == "start_service_by_name(TQString,TQStringList,TQValueList<TQCString>,TQCString,bool)") ||
  252. (fun == "start_service_by_desktop_path(TQString,TQStringList,TQValueList<TQCString>,TQCString,bool)")||
  253. (fun == "start_service_by_desktop_name(TQString,TQStringList,TQValueList<TQCString>,TQCString,bool)") ||
  254. (fun == "tdeinit_exec(TQString,TQStringList,TQValueList<TQCString>)") ||
  255. (fun == "tdeinit_exec_wait(TQString,TQStringList,TQValueList<TQCString>)") ||
  256. (fun == "tdeinit_exec(TQString,TQStringList,TQValueList<TQCString>,TQCString)") ||
  257. (fun == "tdeinit_exec_wait(TQString,TQStringList,TQValueList<TQCString>,TQCString)"))
  258. {
  259. TQDataStream stream(data, IO_ReadOnly);
  260. bool bNoWait = false;
  261. TQString serviceName;
  262. TQStringList urls;
  263. TQValueList<TQCString> envs;
  264. TQCString startup_id = "";
  265. DCOPresult.result = -1;
  266. DCOPresult.dcopName = 0;
  267. DCOPresult.error = TQString::null;
  268. DCOPresult.pid = 0;
  269. stream >> serviceName >> urls;
  270. if ((fun == "start_service_by_name(TQString,TQStringList,TQValueList<TQCString>,TQCString,bool)") ||
  271. (fun == "start_service_by_desktop_path(TQString,TQStringList,TQValueList<TQCString>,TQCString,bool)")||
  272. (fun == "start_service_by_desktop_name(TQString,TQStringList,TQValueList<TQCString>,TQCString,bool)"))
  273. stream >> envs >> startup_id >> bNoWait;
  274. else if ((fun == "start_service_by_name(TQString,TQStringList,TQValueList<TQCString>,TQCString)") ||
  275. (fun == "start_service_by_desktop_path(TQString,TQStringList,TQValueList<TQCString>,TQCString)")||
  276. (fun == "start_service_by_desktop_name(TQString,TQStringList,TQValueList<TQCString>,TQCString)"))
  277. stream >> envs >> startup_id;
  278. else if ((fun == "tdeinit_exec(TQString,TQStringList,TQValueList<TQCString>)") ||
  279. (fun == "tdeinit_exec_wait(TQString,TQStringList,TQValueList<TQCString>)"))
  280. stream >> envs;
  281. else if ((fun == "tdeinit_exec(TQString,TQStringList,TQValueList<TQCString>,TQCString)") ||
  282. (fun == "tdeinit_exec_wait(TQString,TQStringList,TQValueList<TQCString>,TQCString)"))
  283. stream >> envs >> startup_id;
  284. bool finished;
  285. if (strncmp(fun, "start_service_by_name(", 22) == 0)
  286. {
  287. kdDebug(7016) << "TDELauncher: Got start_service_by_name('" << serviceName << "', ...)" << endl;
  288. finished = start_service_by_name(serviceName, urls, envs, startup_id, bNoWait);
  289. }
  290. else if (strncmp(fun, "start_service_by_desktop_path(", 30) == 0)
  291. {
  292. kdDebug(7016) << "TDELauncher: Got start_service_by_desktop_path('" << serviceName << "', ...)" << endl;
  293. finished = start_service_by_desktop_path(serviceName, urls, envs, startup_id, bNoWait);
  294. }
  295. else if (strncmp(fun, "start_service_by_desktop_name(", 30) == 0)
  296. {
  297. kdDebug(7016) << "TDELauncher: Got start_service_by_desktop_name('" << serviceName << "', ...)" << endl;
  298. finished = start_service_by_desktop_name(serviceName, urls, envs, startup_id, bNoWait );
  299. }
  300. else if ((fun == "tdeinit_exec(TQString,TQStringList)")
  301. || (fun == "tdeinit_exec(TQString,TQStringList,TQValueList<TQCString>)")
  302. || (fun == "tdeinit_exec(TQString,TQStringList,TQValueList<TQCString>,TQCString)"))
  303. {
  304. kdDebug(7016) << "TDELauncher: Got tdeinit_exec('" << serviceName << "', ...)" << endl;
  305. finished = tdeinit_exec(serviceName, urls, envs, startup_id, false);
  306. }
  307. else
  308. {
  309. kdDebug(7016) << "TDELauncher: Got tdeinit_exec_wait('" << serviceName << "', ...)" << endl;
  310. finished = tdeinit_exec(serviceName, urls, envs, startup_id, true);
  311. }
  312. if (!finished)
  313. {
  314. replyType = "serviceResult";
  315. TQDataStream stream2(replyData, IO_WriteOnly);
  316. stream2 << DCOPresult.result << DCOPresult.dcopName << DCOPresult.error << DCOPresult.pid;
  317. }
  318. return true;
  319. }
  320. else if (fun == "requestSlave(TQString,TQString,TQString)")
  321. {
  322. TQDataStream stream(data, IO_ReadOnly);
  323. TQString protocol;
  324. TQString host;
  325. TQString app_socket;
  326. stream >> protocol >> host >> app_socket;
  327. replyType = "TQString";
  328. TQString error;
  329. pid_t pid = requestSlave(protocol, host, app_socket, error);
  330. TQDataStream stream2(replyData, IO_WriteOnly);
  331. stream2 << pid << error;
  332. return true;
  333. }
  334. else if (fun == "requestHoldSlave(KURL,TQString)")
  335. {
  336. TQDataStream stream(data, IO_ReadOnly);
  337. KURL url;
  338. TQString app_socket;
  339. stream >> url >> app_socket;
  340. replyType = "pid_t";
  341. pid_t pid = requestHoldSlave(url, app_socket);
  342. TQDataStream stream2(replyData, IO_WriteOnly);
  343. stream2 << pid;
  344. return true;
  345. }
  346. else if (fun == "waitForSlave(pid_t)")
  347. {
  348. TQDataStream stream(data, IO_ReadOnly);
  349. pid_t pid;
  350. stream >> pid;
  351. waitForSlave(pid);
  352. replyType = "void";
  353. return true;
  354. }
  355. else if (fun == "setLaunchEnv(TQCString,TQCString)")
  356. {
  357. TQDataStream stream(data, IO_ReadOnly);
  358. TQCString name;
  359. TQCString value;
  360. stream >> name >> value;
  361. setLaunchEnv(name, value);
  362. replyType = "void";
  363. return true;
  364. }
  365. else if (fun == "reparseConfiguration()")
  366. {
  367. TDEGlobal::config()->reparseConfiguration();
  368. kdDebug(7016) << "TDELauncher::process : reparseConfiguration" << endl;
  369. KProtocolManager::reparseConfiguration();
  370. IdleSlave *slave;
  371. for(slave = mSlaveList.first(); slave; slave = mSlaveList.next())
  372. slave->reparseConfiguration();
  373. replyType = "void";
  374. return true;
  375. }
  376. else if (fun == "terminateKDE()")
  377. {
  378. ::signal( SIGHUP, SIG_IGN);
  379. ::signal( SIGTERM, SIG_IGN);
  380. kdDebug() << "TDELauncher::process ---> terminateKDE" << endl;
  381. tdelauncher_header request_header;
  382. request_header.cmd = LAUNCHER_TERMINATE_KDE;
  383. request_header.arg_length = 0;
  384. write(tdeinitSocket, &request_header, sizeof(request_header));
  385. destruct(0);
  386. }
  387. else if (fun == "autoStart()")
  388. {
  389. kdDebug() << "TDELauncher::process ---> autoStart" << endl;
  390. autoStart(1);
  391. replyType = "void";
  392. return true;
  393. }
  394. else if (fun == "autoStart(int)")
  395. {
  396. kdDebug() << "TDELauncher::process ---> autoStart(int)" << endl;
  397. TQDataStream stream(data, IO_ReadOnly);
  398. int phase;
  399. stream >> phase;
  400. autoStart(phase);
  401. replyType = "void";
  402. return true;
  403. }
  404. if (DCOPObject::process(fun, data, replyType, replyData))
  405. {
  406. return true;
  407. }
  408. kdWarning(7016) << "Got unknown DCOP function: " << fun << endl;
  409. return false;
  410. }
  411. QCStringList
  412. TDELauncher::interfaces()
  413. {
  414. QCStringList ifaces = DCOPObject::interfaces();
  415. ifaces += "TDELauncher";
  416. return ifaces;
  417. }
  418. QCStringList
  419. TDELauncher::functions()
  420. {
  421. QCStringList funcs = DCOPObject::functions();
  422. funcs << "void exec_blind(TQCString,TQValueList<TQCString>)";
  423. funcs << "void exec_blind(TQCString,TQValueList<TQCString>,TQValueList<TQCString>,TQCString)";
  424. funcs << "serviceResult start_service_by_name(TQString,TQStringList)";
  425. funcs << "serviceResult start_service_by_desktop_path(TQString,TQStringList)";
  426. funcs << "serviceResult start_service_by_desktop_name(TQString,TQStringList)";
  427. funcs << "serviceResult tdeinit_exec(TQString,TQStringList)";
  428. funcs << "serviceResult tdeinit_exec_wait(TQString,TQStringList)";
  429. funcs << "serviceResult start_service_by_name(TQString,TQStringList,TQValueList<TQCString>,TQCString)";
  430. funcs << "serviceResult start_service_by_desktop_path(TQString,TQStringList,TQValueList<TQCString>,TQCString)";
  431. funcs << "serviceResult start_service_by_desktop_name(TQString,TQStringList,TQValueList<TQCString>,TQCString)";
  432. funcs << "serviceResult start_service_by_name(TQString,TQStringList,TQValueList<TQCString>,TQCString,bool)";
  433. funcs << "serviceResult start_service_by_desktop_path(TQString,TQStringList,TQValueList<TQCString>,TQCString,bool)";
  434. funcs << "serviceResult start_service_by_desktop_name(TQString,TQStringList,TQValueList<TQCString>,TQCString,bool)";
  435. funcs << "serviceResult tdeinit_exec(TQString,TQStringList,TQValueList<TQCString>)";
  436. funcs << "serviceResult tdeinit_exec_wait(TQString,TQStringList,TQValueList<TQCString>)";
  437. funcs << "TQString requestSlave(TQString,TQString,TQString)";
  438. funcs << "pid_t requestHoldSlave(KURL,TQString)";
  439. funcs << "void waitForSlave(pid_t)";
  440. funcs << "void setLaunchEnv(TQCString,TQCString)";
  441. funcs << "void reparseConfiguration()";
  442. // funcs << "void terminateKDE()";
  443. funcs << "void autoStart()";
  444. funcs << "void autoStart(int)";
  445. return funcs;
  446. }
  447. void TDELauncher::setLaunchEnv(const TQCString &name, const TQCString &_value)
  448. {
  449. TQCString value(_value);
  450. if (value.isNull())
  451. value = "";
  452. tdelauncher_header request_header;
  453. TQByteArray requestData(name.length()+value.length()+2);
  454. memcpy(requestData.data(), name.data(), name.length()+1);
  455. memcpy(requestData.data()+name.length()+1, value.data(), value.length()+1);
  456. request_header.cmd = LAUNCHER_SETENV;
  457. request_header.arg_length = requestData.size();
  458. write(tdeinitSocket, &request_header, sizeof(request_header));
  459. write(tdeinitSocket, requestData.data(), request_header.arg_length);
  460. }
  461. /*
  462. * Read 'len' bytes from 'sock' into buffer.
  463. * returns -1 on failure, 0 on no data.
  464. */
  465. static int
  466. read_socket(int sock, char *buffer, int len)
  467. {
  468. ssize_t result;
  469. int bytes_left = len;
  470. while ( bytes_left > 0)
  471. {
  472. result = read(sock, buffer, bytes_left);
  473. if (result > 0)
  474. {
  475. buffer += result;
  476. bytes_left -= result;
  477. }
  478. else if (result == 0)
  479. return -1;
  480. else if ((result == -1) && (errno != EINTR))
  481. return -1;
  482. }
  483. return 0;
  484. }
  485. void
  486. TDELauncher::slotKDEInitData(int)
  487. {
  488. tdelauncher_header request_header;
  489. TQByteArray requestData;
  490. if( dontBlockReading )
  491. {
  492. // in case we get a request to start an application and data arrive
  493. // to tdeinitSocket at the same time, requestStart() will already
  494. // call slotKDEInitData(), so we must check there's still something
  495. // to read, otherwise this would block
  496. fd_set in;
  497. timeval tm = { 0, 0 };
  498. FD_ZERO ( &in );
  499. FD_SET( tdeinitSocket, &in );
  500. select( tdeinitSocket + 1, &in, 0, 0, &tm );
  501. if( !FD_ISSET( tdeinitSocket, &in ))
  502. return;
  503. }
  504. dontBlockReading = false;
  505. int result = read_socket(tdeinitSocket, (char *) &request_header,
  506. sizeof( request_header));
  507. if (result == -1)
  508. {
  509. kdDebug() << "Exiting on read_socket errno: " << errno << endl;
  510. ::signal( SIGHUP, SIG_IGN);
  511. ::signal( SIGTERM, SIG_IGN);
  512. destruct(255); // Exit!
  513. }
  514. requestData.resize(request_header.arg_length);
  515. result = read_socket(tdeinitSocket, (char *) requestData.data(),
  516. request_header.arg_length);
  517. if (request_header.cmd == LAUNCHER_DIED)
  518. {
  519. long *request_data;
  520. request_data = (long *) requestData.data();
  521. processDied(request_data[0], request_data[1]);
  522. return;
  523. }
  524. if (lastRequest && (request_header.cmd == LAUNCHER_OK))
  525. {
  526. long *request_data;
  527. request_data = (long *) requestData.data();
  528. lastRequest->pid = (pid_t) (*request_data);
  529. kdDebug(7016) << lastRequest->name << " (pid " << lastRequest->pid <<
  530. ") up and running." << endl;
  531. switch(lastRequest->dcop_service_type)
  532. {
  533. case KService::DCOP_None:
  534. {
  535. lastRequest->status = TDELaunchRequest::Running;
  536. break;
  537. }
  538. case KService::DCOP_Unique:
  539. {
  540. lastRequest->status = TDELaunchRequest::Launching;
  541. break;
  542. }
  543. case KService::DCOP_Wait:
  544. {
  545. lastRequest->status = TDELaunchRequest::Launching;
  546. break;
  547. }
  548. case KService::DCOP_Multi:
  549. {
  550. lastRequest->status = TDELaunchRequest::Launching;
  551. break;
  552. }
  553. }
  554. lastRequest = 0;
  555. return;
  556. }
  557. if (lastRequest && (request_header.cmd == LAUNCHER_ERROR))
  558. {
  559. lastRequest->status = TDELaunchRequest::Error;
  560. if (!requestData.isEmpty())
  561. lastRequest->errorMsg = TQString::fromUtf8((char *) requestData.data());
  562. lastRequest = 0;
  563. return;
  564. }
  565. kdWarning(7016) << "Unexpected command from KDEInit (" << (unsigned int) request_header.cmd
  566. << ")" << endl;
  567. }
  568. void
  569. TDELauncher::processDied(pid_t pid, long /* exitStatus */)
  570. {
  571. TDELaunchRequest *request = requestList.first();
  572. for(; request; request = requestList.next())
  573. {
  574. if (request->pid == pid)
  575. {
  576. if (request->dcop_service_type == KService::DCOP_Wait)
  577. request->status = TDELaunchRequest::Done;
  578. else if ((request->dcop_service_type == KService::DCOP_Unique) &&
  579. (dcopClient()->isApplicationRegistered(request->dcop_name)))
  580. request->status = TDELaunchRequest::Running;
  581. else
  582. request->status = TDELaunchRequest::Error;
  583. requestDone(request);
  584. return;
  585. }
  586. }
  587. }
  588. void
  589. TDELauncher::slotAppRegistered(const TQCString &appId)
  590. {
  591. const char *cAppId = appId.data();
  592. if (!cAppId) return;
  593. TDELaunchRequest *request = requestList.first();
  594. TDELaunchRequest *nextRequest;
  595. for(; request; request = nextRequest)
  596. {
  597. nextRequest = requestList.next();
  598. if (request->status != TDELaunchRequest::Launching)
  599. continue;
  600. // For unique services check the requested service name first
  601. if ((request->dcop_service_type == KService::DCOP_Unique) &&
  602. ((appId == request->dcop_name) ||
  603. dcopClient()->isApplicationRegistered(request->dcop_name)))
  604. {
  605. request->status = TDELaunchRequest::Running;
  606. requestDone(request);
  607. continue;
  608. }
  609. const char *rAppId = request->dcop_name.data();
  610. if (!rAppId) continue;
  611. int l = strlen(rAppId);
  612. if ((strncmp(rAppId, cAppId, l) == 0) &&
  613. ((cAppId[l] == '\0') || (cAppId[l] == '-')))
  614. {
  615. request->dcop_name = appId;
  616. request->status = TDELaunchRequest::Running;
  617. requestDone(request);
  618. continue;
  619. }
  620. }
  621. }
  622. void
  623. TDELauncher::autoStart(int phase)
  624. {
  625. if( mAutoStart.phase() >= phase )
  626. return;
  627. mAutoStart.setPhase(phase);
  628. if( newStartup )
  629. {
  630. if (phase == 0)
  631. mAutoStart.loadAutoStartList();
  632. }
  633. else
  634. {
  635. if (phase == 1)
  636. mAutoStart.loadAutoStartList();
  637. }
  638. mAutoTimer.start(0, true);
  639. }
  640. void
  641. TDELauncher::slotAutoStart()
  642. {
  643. KService::Ptr s;
  644. do
  645. {
  646. TQString service = mAutoStart.startService();
  647. if (service.isEmpty())
  648. {
  649. // Done
  650. if( !mAutoStart.phaseDone())
  651. {
  652. mAutoStart.setPhaseDone();
  653. // Emit signal
  654. if( newStartup )
  655. {
  656. TQCString autoStartSignal;
  657. autoStartSignal.sprintf( "autoStart%dDone()", mAutoStart.phase());
  658. emitDCOPSignal(autoStartSignal, TQByteArray());
  659. }
  660. else
  661. {
  662. TQCString autoStartSignal( "autoStartDone()" );
  663. int phase = mAutoStart.phase();
  664. if ( phase > 1 )
  665. autoStartSignal.sprintf( "autoStart%dDone()", phase );
  666. emitDCOPSignal(autoStartSignal, TQByteArray());
  667. }
  668. }
  669. return;
  670. }
  671. s = new KService(service);
  672. }
  673. while (!start_service(s, TQStringList(), TQValueList<TQCString>(), "0", false, true));
  674. // Loop till we find a service that we can start.
  675. }
  676. void
  677. TDELauncher::requestDone(TDELaunchRequest *request)
  678. {
  679. if ((request->status == TDELaunchRequest::Running) ||
  680. (request->status == TDELaunchRequest::Done))
  681. {
  682. DCOPresult.result = 0;
  683. DCOPresult.dcopName = request->dcop_name;
  684. DCOPresult.error = TQString::null;
  685. DCOPresult.pid = request->pid;
  686. }
  687. else
  688. {
  689. DCOPresult.result = 1;
  690. DCOPresult.dcopName = "";
  691. DCOPresult.error = i18n("KDEInit could not launch '%1'.").arg(TQString(request->name));
  692. if (!request->errorMsg.isEmpty())
  693. DCOPresult.error += ":\n" + request->errorMsg;
  694. DCOPresult.pid = 0;
  695. #if defined Q_WS_X11 && ! defined K_WS_QTONLY
  696. //#ifdef Q_WS_X11
  697. if (!request->startup_dpy.isEmpty())
  698. {
  699. Display* dpy = NULL;
  700. if( (mCached_dpy != NULL) &&
  701. (request->startup_dpy == XDisplayString( mCached_dpy )))
  702. dpy = mCached_dpy;
  703. if( dpy == NULL )
  704. dpy = XOpenDisplay( request->startup_dpy );
  705. if( dpy )
  706. {
  707. TDEStartupInfoId id;
  708. id.initId( request->startup_id );
  709. TDEStartupInfo::sendFinishX( dpy, id );
  710. if( mCached_dpy != dpy && mCached_dpy != NULL )
  711. XCloseDisplay( mCached_dpy );
  712. mCached_dpy = dpy;
  713. }
  714. }
  715. #endif
  716. }
  717. if (request->autoStart)
  718. {
  719. mAutoTimer.start(0, true);
  720. }
  721. if (request->transaction)
  722. {
  723. TQByteArray replyData;
  724. TQCString replyType;
  725. replyType = "serviceResult";
  726. TQDataStream stream2(replyData, IO_WriteOnly);
  727. stream2 << DCOPresult.result << DCOPresult.dcopName << DCOPresult.error << DCOPresult.pid;
  728. dcopClient()->endTransaction( request->transaction,
  729. replyType, replyData);
  730. }
  731. requestList.removeRef( request );
  732. }
  733. void
  734. TDELauncher::requestStart(TDELaunchRequest *request)
  735. {
  736. requestList.append( request );
  737. // Send request to tdeinit.
  738. tdelauncher_header request_header;
  739. TQByteArray requestData;
  740. int length = 0;
  741. length += sizeof(long); // Nr of. Args
  742. length += request->name.length() + 1; // Cmd
  743. for(TQValueList<TQCString>::Iterator it = request->arg_list.begin();
  744. it != request->arg_list.end();
  745. it++)
  746. {
  747. length += (*it).length() + 1; // Args...
  748. }
  749. length += sizeof(long); // Nr of. envs
  750. for(TQValueList<TQCString>::ConstIterator it = request->envs.begin();
  751. it != request->envs.end();
  752. it++)
  753. {
  754. length += (*it).length() + 1; // Envs...
  755. }
  756. length += sizeof( long ); // avoid_loops
  757. #ifdef Q_WS_X11
  758. bool startup_notify = !request->startup_id.isNull() && request->startup_id != "0";
  759. if( startup_notify )
  760. length += request->startup_id.length() + 1;
  761. #endif
  762. if (!request->cwd.isEmpty())
  763. length += request->cwd.length() + 1;
  764. requestData.resize( length );
  765. char *p = requestData.data();
  766. long l = request->arg_list.count()+1;
  767. memcpy(p, &l, sizeof(long));
  768. p += sizeof(long);
  769. strcpy(p, request->name.data());
  770. p += strlen(p) + 1;
  771. for(TQValueList<TQCString>::Iterator it = request->arg_list.begin();
  772. it != request->arg_list.end();
  773. it++)
  774. {
  775. strcpy(p, (*it).data());
  776. p += strlen(p) + 1;
  777. }
  778. l = request->envs.count();
  779. memcpy(p, &l, sizeof(long));
  780. p += sizeof(long);
  781. for(TQValueList<TQCString>::ConstIterator it = request->envs.begin();
  782. it != request->envs.end();
  783. it++)
  784. {
  785. strcpy(p, (*it).data());
  786. p += strlen(p) + 1;
  787. }
  788. l = 0; // avoid_loops, always false here
  789. memcpy(p, &l, sizeof(long));
  790. p += sizeof(long);
  791. #ifdef Q_WS_X11
  792. if( startup_notify )
  793. {
  794. strcpy(p, request->startup_id.data());
  795. p += strlen( p ) + 1;
  796. }
  797. #endif
  798. if (!request->cwd.isEmpty())
  799. {
  800. strcpy(p, request->cwd.data());
  801. p += strlen( p ) + 1;
  802. }
  803. #ifdef Q_WS_X11
  804. request_header.cmd = startup_notify ? LAUNCHER_EXT_EXEC : LAUNCHER_EXEC_NEW;
  805. #else
  806. request_header.cmd = LAUNCHER_EXEC_NEW;
  807. #endif
  808. request_header.arg_length = length;
  809. write(tdeinitSocket, &request_header, sizeof(request_header));
  810. write(tdeinitSocket, requestData.data(), request_header.arg_length);
  811. // Wait for pid to return.
  812. lastRequest = request;
  813. dontBlockReading = false;
  814. do {
  815. slotKDEInitData( tdeinitSocket );
  816. }
  817. while (lastRequest != 0);
  818. dontBlockReading = true;
  819. }
  820. void
  821. TDELauncher::exec_blind( const TQCString &name, const TQValueList<TQCString> &arg_list,
  822. const TQValueList<TQCString> &envs, const TQCString& startup_id )
  823. {
  824. TDELaunchRequest *request = new TDELaunchRequest;
  825. request->autoStart = false;
  826. request->name = name;
  827. request->arg_list = arg_list;
  828. request->dcop_name = 0;
  829. request->dcop_service_type = KService::DCOP_None;
  830. request->pid = 0;
  831. request->status = TDELaunchRequest::Launching;
  832. request->transaction = 0; // No confirmation is send
  833. request->envs = envs;
  834. // Find service, if any - strip path if needed
  835. KService::Ptr service = KService::serviceByDesktopName( name.mid( name.findRev( '/' ) + 1 ));
  836. if (service != NULL)
  837. send_service_startup_info( request, service,
  838. startup_id, TQValueList< TQCString >());
  839. else // no .desktop file, no startup info
  840. cancel_service_startup_info( request, startup_id, envs );
  841. requestStart(request);
  842. // We don't care about this request any longer....
  843. requestDone(request);
  844. }
  845. bool
  846. TDELauncher::start_service_by_name(const TQString &serviceName, const TQStringList &urls,
  847. const TQValueList<TQCString> &envs, const TQCString& startup_id, bool blind)
  848. {
  849. KService::Ptr service = 0;
  850. // Find service
  851. service = KService::serviceByName(serviceName);
  852. if (!service)
  853. {
  854. DCOPresult.result = ENOENT;
  855. DCOPresult.error = i18n("Could not find service '%1'.").arg(serviceName);
  856. cancel_service_startup_info( NULL, startup_id, envs ); // cancel it if any
  857. return false;
  858. }
  859. return start_service(service, urls, envs, startup_id, blind);
  860. }
  861. bool
  862. TDELauncher::start_service_by_desktop_path(const TQString &serviceName, const TQStringList &urls,
  863. const TQValueList<TQCString> &envs, const TQCString& startup_id, bool blind)
  864. {
  865. KService::Ptr service = 0;
  866. // Find service
  867. if (serviceName[0] == '/')
  868. {
  869. // Full path
  870. service = new KService(serviceName);
  871. }
  872. else
  873. {
  874. service = KService::serviceByDesktopPath(serviceName);
  875. }
  876. if (!service)
  877. {
  878. DCOPresult.result = ENOENT;
  879. DCOPresult.error = i18n("Could not find service '%1'.").arg(serviceName);
  880. cancel_service_startup_info( NULL, startup_id, envs ); // cancel it if any
  881. return false;
  882. }
  883. return start_service(service, urls, envs, startup_id, blind);
  884. }
  885. bool
  886. TDELauncher::start_service_by_desktop_name(const TQString &serviceName, const TQStringList &urls,
  887. const TQValueList<TQCString> &envs, const TQCString& startup_id, bool blind)
  888. {
  889. KService::Ptr service = 0;
  890. // Find service
  891. service = KService::serviceByDesktopName(serviceName);
  892. if (!service)
  893. {
  894. DCOPresult.result = ENOENT;
  895. DCOPresult.error = i18n("Could not find service '%1'.").arg(serviceName);
  896. cancel_service_startup_info( NULL, startup_id, envs ); // cancel it if any
  897. return false;
  898. }
  899. return start_service(service, urls, envs, startup_id, blind);
  900. }
  901. bool
  902. TDELauncher::start_service(KService::Ptr service, const TQStringList &_urls,
  903. const TQValueList<TQCString> &envs, const TQCString& startup_id, bool blind, bool autoStart)
  904. {
  905. TQStringList urls = _urls;
  906. if (!service->isValid())
  907. {
  908. DCOPresult.result = ENOEXEC;
  909. DCOPresult.error = i18n("Service '%1' is malformatted.").arg(service->desktopEntryPath());
  910. cancel_service_startup_info( NULL, startup_id, envs ); // cancel it if any
  911. return false;
  912. }
  913. TDELaunchRequest *request = new TDELaunchRequest;
  914. request->autoStart = autoStart;
  915. if ((urls.count() > 1) && !service->allowMultipleFiles())
  916. {
  917. // We need to launch the application N times. That sucks.
  918. // We ignore the result for application 2 to N.
  919. // For the first file we launch the application in the
  920. // usual way. The reported result is based on this
  921. // application.
  922. TQStringList::ConstIterator it = urls.begin();
  923. for(++it;
  924. it != urls.end();
  925. ++it)
  926. {
  927. TQStringList singleUrl;
  928. singleUrl.append(*it);
  929. TQCString startup_id2 = startup_id;
  930. if( !startup_id2.isEmpty() && startup_id2 != "0" )
  931. startup_id2 = "0"; // can't use the same startup_id several times
  932. start_service( service, singleUrl, envs, startup_id2, true);
  933. }
  934. TQString firstURL = *(urls.begin());
  935. urls.clear();
  936. urls.append(firstURL);
  937. }
  938. createArgs(request, service, urls);
  939. // We must have one argument at least!
  940. if (!request->arg_list.count())
  941. {
  942. DCOPresult.result = ENOEXEC;
  943. DCOPresult.error = i18n("Service '%1' is malformatted.").arg(service->desktopEntryPath());
  944. delete request;
  945. cancel_service_startup_info( NULL, startup_id, envs );
  946. return false;
  947. }
  948. request->name = request->arg_list.first();
  949. request->arg_list.remove(request->arg_list.begin());
  950. request->dcop_service_type = service->DCOPServiceType();
  951. if ((request->dcop_service_type == KService::DCOP_Unique) ||
  952. (request->dcop_service_type == KService::DCOP_Multi))
  953. {
  954. TQVariant v = service->property("X-DCOP-ServiceName");
  955. if (v.isValid())
  956. request->dcop_name = v.toString().utf8();
  957. if (request->dcop_name.isEmpty())
  958. {
  959. request->dcop_name = TQFile::encodeName(KRun::binaryName(service->exec(), true));
  960. }
  961. }
  962. request->pid = 0;
  963. request->transaction = 0;
  964. request->envs = envs;
  965. send_service_startup_info( request, service, startup_id, envs );
  966. // Request will be handled later.
  967. if (!blind && !autoStart)
  968. {
  969. request->transaction = dcopClient()->beginTransaction();
  970. }
  971. queueRequest(request);
  972. return true;
  973. }
  974. void
  975. TDELauncher::send_service_startup_info( TDELaunchRequest *request, KService::Ptr service, const TQCString& startup_id,
  976. const TQValueList<TQCString> &envs )
  977. {
  978. #if defined Q_WS_X11 && ! defined K_WS_QTONLY
  979. //#ifdef Q_WS_X11 // TDEStartup* isn't implemented for Qt/Embedded yet
  980. request->startup_id = "0";
  981. if( startup_id == "0" )
  982. return;
  983. bool silent;
  984. TQCString wmclass;
  985. if( !KRun::checkStartupNotify( TQString::null, service, &silent, &wmclass ))
  986. return;
  987. TDEStartupInfoId id;
  988. id.initId( startup_id );
  989. const char* dpy_str = NULL;
  990. for( TQValueList<TQCString>::ConstIterator it = envs.begin();
  991. it != envs.end();
  992. ++it )
  993. if( strncmp( *it, "DISPLAY=", 8 ) == 0 )
  994. dpy_str = static_cast< const char* >( *it ) + 8;
  995. Display* dpy = NULL;
  996. if( dpy_str != NULL && mCached_dpy != NULL
  997. && qstrcmp( dpy_str, XDisplayString( mCached_dpy )) == 0 )
  998. dpy = mCached_dpy;
  999. if( dpy == NULL )
  1000. dpy = XOpenDisplay( dpy_str );
  1001. request->startup_id = id.id();
  1002. if( dpy == NULL )
  1003. {
  1004. cancel_service_startup_info( request, startup_id, envs );
  1005. return;
  1006. }
  1007. request->startup_dpy = dpy_str;
  1008. TDEStartupInfoData data;
  1009. data.setName( service->name());
  1010. data.setIcon( service->icon());
  1011. data.setDescription( i18n( "Launching %1" ).arg( service->name()));
  1012. if( !wmclass.isEmpty())
  1013. data.setWMClass( wmclass );
  1014. if( silent )
  1015. data.setSilent( TDEStartupInfoData::Yes );
  1016. // the rest will be sent by tdeinit
  1017. TDEStartupInfo::sendStartupX( dpy, id, data );
  1018. if( mCached_dpy != dpy && mCached_dpy != NULL )
  1019. XCloseDisplay( mCached_dpy );
  1020. mCached_dpy = dpy;
  1021. return;
  1022. #else
  1023. return;
  1024. #endif
  1025. }
  1026. void
  1027. TDELauncher::cancel_service_startup_info( TDELaunchRequest* request, const TQCString& startup_id,
  1028. const TQValueList<TQCString> &envs )
  1029. {
  1030. #if defined Q_WS_X11 && ! defined K_WS_QTONLY
  1031. //#ifdef Q_WS_X11 // TDEStartup* isn't implemented for Qt/Embedded yet
  1032. if( request != NULL )
  1033. request->startup_id = "0";
  1034. if( !startup_id.isEmpty() && startup_id != "0" )
  1035. {
  1036. const char* dpy_str = NULL;
  1037. for( TQValueList<TQCString>::ConstIterator it = envs.begin();
  1038. it != envs.end();
  1039. ++it )
  1040. if( strncmp( *it, "DISPLAY=", 8 ) == 0 )
  1041. dpy_str = static_cast< const char* >( *it ) + 8;
  1042. Display* dpy = NULL;
  1043. if( dpy_str != NULL && mCached_dpy != NULL
  1044. && qstrcmp( dpy_str, XDisplayString( mCached_dpy )) == 0 )
  1045. dpy = mCached_dpy;
  1046. if( dpy == NULL )
  1047. dpy = XOpenDisplay( dpy_str );
  1048. if( dpy == NULL )
  1049. return;
  1050. TDEStartupInfoId id;
  1051. id.initId( startup_id );
  1052. TDEStartupInfo::sendFinishX( dpy, id );
  1053. if( mCached_dpy != dpy && mCached_dpy != NULL )
  1054. XCloseDisplay( mCached_dpy );
  1055. mCached_dpy = dpy;
  1056. }
  1057. #endif
  1058. }
  1059. bool
  1060. TDELauncher::tdeinit_exec(const TQString &app, const TQStringList &args,
  1061. const TQValueList<TQCString> &envs, TQCString startup_id, bool wait)
  1062. {
  1063. TDELaunchRequest *request = new TDELaunchRequest;
  1064. request->autoStart = false;
  1065. for(TQStringList::ConstIterator it = args.begin();
  1066. it != args.end();
  1067. it++)
  1068. {
  1069. TQString arg = *it;
  1070. request->arg_list.append(arg.local8Bit());
  1071. }
  1072. request->name = app.local8Bit();
  1073. if (wait)
  1074. request->dcop_service_type = KService::DCOP_Wait;
  1075. else
  1076. request->dcop_service_type = KService::DCOP_None;
  1077. request->dcop_name = 0;
  1078. request->pid = 0;
  1079. #ifdef Q_WS_X11
  1080. request->startup_id = startup_id;
  1081. #endif
  1082. request->envs = envs;
  1083. if( app != "tdebuildsycoca" ) // avoid stupid loop
  1084. {
  1085. // Find service, if any - strip path if needed
  1086. KService::Ptr service = KService::serviceByDesktopName( app.mid( app.findRev( '/' ) + 1 ));
  1087. if (service != NULL)
  1088. send_service_startup_info( request, service,
  1089. startup_id, TQValueList< TQCString >());
  1090. else // no .desktop file, no startup info
  1091. cancel_service_startup_info( request, startup_id, envs );
  1092. }
  1093. request->transaction = dcopClient()->beginTransaction();
  1094. queueRequest(request);
  1095. return true;
  1096. }
  1097. void
  1098. TDELauncher::queueRequest(TDELaunchRequest *request)
  1099. {
  1100. requestQueue.append( request );
  1101. if (!bProcessingQueue)
  1102. {
  1103. bProcessingQueue = true;
  1104. TQTimer::singleShot(0, this, TQT_SLOT( slotDequeue() ));
  1105. }
  1106. }
  1107. void
  1108. TDELauncher::slotDequeue()
  1109. {
  1110. do {
  1111. TDELaunchRequest *request = requestQueue.take(0);
  1112. // process request
  1113. request->status = TDELaunchRequest::Launching;
  1114. requestStart(request);
  1115. if (request->status != TDELaunchRequest::Launching)
  1116. {
  1117. // Request handled.
  1118. requestDone( request );
  1119. continue;
  1120. }
  1121. } while(requestQueue.count());
  1122. bProcessingQueue = false;
  1123. }
  1124. void
  1125. TDELauncher::createArgs( TDELaunchRequest *request, const KService::Ptr service ,
  1126. const TQStringList &urls)
  1127. {
  1128. TQStringList params = KRun::processDesktopExec(*service, urls, false);
  1129. for(TQStringList::ConstIterator it = params.begin();
  1130. it != params.end(); ++it)
  1131. {
  1132. request->arg_list.append((*it).local8Bit());
  1133. }
  1134. request->cwd = TQFile::encodeName(service->path());
  1135. }
  1136. ///// IO-Slave functions
  1137. pid_t
  1138. TDELauncher::requestHoldSlave(const KURL &url, const TQString &app_socket)
  1139. {
  1140. IdleSlave *slave;
  1141. for(slave = mSlaveList.first(); slave; slave = mSlaveList.next())
  1142. {
  1143. if (slave->onHold(url))
  1144. break;
  1145. }
  1146. if (slave)
  1147. {
  1148. mSlaveList.removeRef(slave);
  1149. slave->connect(app_socket);
  1150. return slave->pid();
  1151. }
  1152. return 0;
  1153. }
  1154. pid_t
  1155. TDELauncher::requestSlave(const TQString &protocol,
  1156. const TQString &host,
  1157. const TQString &app_socket,
  1158. TQString &error)
  1159. {
  1160. IdleSlave *slave;
  1161. for(slave = mSlaveList.first(); slave; slave = mSlaveList.next())
  1162. {
  1163. if (slave->match(protocol, host, true))
  1164. break;
  1165. }
  1166. if (!slave)
  1167. {
  1168. for(slave = mSlaveList.first(); slave; slave = mSlaveList.next())
  1169. {
  1170. if (slave->match(protocol, host, false))
  1171. break;
  1172. }
  1173. }
  1174. if (!slave)
  1175. {
  1176. for(slave = mSlaveList.first(); slave; slave = mSlaveList.next())
  1177. {
  1178. if (slave->match(protocol, TQString::null, false))
  1179. break;
  1180. }
  1181. }
  1182. if (slave)
  1183. {
  1184. mSlaveList.removeRef(slave);
  1185. slave->connect(app_socket);
  1186. return slave->pid();
  1187. }
  1188. TQString _name = KProtocolInfo::exec(protocol);
  1189. if (_name.isEmpty())
  1190. {
  1191. error = i18n("Unknown protocol '%1'.\n").arg(protocol);
  1192. return 0;
  1193. }
  1194. TQCString name = _name.latin1(); // ex: "tdeio_ftp"
  1195. TQCString arg1 = protocol.latin1();
  1196. TQCString arg2 = TQFile::encodeName(mPoolSocketName);
  1197. TQCString arg3 = TQFile::encodeName(app_socket);
  1198. TQValueList<TQCString> arg_list;
  1199. arg_list.append(arg1);
  1200. arg_list.append(arg2);
  1201. arg_list.append(arg3);
  1202. // kdDebug(7016) << "TDELauncher: launching new slave " << _name << " with protocol=" << protocol << endl;
  1203. if (mSlaveDebug == arg1)
  1204. {
  1205. tdelauncher_header request_header;
  1206. request_header.cmd = LAUNCHER_DEBUG_WAIT;
  1207. request_header.arg_length = 0;
  1208. write(tdeinitSocket, &request_header, sizeof(request_header));
  1209. }
  1210. if (mSlaveValgrind == arg1)
  1211. {
  1212. arg_list.prepend(TQFile::encodeName(KLibLoader::findLibrary(name)));
  1213. arg_list.prepend(TQFile::encodeName(locate("exe", "tdeioslave")));
  1214. name = "valgrind";
  1215. if (!mSlaveValgrindSkin.isEmpty()) {
  1216. arg_list.prepend(TQCString("--tool=") + mSlaveValgrindSkin);
  1217. } else
  1218. arg_list.prepend("--tool=memcheck");
  1219. }
  1220. TDELaunchRequest *request = new TDELaunchRequest;
  1221. request->autoStart = false;
  1222. request->name = name;
  1223. request->arg_list = arg_list;
  1224. request->dcop_name = 0;
  1225. request->dcop_service_type = KService::DCOP_None;
  1226. request->pid = 0;
  1227. #ifdef Q_WS_X11
  1228. request->startup_id = "0";
  1229. #endif
  1230. request->status = TDELaunchRequest::Launching;
  1231. request->transaction = 0; // No confirmation is send
  1232. requestStart(request);
  1233. pid_t pid = request->pid;
  1234. // kdDebug(7016) << "Slave launched, pid = " << pid << endl;
  1235. // We don't care about this request any longer....
  1236. requestDone(request);
  1237. if (!pid)
  1238. {
  1239. error = i18n("Error loading '%1'.\n").arg(TQString(name));
  1240. }
  1241. return pid;
  1242. }
  1243. void
  1244. TDELauncher::waitForSlave(pid_t pid)
  1245. {
  1246. IdleSlave *slave;
  1247. for(slave = mSlaveList.first(); slave; slave = mSlaveList.next())
  1248. {
  1249. if (slave->pid() == pid)
  1250. return; // Already here.
  1251. }
  1252. SlaveWaitRequest *waitRequest = new SlaveWaitRequest;
  1253. waitRequest->transaction = dcopClient()->beginTransaction();
  1254. waitRequest->pid = pid;
  1255. mSlaveWaitRequest.append(waitRequest);
  1256. }
  1257. void
  1258. TDELauncher::acceptSlave(TDESocket *slaveSocket)
  1259. {
  1260. IdleSlave *slave = new IdleSlave(slaveSocket);
  1261. // Send it a SLAVE_STATUS command.
  1262. mSlaveList.append(slave);
  1263. connect(slave, TQT_SIGNAL(destroyed()), this, TQT_SLOT(slotSlaveGone()));
  1264. connect(slave, TQT_SIGNAL(statusUpdate(IdleSlave *)),
  1265. this, TQT_SLOT(slotSlaveStatus(IdleSlave *)));
  1266. if (!mTimer.isActive())
  1267. {
  1268. mTimer.start(1000*10);
  1269. }
  1270. }
  1271. void
  1272. TDELauncher::slotSlaveStatus(IdleSlave *slave)
  1273. {
  1274. SlaveWaitRequest *waitRequest = mSlaveWaitRequest.first();
  1275. while(waitRequest)
  1276. {
  1277. if (waitRequest->pid == slave->pid())
  1278. {
  1279. TQByteArray replyData;
  1280. TQCString replyType;
  1281. replyType = "void";
  1282. dcopClient()->endTransaction( waitRequest->transaction, replyType, replyData);
  1283. mSlaveWaitRequest.removeRef(waitRequest);
  1284. waitRequest = mSlaveWaitRequest.current();
  1285. }
  1286. else
  1287. {
  1288. waitRequest = mSlaveWaitRequest.next();
  1289. }
  1290. }
  1291. }
  1292. void
  1293. TDELauncher::slotSlaveGone()
  1294. {
  1295. IdleSlave *slave = (IdleSlave *) sender();
  1296. mSlaveList.removeRef(slave);
  1297. if ((mSlaveList.count() == 0) && (mTimer.isActive()))
  1298. {
  1299. mTimer.stop();
  1300. }
  1301. }
  1302. void
  1303. TDELauncher::idleTimeout()
  1304. {
  1305. bool keepOneFileSlave=true;
  1306. time_t now = time(0);
  1307. IdleSlave *slave;
  1308. for(slave = mSlaveList.first(); slave; slave = mSlaveList.next())
  1309. {
  1310. if ((slave->protocol()=="file") && (keepOneFileSlave))
  1311. keepOneFileSlave=false;
  1312. else if (slave->age(now) > SLAVE_MAX_IDLE)
  1313. {
  1314. // killing idle slave
  1315. delete slave;
  1316. }
  1317. }
  1318. }
  1319. #include "tdelauncher.moc"