dcop
dcopserver_shutdown_win.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
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
00067
00068
00069
00070
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';
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
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 }