dcopserver_shutdown_win.cpp
00001 /* 00002 This file is part of the KDE libraries 00003 Copyright (c) 1999 Waldo Bastian <bastian@kde.org> 00004 (c) 1999 Mario Weilguni <mweilguni@sime.com> 00005 (c) 2001 Lubos Lunak <l.lunak@kde.org> 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License version 2 as published by the Free Software Foundation. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 00022 #ifdef HAVE_CONFIG_H 00023 #include <config.h> 00024 #endif 00025 00026 #ifdef HAVE_SYS_TYPES_H 00027 #include <sys/types.h> 00028 #endif 00029 00030 #include <sys/socket.h> 00031 #include <stdlib.h> 00032 #if 0 00033 #include <sys/select.h> 00034 #include <sys/time.h> 00035 #include <sys/types.h> 00036 #include <sys/param.h> 00037 #include <sys/time.h> 00038 #include <sys/stat.h> 00039 #include <sys/un.h> 00040 00041 #include <errno.h> 00042 #include <string.h> 00043 #include <stdio.h> 00044 #include <stdlib.h> 00045 #include <unistd.h> 00046 #include <pwd.h> 00047 #include <signal.h> 00048 #endif 00049 00050 #define QT_CLEAN_NAMESPACE 1 00051 #include <qfile.h> 00052 00053 #include <dcopclient.h> 00054 00055 #define BUFFER_SIZE 4096 00056 00057 extern QCString dcopServerFile(const QCString &hostname, bool old); 00058 00059 static char *getDisplay() 00060 { 00061 const char *display; 00062 char *result; 00063 char *screen; 00064 char *colon; 00065 /* 00066 don't test for a value from qglobal.h but instead distinguish 00067 Qt/X11 from Qt/Embedded by the fact that Qt/E apps have -DQWS 00068 on the commandline (which in qglobal.h however triggers Q_WS_QWS, 00069 but we don't want to include that here) (Simon) 00070 #ifdef Q_WS_X11 00071 */ 00072 #if !defined(QWS) 00073 display = getenv("DISPLAY"); 00074 #else 00075 display = getenv("QWS_DISPLAY"); 00076 #endif 00077 if (!display || !*display) 00078 { 00079 display = "NODISPLAY"; 00080 } 00081 result = (char*)malloc(strlen(display)+1); 00082 if (result == NULL) 00083 return NULL; 00084 strcpy(result, display); 00085 screen = strrchr(result, '.'); 00086 colon = strrchr(result, ':'); 00087 if (screen && (screen > colon)) 00088 *screen = '\0'; 00089 return result; 00090 } 00091 00092 static void cleanupDCOPsocket(const char *socketfile) 00093 { 00094 char cmd[BUFFER_SIZE]; 00095 char buffer[BUFFER_SIZE]; 00096 const char *socket_file; 00097 int l; 00098 00099 l = strlen(socketfile); 00100 if (!l) 00101 return; 00102 strncpy(buffer,socketfile,l); 00103 buffer[l-1] = '\0'; /* strip LF */ 00104 00105 socket_file = strchr(buffer, ':'); 00106 if (socket_file) 00107 socket_file++; 00108 00109 if (socket_file) 00110 unlink(socket_file); 00111 00112 snprintf(cmd, BUFFER_SIZE, "iceauth remove netid='%s'", buffer); 00113 system(cmd); 00114 } 00115 00116 #ifdef Q_OS_WIN 00117 static void killDCOPWin(pid_t pid) 00118 { 00119 char sz[256]; 00120 sprintf(sz,"dcopserver%i",pid); 00121 HANDLE hEvent = CreateEventA(NULL,TRUE,FALSE,(LPCSTR)sz); 00122 DWORD dwError = GetLastError(); 00123 printf("Signal event %s %p, %i\n",sz,hEvent,dwError); 00124 if(hEvent != NULL) 00125 { 00126 SetEvent(hEvent); 00127 CloseHandle(hEvent); 00128 } 00129 } 00130 #endif 00131 00132 static void cleanupDCOP(int dont_kill_dcop, int wait_for_exit) 00133 { 00134 QCString host; 00135 QCString strDCOPServer = DCOPClient::dcopServerFile(host); 00136 00137 if(strDCOPServer.isEmpty()) 00138 { 00139 printf("no server file\n"); 00140 return; 00141 } 00142 printf("server file %s\n",(const char *)strDCOPServer); 00143 00144 pid_t pid = 0; 00145 QFile f(strDCOPServer); 00146 if(f.open(IO_ReadOnly)) 00147 { 00148 QString str; 00149 while(f.readLine(str,2048)) 00150 { 00151 pid = str.toULong(); 00152 if (pid) 00153 break; 00154 cleanupDCOPsocket(str.ascii()); 00155 } 00156 } 00157 f.close(); 00158 /* Clean up .DCOPserver file */ 00159 QFile::remove(strDCOPServer); 00160 printf("remove server file %s\n",(const char *)strDCOPServer); 00161 00162 if(pid) 00163 { 00164 if(!dont_kill_dcop) 00165 { 00166 #ifdef Q_OS_WIN 00167 killDCOPWin(pid); 00168 #else 00169 kill(pid, SIGTERM); 00170 #endif 00171 } 00172 else 00173 { 00174 #ifdef Q_OS_WIN 00175 killDCOPWin(pid); 00176 #endif 00177 } 00178 } 00179 00180 #ifdef Q_OS_WIN 00181 if(wait_for_exit) 00182 { 00183 HANDLE hProcess = OpenProcess(SYNCHRONIZE,FALSE,(DWORD)pid); 00184 if(hProcess) 00185 { 00186 WaitForSingleObject(hProcess,INFINITE); 00187 CloseHandle(hProcess); 00188 } 00189 } 00190 #else 00191 while(wait_for_exit && (kill(pid, 0) == 0)) 00192 { 00193 struct timeval tv; 00194 tv.tv_sec = 0; 00195 tv.tv_usec = 100000; 00196 select(0,0,0,0,&tv); 00197 } 00198 #endif 00199 } 00200 00201 int main(int argc, char **argv) 00202 { 00203 QCString host; 00204 00205 int dont_kill_dcop = (argc == 2) && (strcmp(argv[1], "--nokill") == 0); 00206 int wait_for_exit = (argc == 2) && (strcmp(argv[1], "--wait") == 0); 00207 00208 cleanupDCOP(dont_kill_dcop, wait_for_exit); 00209 return 0; 00210 }