/** * @brief check process is unique * * @param name [in] process name * * @return pid of already running, if process unique; \ * otherwise return 0 */ int is_proc_unique(const char *proc_name) { struct dirent *dir = NULL; DIR *dirp = NULL; pid_t pid0 = get_pid_by_name(proc_name); pid_t pid1 = -1; FILE *fp = NULL; char name[64] = {0}; char path[128] = {0}; if ((dirp = opendir("/proc/")) == NULL) return -1; while ((dir = readdir(dirp)) != NULL) { if (dir->d_name && (atoi(dir->d_name)) > 0) { sprintf(path, "/proc/%s/status", dir->d_name); if ((fp = fopen(path, "r")) == NULL) continue; if (fscanf(fp, "Name:%s", name) == -1) name[0] = '\0'; if (fp != NULL) fclose(fp); if (strcmp(name, proc_name)) continue; pid1 = atoi(dir->d_name); if (pid0 != pid1) break; } } if (dirp != NULL) closedir(dirp); if (pid0 != pid1) return pid1; return 0; }
int main(int argc, char **argv) { unsigned int target_pid; if (argc != 2) { fprintf(stderr, "argument error, i.e. %s test_pro\n", argv[0]); return -1; } target_pid = get_pid_by_name(argv[1]); if (target_pid) printf("program %s's pid: %u\n", argv[1], target_pid); return 0; }
int mc_client_command_parser (mc_connection *mc, char *buf) { int i; int x1, y1, z1; int pid, type; if (!strncmp(buf + 1, "tp", 2)) { for (i = 4; i < 66; i++) if (buf[i] == ' ') buf[i] = 0; pid = get_pid_by_name(mc->mc_server_data, buf + 4); if (pid >= 0) { x1 = mc->mc_server_data.mc_player_db[pid].x; y1 = mc->mc_server_data.mc_player_db[pid].y; z1 = mc->mc_server_data.mc_player_db[pid].z; printf("Player %s at %d, %d, %d\n", buf + 4, x1, y1, z1); mc_proto_send_pos(mc, x1, y1, z1, 0, 0); } else { printf("Could not find player \"%s\"\n", buf + 4); } } else if (buf[1] == 'z') { if (strlen(buf) < 3 || sscanf(&buf[3], "%d", &type) != 1) mc->mc_mode |= MC_USE_TYPE; else mc->mc_server_data.type = type; printf("Place two blocks to define the cuboid\n"); mc->mc_mode |= MC_WAITING_FOR_1; } else if (buf[1] == 'n') { if (!(mc->mc_mode & MC_HIDE_POSITION)) { mc->mc_mode |= MC_HIDE_POSITION; printf("No longer sending position updates\n"); } else { mc->mc_mode &= ~MC_HIDE_POSITION; printf("Sending position updates\n"); } } else { printf("Invalid command %s\n", buf + 1); } return 0; }
void *mc_command_run_listener (void *arg) { mc_connection *mc = (mc_connection *)arg; char buffer[1024]; int x1, y1, z1, x2, y2, z2, type, pid; while (1) { fgets(buffer, sizeof(buffer), stdin); buffer[strlen(buffer) - 1] = '\0'; switch (buffer[0]) { case 'k': pid = get_pid_by_name(mc->mc_server_data, buffer + 2); if (pid >= 0) { x1 = mc->mc_server_data.mc_player_db[pid].x; y1 = mc->mc_server_data.mc_player_db[pid].y; z1 = mc->mc_server_data.mc_player_db[pid].z; printf("Player %s at %d, %d, %d\n", buffer + 2, x1, y1, z1); } else { printf("Could not find player %s\n", buffer + 2); } break; case 's': mc_proto_send_chat(mc, &buffer[2]); break; case 'l': if (guard(mc, buffer) < 0) printf("Invalid arguments\n"); break; case 'c': if (sscanf(&buffer[2], "%d %d %d %d %d %d %d", &type, &x1, &y1, &z1, &x2, &y2, &z2) != 7) { printf("Invalid coordinates\n"); break; } cuboid(mc, type, x1, y1, z1, x2, y2, z2); break; case 'z':; int times; if (sscanf(&buffer[2], "%d %d %d %d", ×, &x1, &y1, &z1) != 4) { printf("Invalid coordinates\n"); break; } int i; for (i = 0; i < times; i++) { mc_proto_send_pos(mc, x1, y1, z1, arc4random() % 256, arc4random() % 256); usleep(10000); } break; case 'f': if (!(mc->mc_mode & MC_FOLLOW_PLAYER)) { pid = get_pid_by_name(mc->mc_server_data, buffer + 2); if (pid >= 0) { mc->mc_server_data.mc_pid_locked = pid; printf("Following player %s\n", buffer + 2); mc->mc_mode |= MC_FOLLOW_PLAYER; } else { printf("Could not find player %s\n", buffer + 2); } } else { mc->mc_mode &= ~MC_FOLLOW_PLAYER; printf("No longer following\n"); } break; default: printf("Invalid option '%c'\n", buffer[0]); } } return NULL; }
int guard (mc_connection *mc, char *buffer) { int mode, pid; struct mc_server_data *data; if (strlen(buffer) < 2) return -1; mode = buffer[1]; data = &mc->mc_server_data; if (mode == GUARD_MAP) { if (!(mc->mc_mode & MC_LOCK_MAP)) { mc->mc_mode |= MC_LOCK_MAP; printf("Locked map\n"); if (mc->mc_mode & MC_LOCK_AREA) { mc->mc_mode &= ~MC_LOCK_AREA; printf("Unlocked area\n"); } if (mc->mc_mode & (MC_LOCK_PLAYER|MC_FOLLOW_PLAYER)) { mc->mc_mode &= ~(MC_LOCK_PLAYER|MC_FOLLOW_PLAYER); printf("Unlocked player %s\n", get_name_by_pid(data, data->mc_pid_locked)); } } else { mc->mc_mode &= ~MC_LOCK_MAP; printf("Unlocked map\n"); } } else if (mode == GUARD_PLAYER) { if (!(mc->mc_mode & (MC_LOCK_PLAYER|MC_FOLLOW_PLAYER))) { if (strlen(buffer) < 4) return -1; pid = get_pid_by_name(mc->mc_server_data, buffer + 3); mc->mc_server_data.mc_pid_locked = pid; if (pid < 0) return -1; printf("Locked player %s\n", buffer + 3); mc->mc_mode |= (MC_LOCK_PLAYER|MC_FOLLOW_PLAYER); if (mc->mc_mode & MC_LOCK_AREA) { mc->mc_mode &= ~MC_LOCK_AREA; printf("Unlocked area\n"); } if (mc->mc_mode & MC_LOCK_MAP) { mc->mc_mode &= ~MC_LOCK_MAP; printf("Unlocked map\n"); } } else { mc->mc_mode &= ~(MC_LOCK_PLAYER|MC_FOLLOW_PLAYER); printf("Unlocked player %s\n", get_name_by_pid(data, data->mc_pid_locked)); } } else if (mode == GUARD_AREA) { if (!(mc->mc_mode & MC_LOCK_AREA)) { if (sscanf(&buffer[3], "%d %d %d %d %d %d", &data->x1, &data->y1, &data->z1, &data->x2, &data->y2, &data->z2) != 6) { printf("Invalid coordinates\n"); return -1; } CHECK_ORDER(data->x1, data->x2); CHECK_ORDER(data->y1, data->y2); CHECK_ORDER(data->z1, data->z2); printf("Locked area (%d, %d, %d) -> (%d, %d, %d)\n", data->x1, data->y1, data->z1, data->x2, data->y2, data->z2); mc->mc_mode |= MC_LOCK_AREA; if (mc->mc_mode & MC_LOCK_MAP) { mc->mc_mode &= ~MC_LOCK_MAP; printf("Unlocked map\n"); } if (mc->mc_mode & (MC_LOCK_PLAYER|MC_FOLLOW_PLAYER)) { mc->mc_mode &= ~(MC_LOCK_PLAYER|MC_FOLLOW_PLAYER); printf("Unlocked player %s\n", get_name_by_pid(data, data->mc_pid_locked)); } } else { mc->mc_mode &= ~MC_LOCK_AREA; printf("Unlocked area\n"); } } else { return -1; } return 0; }