KDEsu
stub.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "stub.h"
00014 #include "kcookie.h"
00015
00016 #include <config.h>
00017 #include <stdlib.h>
00018 #include <unistd.h>
00019
00020 #include <QtCore/QBool>
00021
00022 #include <kapplication.h>
00023 #include <kdebug.h>
00024
00025 namespace KDESu {
00026
00027 using namespace KDESuPrivate;
00028
00029 StubProcess::StubProcess()
00030 : d(0)
00031 {
00032 m_User = "root";
00033 m_Scheduler = SchedNormal;
00034 m_Priority = 50;
00035 m_pCookie = new KCookie;
00036 m_bXOnly = true;
00037 }
00038
00039
00040 StubProcess::~StubProcess()
00041 {
00042 delete m_pCookie;
00043 }
00044
00045
00046 void StubProcess::setCommand(const QByteArray &command)
00047 {
00048 m_Command = command;
00049 }
00050
00051
00052 void StubProcess::setUser(const QByteArray &user)
00053 {
00054 m_User = user;
00055 }
00056
00057
00058 void StubProcess::setXOnly(bool xonly)
00059 {
00060 m_bXOnly = xonly;
00061 }
00062
00063
00064 void StubProcess::setPriority(int prio)
00065 {
00066 if (prio > 100)
00067 m_Priority = 100;
00068 else if (prio < 0)
00069 m_Priority = 0;
00070 else
00071 m_Priority = prio;
00072 }
00073
00074
00075 void StubProcess::setScheduler(int sched)
00076 {
00077 m_Scheduler = sched;
00078 }
00079
00080
00081 QByteArray StubProcess::commaSeparatedList(const QList<QByteArray> &lst)
00082 {
00083 QByteArray str;
00084 for (int i = 0; i < lst.count(); ++i) {
00085 str += ',';
00086 str += lst.at(i);
00087 }
00088 return str;
00089 }
00090
00091
00092
00093
00094
00095 template<int T> struct PIDType { typedef pid_t PID_t; } ;
00096 template<> struct PIDType<2> { typedef qint16 PID_t; } ;
00097 template<> struct PIDType<4> { typedef qint32 PID_t; } ;
00098
00099
00100
00101
00102
00103
00104
00105 int StubProcess::ConverseStub(int check)
00106 {
00107 QByteArray line, tmp;
00108 while (1)
00109 {
00110 line = readLine();
00111 if (line.isNull())
00112 return -1;
00113
00114 if (line == "kdesu_stub")
00115 {
00116
00117 enableLocalEcho(false);
00118 if (check) writeLine("stop");
00119 else writeLine("ok");
00120 } else if (line == "display") {
00121 writeLine(display());
00122 } else if (line == "display_auth") {
00123 #ifdef Q_WS_X11
00124 writeLine(displayAuth());
00125 #else
00126 writeLine("");
00127 #endif
00128 } else if (line == "command") {
00129 writeLine(m_Command);
00130 } else if (line == "path") {
00131 QByteArray path = qgetenv("PATH");
00132 if (!path.isEmpty() && path[0] == ':')
00133 path = path.mid(1);
00134 if (m_User == "root") {
00135 if (!path.isEmpty())
00136 path = "/sbin:/bin:/usr/sbin:/usr/bin:" + path;
00137 else
00138 path = "/sbin:/bin:/usr/sbin:/usr/bin";
00139 }
00140 writeLine(path);
00141 } else if (line == "user") {
00142 writeLine(m_User);
00143 } else if (line == "priority") {
00144 tmp.setNum(m_Priority);
00145 writeLine(tmp);
00146 } else if (line == "scheduler") {
00147 if (m_Scheduler == SchedRealtime) writeLine("realtime");
00148 else writeLine("normal");
00149 } else if (line == "xwindows_only") {
00150 if (m_bXOnly) writeLine("no");
00151 else writeLine("yes");
00152 } else if (line == "app_startup_id") {
00153 QList<QByteArray> env = environment();
00154 QByteArray tmp;
00155 for(int i = 0; i < env.count(); ++i)
00156 {
00157 const char startup_env[] = "DESKTOP_STARTUP_ID=";
00158 if (env.at(i).startsWith(startup_env))
00159 tmp = env.at(i).mid(sizeof(startup_env) - 1);
00160 }
00161 if( tmp.isEmpty())
00162 tmp = "0";
00163 writeLine(tmp);
00164 } else if (line == "app_start_pid") {
00165
00166
00167
00168 tmp.setNum((PIDType<sizeof(pid_t)>::PID_t)(getpid()));
00169 writeLine(tmp);
00170 } else if (line == "environment") {
00171 QList<QByteArray> env = environment();
00172 for (int i = 0; i < env.count(); ++i)
00173 writeLine(env.at(i));
00174 writeLine( "" );
00175 } else if (line == "end") {
00176 return 0;
00177 } else
00178 {
00179 kWarning(900) << k_lineinfo << "Unknown request: -->" << line
00180 << "<--\n";
00181 return 1;
00182 }
00183 }
00184
00185 return 0;
00186 }
00187
00188
00189 QByteArray StubProcess::display()
00190 {
00191 return m_pCookie->display();
00192 }
00193
00194
00195 #ifdef Q_WS_X11
00196 QByteArray StubProcess::displayAuth()
00197 {
00198 return m_pCookie->displayAuth();
00199 }
00200 #endif
00201
00202
00203 void StubProcess::virtual_hook( int id, void* data )
00204 { PtyProcess::virtual_hook( id, data ); }
00205
00206 }