KDED
khostname.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <sys/types.h>
00020 #include <sys/stat.h>
00021 #include <unistd.h>
00022 #include <stdlib.h>
00023 #include <stdio.h>
00024
00025 #include <QtCore/QFile>
00026 #include <QtCore/QRegExp>
00027 #include <QtCore/Q_PID>
00028
00029 #include <kcmdlineargs.h>
00030 #include <kapplication.h>
00031 #include <klocale.h>
00032 #include <kaboutdata.h>
00033 #include <kglobal.h>
00034 #include <kstandarddirs.h>
00035 #include <ktoolinvocation.h>
00036 #include <klauncher_iface.h>
00037 #include <kde_file.h>
00038 #include <QtDBus/QtDBus>
00039
00040 static const char appName[] = "kdontchangethehostname";
00041 static const char appVersion[] = "1.1";
00042
00043 class KHostName
00044 {
00045 public:
00046 KHostName();
00047
00048 void changeX();
00049 void changeStdDirs(const QByteArray &type);
00050 void changeSessionManager();
00051
00052 protected:
00053 QString oldName;
00054 QString newName;
00055 QString display;
00056 QByteArray home;
00057 };
00058
00059 KHostName::KHostName()
00060 {
00061 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00062 if (args->count() != 2)
00063 args->usage();
00064 oldName = args->arg(0);
00065 newName = args->arg(1);
00066 if (oldName == newName)
00067 exit(0);
00068
00069 home = qgetenv("HOME");
00070 if (home.isEmpty())
00071 {
00072 fprintf(stderr, "%s", i18n("Error: HOME environment variable not set.\n").toLocal8Bit().data());
00073 exit(1);
00074 }
00075
00076 display = QString::fromLocal8Bit(qgetenv("DISPLAY"));
00077
00078 display.remove(QRegExp("\\.[0-9]+$"));
00079 #if defined(Q_WS_X11) || defined(Q_WS_QWS)
00080 if (display.isEmpty())
00081 {
00082 fprintf(stderr, "%s", i18n("Error: DISPLAY environment variable not set.\n").toLocal8Bit().data());
00083 exit(1);
00084 }
00085 #endif
00086 }
00087
00088 static QList<QByteArray> split(const QByteArray &str)
00089 {
00090 const char *s = str.data();
00091 QList<QByteArray> result;
00092 while (*s)
00093 {
00094 const char *i = strchr(s, ' ');
00095 if (!i)
00096 {
00097 result.append(QByteArray(s));
00098 return result;
00099 }
00100 result.append(QByteArray(s, i-s+1));
00101 s = i;
00102 while (*s == ' ') s++;
00103 }
00104 return result;
00105 }
00106
00107 void KHostName::changeX()
00108 {
00109 QProcess proc;
00110 proc.start("xauth", QStringList() << "-n" << "list");
00111 if (!proc.waitForFinished())
00112 {
00113 fprintf(stderr, "Warning: Can not run xauth.\n");
00114 return;
00115 }
00116 QList<QByteArray> lines;
00117 {
00118 while (!proc.atEnd())
00119 {
00120 QByteArray line = proc.readLine();
00121 if (line.length())
00122 line.truncate(line.length()-1);
00123 if (!line.isEmpty())
00124 lines.append(line);
00125 }
00126 }
00127
00128 foreach ( const QByteArray &it, lines )
00129 {
00130 QList<QByteArray> entries = split(it);
00131 if (entries.count() != 3)
00132 continue;
00133
00134 QByteArray netId = entries[0];
00135 QByteArray authName = entries[1];
00136 QByteArray authKey = entries[2];
00137
00138 int i = netId.lastIndexOf(':');
00139 if (i == -1)
00140 continue;
00141 QByteArray netDisplay = netId.mid(i);
00142 if (netDisplay != display)
00143 continue;
00144
00145 i = netId.indexOf('/');
00146 if (i == -1)
00147 continue;
00148
00149 QString newNetId = newName+netId.mid(i);
00150 QString oldNetId = netId.left(i);
00151
00152 if (oldNetId != oldName)
00153 continue;
00154
00155 QProcess::execute("xauth", QStringList() << "-n" << "remove" << netId);
00156 QProcess::execute("xauth", QStringList() << "-n" << "add" << newNetId << authName << authKey);
00157 }
00158 }
00159
00160 void KHostName::changeStdDirs(const QByteArray &type)
00161 {
00162
00163 QByteArray oldDir = QFile::encodeName(QString("%1%2-%3").arg(KGlobal::dirs()->localkdedir()).arg(QString( type )).arg(QString( oldName )));
00164 QByteArray newDir = QFile::encodeName(QString("%1%2-%3").arg(KGlobal::dirs()->localkdedir()).arg(QString( type )).arg(QString( newName )));
00165
00166 KDE_struct_stat st_buf;
00167
00168 int result = KDE_lstat(oldDir.data(), &st_buf);
00169 if (result == 0)
00170 {
00171 if (S_ISLNK(st_buf.st_mode))
00172 {
00173 char buf[4096+1];
00174 result = readlink(oldDir.data(), buf, 4096);
00175 if (result >= 0)
00176 {
00177 buf[result] = 0;
00178 result = symlink(buf, newDir.data());
00179 }
00180 }
00181 else if (S_ISDIR(st_buf.st_mode))
00182 {
00183 result = symlink(oldDir.data(), newDir.data());
00184 }
00185 else
00186 {
00187 result = -1;
00188 }
00189 }
00190 if (result != 0)
00191 {
00192 const QString lnusertemp = KGlobal::dirs()->findExe( "lnusertemp" );
00193 QProcess::execute( lnusertemp, QStringList() << type );
00194 }
00195 }
00196
00197 void KHostName::changeSessionManager()
00198 {
00199 QString sm = QString::fromLocal8Bit(qgetenv("SESSION_MANAGER"));
00200 if (sm.isEmpty())
00201 {
00202 fprintf(stderr, "Warning: No session management specified.\n");
00203 return;
00204 }
00205 int i = sm.lastIndexOf(':');
00206 if ((i == -1) || (sm.left(6) != "local/"))
00207 {
00208 fprintf(stderr, "Warning: Session Management socket '%s' has unexpected format.\n", sm.toLocal8Bit().constData());
00209 return;
00210 }
00211 sm = "local/"+newName+sm.mid(i);
00212 KToolInvocation::klauncher()->call(QDBus::NoBlock, "setLaunchEnv", QByteArray("SESSION_MANAGER"), sm);
00213 }
00214
00215 int main(int argc, char **argv)
00216 {
00217 KAboutData d(appName, "kdelibs4", ki18n("KDontChangeTheHostName"), appVersion,
00218 ki18n("Informs KDE about a change in hostname"),
00219 KAboutData::License_GPL, ki18n("(c) 2001 Waldo Bastian"));
00220 d.addAuthor(ki18n("Waldo Bastian"), ki18n("Author"), "bastian@kde.org");
00221
00222 KCmdLineOptions options;
00223 options.add("+old", ki18n("Old hostname"));
00224 options.add("+new", ki18n("New hostname"));
00225
00226 KCmdLineArgs::init(argc, argv, &d);
00227 KCmdLineArgs::addCmdLineOptions(options);
00228
00229 KComponentData k(&d);
00230
00231 KHostName hn;
00232
00233 hn.changeX();
00234 hn.changeStdDirs("socket");
00235 hn.changeStdDirs("tmp");
00236 hn.changeSessionManager();
00237 }
00238