dcop
dcopserver_shutdown_win.cpp
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 #include <tqfile.h>
00051
00052 #include <dcopclient.h>
00053
00054 #define BUFFER_SIZE 4096
00055
00056 extern TQCString dcopServerFile(const TQCString &hostname, bool old);
00057
00058 static char *getDisplay()
00059 {
00060 const char *display;
00061 char *result;
00062 char *screen;
00063 char *colon;
00064
00065
00066
00067
00068
00069
00070
00071 #if !defined(QWS)
00072 display = getenv("DISPLAY");
00073 #else
00074 display = getenv("QWS_DISPLAY");
00075 #endif
00076 if (!display || !*display)
00077 {
00078 display = "NODISPLAY";
00079 }
00080 result = (char*)malloc(strlen(display)+1);
00081 if (result == NULL)
00082 return NULL;
00083 strcpy(result, display);
00084 screen = strrchr(result, '.');
00085 colon = strrchr(result, ':');
00086 if (screen && (screen > colon))
00087 *screen = '\0';
00088 return result;
00089 }
00090
00091 static void cleanupDCOPsocket(const char *socketfile)
00092 {
00093 char cmd[BUFFER_SIZE];
00094 char buffer[BUFFER_SIZE];
00095 const char *socket_file;
00096 int l;
00097
00098 l = strlen(socketfile);
00099 if (!l)
00100 return;
00101 strncpy(buffer,socketfile,l);
00102 buffer[l-1] = '\0';
00103
00104 socket_file = strchr(buffer, ':');
00105 if (socket_file)
00106 socket_file++;
00107
00108 if (socket_file)
00109 unlink(socket_file);
00110
00111 snprintf(cmd, BUFFER_SIZE, "iceauth remove netid='%s'", buffer);
00112 system(cmd);
00113 }
00114
00115 #ifdef Q_OS_WIN
00116 static void killDCOPWin(pid_t pid)
00117 {
00118 char sz[256];
00119 sprintf(sz,"dcopserver%i",pid);
00120 HANDLE hEvent = CreateEventA(NULL,TRUE,FALSE,(LPCSTR)sz);
00121 DWORD dwError = GetLastError();
00122 printf("Signal event %s %p, %i\n",sz,hEvent,dwError);
00123 if(hEvent != NULL)
00124 {
00125 SetEvent(hEvent);
00126 CloseHandle(hEvent);
00127 }
00128 }
00129 #endif
00130
00131 static void cleanupDCOP(int dont_kill_dcop, int wait_for_exit)
00132 {
00133 TQCString host;
00134 TQCString strDCOPServer = DCOPClient::dcopServerFile(host);
00135
00136 if(strDCOPServer.isEmpty())
00137 {
00138 printf("no server file\n");
00139 return;
00140 }
00141 printf("server file %s\n",(const char *)strDCOPServer);
00142
00143 pid_t pid = 0;
00144 TQFile f(strDCOPServer);
00145 if(f.open(IO_ReadOnly))
00146 {
00147 TQString str;
00148 while(f.readLine(str,2048))
00149 {
00150 pid = str.toULong();
00151 if (pid)
00152 break;
00153 cleanupDCOPsocket(str.ascii());
00154 }
00155 }
00156 f.close();
00157
00158 TQFile::remove(strDCOPServer);
00159 printf("remove server file %s\n",(const char *)strDCOPServer);
00160
00161 if(pid)
00162 {
00163 if(!dont_kill_dcop)
00164 {
00165 #ifdef Q_OS_WIN
00166 killDCOPWin(pid);
00167 #else
00168 kill(pid, SIGTERM);
00169 #endif
00170 }
00171 else
00172 {
00173 #ifdef Q_OS_WIN
00174 killDCOPWin(pid);
00175 #endif
00176 }
00177 }
00178
00179 #ifdef Q_OS_WIN
00180 if(wait_for_exit)
00181 {
00182 HANDLE hProcess = OpenProcess(SYNCHRONIZE,FALSE,(DWORD)pid);
00183 if(hProcess)
00184 {
00185 WaitForSingleObject(hProcess,INFINITE);
00186 CloseHandle(hProcess);
00187 }
00188 }
00189 #else
00190 while(wait_for_exit && (kill(pid, 0) == 0))
00191 {
00192 struct timeval tv;
00193 tv.tv_sec = 0;
00194 tv.tv_usec = 100000;
00195 select(0,0,0,0,&tv);
00196 }
00197 #endif
00198 }
00199
00200 int main(int argc, char **argv)
00201 {
00202 TQCString host;
00203
00204 int dont_kill_dcop = (argc == 2) && (strcmp(argv[1], "--nokill") == 0);
00205 int wait_for_exit = (argc == 2) && (strcmp(argv[1], "--wait") == 0);
00206
00207 cleanupDCOP(dont_kill_dcop, wait_for_exit);
00208 return 0;
00209 }