int main(int argc, char *argv[]) { set_process_name("ivpn-client"); parse_options(argc, argv); if (geteuid() != 0) { lerr("Effective UID is %u. It need to be 0.", geteuid()); return EXIT_FAILURE; } if (install_sigchld_handler() == 0) return EXIT_FAILURE; if (username == 0) { username = get_current_user(); if (username == 0) exit(EXIT_FAILURE); } if (!sslutil_init(CA_CERT_FILE, 0, 0)) return EXIT_SSL_ERROR; return run_client(); }
void set_progname(const char *progname, int argc, char **argv, int copy_index) { int argvlen = 0; int proglen = strlen(progname) + 1; int i; for (i = 0; i < argc; i++) argvlen += strlen(argv[i]) + 1; if (proglen > argvlen) proglen = argvlen; memmove(argv[0], progname, proglen); if (copy_index > 0) { int j; for (j = 0; j + copy_index < argc; j++) { int arglen = strlen(argv[j + copy_index]) + 1; argv[j + 1] = argv[0] + proglen; memmove(argv[j + 1], argv[j + copy_index], arglen); proglen += arglen; } } memset(&argv[0][proglen], 0, argvlen - proglen); set_process_name(progname); setenv("_", progname, true); }
void ProcessState::setArgV0(const char* txt) { if (mArgV != NULL) { strncpy((char*)mArgV[0], txt, mArgLen); set_process_name(txt); } }
void CUtils::set_process_name(const char* format, ...) { va_list args; va_start(args, format); utils::VaListHelper vlh(args); char name[NAME_MAX]; vsnprintf(name, sizeof(name), format, args); set_process_name(std::string(name)); }
/* * Main entry point. Decide where to go. */ int main(int argc, char* const argv[]) { set_process_name("dexopt"); setvbuf(stdout, NULL, _IONBF, 0); if (argc > 1) { if (strcmp(argv[1], "--zip") == 0) return fromZip(argc, argv); else if (strcmp(argv[1], "--dex") == 0) return fromDex(argc, argv); else if (strcmp(argv[1], "--preopt") == 0) return preopt(argc, argv); } fprintf(stderr, "Usage:\n\n" "Short version: Don't use this.\n\n" "Slightly longer version: This system-internal tool is used to\n" "produce optimized dex files. See the source code for details.\n"); return 1; }
static void * pthread_broadcast_send() { set_process_name("xy: broadcast"); int len; char *buffer, *padded_buffer; ssize_t rc; for (;;) { pthread_mutex_lock(&mutex); pthread_cond_wait(&cond, &mutex); if (messages == NULL) { pthread_mutex_unlock(&mutex); continue; } MESSAGE *current, *message = messages->head; while (message) { buffer = message->msg; len = strlen(buffer); padded_buffer = malloc(BROADCAST_LENGTH); sprintf(padded_buffer, "%s%*s", buffer, (BROADCAST_LENGTH - (len + 1)), ""); rc = sendto(endpt->sd, padded_buffer, len, 0, endpt->dest, endpt->size); if (rc == -1) { const char *msg = "failed to broadcast message, shutting down?"; log_warn(globals->log, msg); } current = message; message = message->next; free(padded_buffer); free(current->msg); free(current); } free(messages); messages = NULL; pthread_mutex_unlock(&mutex); } return NULL; }
/* * retrieve udp table for win xp and up */ DWORD windows_get_udp_table(struct connection_table **table_connection) { DWORD result = ERROR_SUCCESS; struct connection_entry * current_connection = NULL; MIB_UDPTABLE_OWNER_MODULE * tablev4 = NULL; MIB_UDP6TABLE_OWNER_MODULE * tablev6 = NULL; MIB_UDPROW_OWNER_MODULE * currentv4 = NULL; MIB_UDP6ROW_OWNER_MODULE * currentv6 = NULL; DWORD i, dwSize; ptr_GetExtendedUdpTable geut = NULL; geut = (ptr_GetExtendedTcpTable)GetProcAddress(GetModuleHandle("iphlpapi"), "GetExtendedUdpTable"); // systems that don't support GetExtendedUdpTable if (geut == NULL) { return windows_get_udp_table_win2000_down(table_connection); } do { // IPv4 part dwSize = 0; if (geut(NULL,&dwSize, TRUE, AF_INET, UDP_TABLE_OWNER_MODULE, 0) == ERROR_INSUFFICIENT_BUFFER) { tablev4 = (MIB_UDPTABLE_OWNER_MODULE *)malloc(dwSize); if (geut(tablev4, &dwSize, TRUE, AF_INET, UDP_TABLE_OWNER_MODULE, 0) == NO_ERROR) { for(i=0; i<tablev4->dwNumEntries; i++) { // check available memory and allocate if necessary if (check_and_allocate(table_connection) == ERROR_NOT_ENOUGH_MEMORY) { free(tablev4); return ERROR_NOT_ENOUGH_MEMORY; } // GetExtendedUdpTable reports only listening UDP sockets, not "active" ones currentv4 = &tablev4->table[i]; current_connection = &(*table_connection)->table[(*table_connection)->entries]; current_connection->type = AF_INET; current_connection->local_addr.addr = currentv4->dwLocalAddr; current_connection->remote_addr.addr = 0; current_connection->local_port = ntohs((u_short)(currentv4->dwLocalPort & 0x0000ffff)); current_connection->remote_port = 0; strncpy(current_connection->state, "", sizeof(current_connection->state)); strncpy(current_connection->protocol, "udp", sizeof(current_connection->protocol)); // force program_name to "-" and try to get real name through GetOwnerModuleFromXXXEntry strncpy(current_connection->program_name, "-", sizeof(current_connection->program_name)); set_process_name(currentv4->dwOwningPid, current_connection->program_name, sizeof(current_connection->program_name)); (*table_connection)->entries++; } } else { // geut failed result = GetLastError(); if (tablev4) free(tablev4); break; } if (tablev4) free(tablev4); } // IPv6 part dwSize = 0; if (geut(NULL,&dwSize, TRUE, AF_INET6, UDP_TABLE_OWNER_MODULE, 0) == ERROR_INSUFFICIENT_BUFFER) { tablev6 = (MIB_UDP6TABLE_OWNER_MODULE *)malloc(dwSize); if (geut(tablev6, &dwSize, TRUE, AF_INET6, UDP_TABLE_OWNER_MODULE, 0) == NO_ERROR) { for(i=0; i<tablev6->dwNumEntries; i++) { // check available memory and allocate if necessary if (check_and_allocate(table_connection) == ERROR_NOT_ENOUGH_MEMORY) { free(tablev6); return ERROR_NOT_ENOUGH_MEMORY; } currentv6 = &tablev6->table[i]; current_connection = &(*table_connection)->table[(*table_connection)->entries]; current_connection->type = AF_INET6; memcpy(¤t_connection->local_addr.addr6, currentv6->ucLocalAddr, sizeof(current_connection->local_addr.addr6)); memset(¤t_connection->remote_addr.addr6, 0, sizeof(current_connection->remote_addr.addr6)); current_connection->local_port = ntohs((u_short)(currentv6->dwLocalPort & 0x0000ffff)); current_connection->remote_port = 0; strncpy(current_connection->state, "", sizeof(current_connection->state)); strncpy(current_connection->protocol, "udp6", sizeof(current_connection->protocol)); // force program_name to "-" and try to get real name through GetOwnerModuleFromXXXEntry strncpy(current_connection->program_name, "-", sizeof(current_connection->program_name)); set_process_name(currentv6->dwOwningPid, current_connection->program_name, sizeof(current_connection->program_name)); (*table_connection)->entries++; } } else { // gett failed result = GetLastError(); if (tablev6) free(tablev6); break; } if (tablev6) free(tablev6); } } while (0); return result; }
/* * retrieve tcp table for win xp and up */ DWORD windows_get_tcp_table(struct connection_table **table_connection) { DWORD result = ERROR_SUCCESS; struct connection_entry * current_connection = NULL; MIB_TCPTABLE_OWNER_MODULE * tablev4 = NULL; MIB_TCP6TABLE_OWNER_MODULE * tablev6 = NULL; MIB_TCPROW_OWNER_MODULE * currentv4 = NULL; MIB_TCP6ROW_OWNER_MODULE * currentv6 = NULL; DWORD i, state, dwSize; ptr_GetExtendedTcpTable gett = NULL; gett = (ptr_GetExtendedTcpTable)GetProcAddress(GetModuleHandle("iphlpapi"), "GetExtendedTcpTable"); // systems that don't support GetExtendedTcpTable if (gett == NULL) { return windows_get_tcp_table_win2000_down(table_connection); } do { // IPv4 part dwSize = 0; if (gett(NULL,&dwSize, TRUE, AF_INET, TCP_TABLE_OWNER_MODULE_ALL, 0) == ERROR_INSUFFICIENT_BUFFER) { tablev4 = (MIB_TCPTABLE_OWNER_MODULE *)malloc(dwSize); if (gett(tablev4, &dwSize, TRUE, AF_INET, TCP_TABLE_OWNER_MODULE_ALL, 0) == NO_ERROR) { for(i=0; i<tablev4->dwNumEntries; i++) { // check available memory and allocate if necessary if (check_and_allocate(table_connection) == ERROR_NOT_ENOUGH_MEMORY) { free(tablev4); return ERROR_NOT_ENOUGH_MEMORY; } currentv4 = &tablev4->table[i]; current_connection = &(*table_connection)->table[(*table_connection)->entries]; current_connection->type = AF_INET; current_connection->local_addr.addr = currentv4->dwLocalAddr; current_connection->remote_addr.addr = currentv4->dwRemoteAddr; current_connection->local_port = ntohs((u_short)(currentv4->dwLocalPort & 0x0000ffff)); // if socket is in LISTEN, remote_port is garbage, force value to 0 if (currentv4->dwState == MIB_TCP_STATE_LISTEN) current_connection->remote_port = 0; else current_connection->remote_port = ntohs((u_short)(currentv4->dwRemotePort & 0x0000ffff)); state = currentv4->dwState; if ((state <= 0) || (state > 12)) state = 13; // points to UNKNOWN in the state array strncpy(current_connection->state, tcp_connection_states[state], sizeof(current_connection->state)); strncpy(current_connection->protocol, "tcp", sizeof(current_connection->protocol)); // force program_name to "-" and try to get real name through GetOwnerModuleFromXXXEntry strncpy(current_connection->program_name, "-", sizeof(current_connection->program_name)); set_process_name(currentv4->dwOwningPid, current_connection->program_name, sizeof(current_connection->program_name)); (*table_connection)->entries++; } } else { // gett failed result = GetLastError(); if (tablev4) free(tablev4); break; } if (tablev4) free(tablev4); } // IPv6 part dwSize = 0; if (gett(NULL,&dwSize, TRUE, AF_INET6, TCP_TABLE_OWNER_MODULE_ALL, 0) == ERROR_INSUFFICIENT_BUFFER) { tablev6 = (MIB_TCP6TABLE_OWNER_MODULE *)malloc(dwSize); if (gett(tablev6, &dwSize, TRUE, AF_INET6, TCP_TABLE_OWNER_MODULE_ALL, 0) == NO_ERROR) { for(i=0; i<tablev6->dwNumEntries; i++) { // check available memory and allocate if necessary if (check_and_allocate(table_connection) == ERROR_NOT_ENOUGH_MEMORY) { free(tablev6); return ERROR_NOT_ENOUGH_MEMORY; } currentv6 = &tablev6->table[i]; current_connection = &(*table_connection)->table[(*table_connection)->entries]; current_connection->type = AF_INET6; memcpy(¤t_connection->local_addr.addr6, currentv6->ucLocalAddr, sizeof(current_connection->local_addr.addr6)); memcpy(¤t_connection->remote_addr.addr6, currentv6->ucRemoteAddr, sizeof(current_connection->remote_addr.addr6)); current_connection->local_port = ntohs((u_short)(currentv6->dwLocalPort & 0x0000ffff)); // if socket is in LISTEN, remote_port is garbage, force value to 0 if (currentv6->dwState == MIB_TCP_STATE_LISTEN) current_connection->remote_port = 0; else current_connection->remote_port = ntohs((u_short)(currentv6->dwRemotePort & 0x0000ffff)); state = currentv6->dwState; if ((state <= 0) || (state > 12)) state = 13; // points to UNKNOWN in the state array strncpy(current_connection->state, tcp_connection_states[state], sizeof(current_connection->state)); strncpy(current_connection->protocol, "tcp6", sizeof(current_connection->protocol)); // force program_name to "-" and try to get real name through GetOwnerModuleFromXXXEntry strncpy(current_connection->program_name, "-", sizeof(current_connection->program_name)); set_process_name(currentv6->dwOwningPid, current_connection->program_name, sizeof(current_connection->program_name)); (*table_connection)->entries++; } } else { // gett failed result = GetLastError(); if (tablev6) free(tablev6); break; } if (tablev6) free(tablev6); } } while (0); return result; }
/** Set the process name */ void setProcessName(const char* name) { memset(argBlockStart, 0, argBlockLength); strlcpy(argBlockStart, name, argBlockLength); set_process_name(name); }
void xy_startup() { globals = malloc(sizeof(GLOBALS)); if (!logging_init()) { fprintf(stderr, INIT_LOGGING_FAILURE); exit(1); } globals->log = get_logger("xy"); log_info(globals->log, STARTUP_MSG); xy_dir_init(); globals->cfg = xy_rc_init(); xy_inotify_init(); if (globals->in_fd == -1) { DIE_MSG("xy_inotify_init failed"); } if (!ipc_init()) { fprintf(stderr, INIT_IPC_FAILURE); DIE; } log_info(globals->log, IPC_STARTUP_MSG); if (!broadcast_init()) { fprintf(stderr, INIT_BROADCAST_FAILURE); DIE; } log_info(globals->log, BROADCAST_STARTUP_MSG); broadcast_send(STARTUP_MSG); /* if (!is_xinerama_active(globals->dpy)) { fprintf(stderr, "Xinerama is not active\n"); DIE; } */ /* char buffer[MSG_LEN]; memset(buffer, 0, MSG_LEN); sprintf(buffer, DISPLAYS_FOUND, *global_num_screens); broadcast_send(buffer); memset(buffer, 0, MSG_LEN); for (int i = 0; i < *global_num_screens; i++) { int sn = global_screens[i].screen_number; int xorg = global_screens[i].x_org; int yorg = global_screens[i].y_org; int width = global_screens[i].width; int height = global_screens[i].height; sprintf(buffer, DISPLAY_MESSAGE, sn, xorg, yorg, width, height); broadcast_send(buffer); memset(buffer, 0, MSG_LEN); } */ // TODO grab keys xy_init(); set_process_name("xy: main"); }