void child_process(int sock, int number) { char fd_buff[16]; char buff[1024]; while (true) { // get fd int fd; socket_fd_read(sock, fd_buff, sizeof(fd_buff), &fd); if (fd == -1) continue; // Work with income socket ssize_t size = recv(fd, buff, sizeof(buff), MSG_NOSIGNAL); if (size == 0) break; buff[size] = '\0'; FILE *log = fopen("/home/box/error.log", "w"); std::string url = parseHttpGet(buff, size); fprintf(log, "URL1: %s\n", url.c_str()); url = parseUrl(url); fprintf(log, "URL2: %s\n", url.c_str()); fprintf(log, "URL3: %s\n", getPostfix(url).c_str()); std::string response; if (getPostfix(url) != "html") { response = HttpResponse404(); } else { std::string filename = directory + url; fprintf(log, "FILE: %s\n", filename.c_str()); if (checkFile(filename)) { std::string content = readFile(filename); response = HttpResponse200(content.c_str(), content.size()); } else { response = HttpResponse404(); } } fprintf(log, "RESPONSE: %s\n", response.c_str()); send(fd, response.c_str(), response.length(), MSG_NOSIGNAL); close(fd); } close(sock); }
static void socket_get_document_list(gint sock) { gchar doc_list[BUFFER_LENGTH]; gint doc_list_len; if (sock < 0) return; socket_fd_write_all(sock, "doclist\n", 8); doc_list_len = socket_fd_read(sock, doc_list, sizeof(doc_list)); if (doc_list_len >= BUFFER_LENGTH) doc_list_len = BUFFER_LENGTH -1; doc_list[doc_list_len] = '\0'; /* if we received ETX (end-of-text), there were no open files, so print only otherwise */ if (! utils_str_equal(doc_list, "\3")) printf("%s", doc_list); }
static gint socket_fd_gets(gint fd, gchar *buf, gint len) { gchar *newline, *bp = buf; gint n; if (--len < 1) return -1; do { if ((n = socket_fd_recv(fd, bp, len, MSG_PEEK)) <= 0) return -1; if ((newline = memchr(bp, '\n', n)) != NULL) n = newline - bp + 1; if ((n = socket_fd_read(fd, bp, n)) < 0) return -1; bp += n; len -= n; } while (! newline && len); *bp = '\0'; return bp - buf; }
static void socket_get_document_list(gint sock) { gchar buf[BUFFER_LENGTH]; gint n_read; if (sock < 0) return; socket_fd_write_all(sock, "doclist\n", 8); do { n_read = socket_fd_read(sock, buf, BUFFER_LENGTH); /* if we received ETX (end-of-text), there is nothing else to read, so cut that * byte not to output it and to be sure not to validate the loop condition */ if (n_read > 0 && buf[n_read - 1] == '\3') n_read--; if (n_read > 0) fwrite(buf, 1, n_read, stdout); } while (n_read >= BUFFER_LENGTH); }
/* (Unix domain) socket support to replace the old FIFO code * (taken from Sylpheed, thanks) * Returns the created socket, -1 if an error occurred or -2 if another socket exists and files * were sent to it. */ gint socket_init(gint argc, gchar **argv) { gint sock; #ifdef G_OS_WIN32 HANDLE hmutex; HWND hwnd; socket_init_win32(); hmutex = CreateMutexA(NULL, FALSE, "Geany"); if (! hmutex) { geany_debug("cannot create Mutex\n"); return -1; } if (GetLastError() != ERROR_ALREADY_EXISTS) { /* To support multiple instances with different configuration directories (as we do on * non-Windows systems) we would need to use different port number s but it might be * difficult to get a port number which is unique for a configuration directory (path) * and which is unused. This port number has to be guessed by the first and new instance * and the only data is the configuration directory path. * For now we use one port number, that is we support only one instance at all. */ sock = socket_fd_open_inet(REMOTE_CMD_PORT); if (sock < 0) return -1; return sock; } sock = socket_fd_connect_inet(REMOTE_CMD_PORT); if (sock < 0) return -1; #else gchar *display_name = NULL; const gchar *hostname = g_get_host_name(); GdkDisplay *display = gdk_display_get_default(); gchar *p; /* On OS X with quartz backend gdk_display_get_name() returns hostname * using [NSHost currentHost] (it could return more or less whatever string * as display name is a X11 specific thing). This call can lead to network * query and block for several seconds so better skip it. */ #ifndef GDK_WINDOWING_QUARTZ if (display != NULL) display_name = g_strdup(gdk_display_get_name(display)); #endif if (display_name == NULL) display_name = g_strdup("NODISPLAY"); /* these lines are taken from dcopc.c in kdelibs */ if ((p = strrchr(display_name, '.')) > strrchr(display_name, ':') && p != NULL) *p = '\0'; /* remove characters that may not be acceptable in a filename */ for (p = display_name; *p; p++) { if (*p == ':' || *p == '/') *p = '_'; } if (socket_info.file_name == NULL) socket_info.file_name = g_strdup_printf("%s%cgeany_socket_%s_%s", app->configdir, G_DIR_SEPARATOR, hostname, display_name); g_free(display_name); /* check whether the real user id is the same as this of the socket file */ check_socket_permissions(); sock = socket_fd_connect_unix(socket_info.file_name); if (sock < 0) { remove_socket_link_full(); /* deletes the socket file and the symlink */ return socket_fd_open_unix(socket_info.file_name); } #endif /* remote command mode, here we have another running instance and want to use it */ /* now we send the command line args */ if (argc > 1) { #ifdef G_OS_WIN32 /* first we send a request to retrieve the window handle and focus the window */ socket_fd_write_all(sock, "window\n", 7); if (socket_fd_read(sock, (gchar *)&hwnd, sizeof(hwnd)) == sizeof(hwnd)) SetForegroundWindow(hwnd); #endif send_open_command(sock, argc, argv); } if (cl_options.list_documents) { socket_get_document_list(sock); } socket_fd_close(sock); return -2; }
/* (Unix domain) socket support to replace the old FIFO code * (taken from Sylpheed, thanks) * Returns the created socket, -1 if an error occurred or -2 if another socket exists and files * were sent to it. */ gint socket_init(gint argc, gchar **argv) { gint sock; #ifdef G_OS_WIN32 HANDLE hmutex; HWND hwnd; socket_init_win32(); hmutex = CreateMutexA(NULL, FALSE, "Geany"); if (! hmutex) { geany_debug("cannot create Mutex\n"); return -1; } if (GetLastError() != ERROR_ALREADY_EXISTS) { /* To support multiple instances with different configuration directories (as we do on * non-Windows systems) we would need to use different port number s but it might be * difficult to get a port number which is unique for a configuration directory (path) * and which is unused. This port number has to be guessed by the first and new instance * and the only data is the configuration directory path. * For now we use one port number, that is we support only one instance at all. */ sock = socket_fd_open_inet(REMOTE_CMD_PORT); if (sock < 0) return 0; return sock; } sock = socket_fd_connect_inet(REMOTE_CMD_PORT); if (sock < 0) return -1; #else gchar *display_name = gdk_get_display(); gchar *hostname = utils_get_hostname(); gchar *p; if (display_name == NULL) display_name = g_strdup("NODISPLAY"); /* these lines are taken from dcopc.c in kdelibs */ if ((p = strrchr(display_name, '.')) > strrchr(display_name, ':') && p != NULL) *p = '\0'; while ((p = strchr(display_name, ':')) != NULL) *p = '_'; if (socket_info.file_name == NULL) socket_info.file_name = g_strdup_printf("%s%cgeany_socket_%s_%s", app->configdir, G_DIR_SEPARATOR, hostname, display_name); g_free(display_name); g_free(hostname); /* check whether the real user id is the same as this of the socket file */ check_socket_permissions(); sock = socket_fd_connect_unix(socket_info.file_name); if (sock < 0) { remove_socket_link_full(); /* deletes the socket file and the symlink */ return socket_fd_open_unix(socket_info.file_name); } #endif /* remote command mode, here we have another running instance and want to use it */ #ifdef G_OS_WIN32 /* first we send a request to retrieve the window handle and focus the window */ socket_fd_write_all(sock, "window\n", 7); if (socket_fd_read(sock, (gchar *)&hwnd, sizeof(hwnd)) == sizeof(hwnd)) SetForegroundWindow(hwnd); #endif /* now we send the command line args */ if (argc > 1) { send_open_command(sock, argc, argv); } if (cl_options.list_documents) { socket_get_document_list(sock); } socket_fd_close(sock); return -2; }