Exemple #1
0
void exit(int status)
{
    printf("We are in exit\n");
//    char * value = getenv("PIPE_READ");
    print_maps();
    void __attribute__ ((noreturn)) (*real_exit)(int) = dlsym(RTLD_NEXT, "exit");
    real_exit(status);
}
Exemple #2
0
void * escucharServidor(void * args) {

	int r;
	char buffer[MAXBUF];

	while (partida.estado == EnProgreso) {

		bzero(buffer, MAXBUF);

		// Recibe jugadas que le reenvia el servidor
		r = recv(sockfd, buffer, sizeof(struct MensajeNIPC), 0);

		if (r == -1) {
			perror("Error al recibir el mensaje.\n");
			abort();
		}

		struct MensajeNIPC * mensaje;

		mensaje = (struct MensajeNIPC *) buffer;

		switch (mensaje->tipo) {

		case Recibe_Ataque:

			if (handler_ataque(mensaje) == -1) {
				printf("Error al procesar el ataque.\n");
				abort();
			}

			break;

		case Resultado:
			handler_respuesta(mensaje);
			break;

		case Fin_partida:
			handler_fin(mensaje);
			partida.estado = Terminada;

			break;

		case Turno:

			printf("Mensaje turno.\n");
			turno = 1;

			system("clear");

			print_maps();

			printf("Ingrese coordenadas:\n");

			break;
		}
	}
}
Exemple #3
0
static void lsof_dumpinfo(pid_t pid)
{
    int fd;
    struct pid_info_t info;
    struct stat pidstat;
    struct passwd *pw;

    info.pid = pid;
    snprintf(info.path, sizeof(info.path), "/proc/%d/", pid);
    info.parent_length = strlen(info.path);

    // Get the UID by calling stat on the proc/pid directory.
    if (!stat(info.path, &pidstat)) {
        pw = getpwuid(pidstat.st_uid);
        if (pw) {
            strlcpy(info.user, pw->pw_name, sizeof(info.user));
        } else {
            snprintf(info.user, USER_DISPLAY_MAX, "%d", (int)pidstat.st_uid);
        }
    } else {
        strcpy(info.user, "???");
    }

    // Read the command line information; each argument is terminated with NULL.
    strlcat(info.path, "cmdline", sizeof(info.path));
    fd = open(info.path, O_RDONLY);
    if (fd < 0) {
        fprintf(stderr, "Couldn't read %s\n", info.path);
        return;
    }

    char cmdline[PATH_MAX];
    int numRead = read(fd, cmdline, sizeof(cmdline) - 1);
    close(fd);

    if (numRead < 0) {
        fprintf(stderr, "Error reading cmdline: %s: %s\n", info.path, strerror(errno));
        return;
    }

    cmdline[numRead] = '\0';

    // We only want the basename of the cmdline
    strlcpy(info.cmdline, basename(cmdline), sizeof(info.cmdline));

    // Read each of these symlinks
    print_type("cwd", &info);
    print_type("exe", &info);
    print_type("root", &info);

    print_fds(&info);
    print_maps(&info);
}
Exemple #4
0
/*
 * transfer_all_new_dbs()
 *
 * Responsible for upgrading all database. invokes routines to generate mappings and then
 * physically link the databases.
 */
void
transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
					 char *old_pgdata, char *new_pgdata, char *old_tablespace)
{
	int			old_dbnum,
				new_dbnum;

	/* Scan the old cluster databases and transfer their files */
	for (old_dbnum = new_dbnum = 0;
		 old_dbnum < old_db_arr->ndbs;
		 old_dbnum++, new_dbnum++)
	{
		DbInfo	   *old_db = &old_db_arr->dbs[old_dbnum],
				   *new_db = NULL;
		FileNameMap *mappings;
		int			n_maps;

		/*
		 * Advance past any databases that exist in the new cluster but not in
		 * the old, e.g. "postgres".  (The user might have removed the
		 * 'postgres' database from the old cluster.)
		 */
		for (; new_dbnum < new_db_arr->ndbs; new_dbnum++)
		{
			new_db = &new_db_arr->dbs[new_dbnum];
			if (strcmp(old_db->db_name, new_db->db_name) == 0)
				break;
		}

		if (new_dbnum >= new_db_arr->ndbs)
			pg_fatal("old database \"%s\" not found in the new cluster\n",
					 old_db->db_name);

		mappings = gen_db_file_maps(old_db, new_db, &n_maps, old_pgdata,
									new_pgdata);
		if (n_maps)
		{
			print_maps(mappings, n_maps, new_db->db_name);

			transfer_single_new_db(mappings, n_maps, old_tablespace);
		}
		/* We allocate something even for n_maps == 0 */
		pg_free(mappings);
	}

	return;
}
Exemple #5
0
/*
 * transfer_all_new_dbs()
 *
 * Responsible for upgrading all database. invokes routines to generate mappings and then
 * physically link the databases.
 */
const char *
transfer_all_new_dbs(DbInfoArr *old_db_arr,
				   DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata)
{
	int			dbnum;
	const char *msg = NULL;

	prep_status("Restoring user relation files\n");

	if (old_db_arr->ndbs != new_db_arr->ndbs)
		pg_log(PG_FATAL, "old and new clusters have a different number of databases\n");

	for (dbnum = 0; dbnum < old_db_arr->ndbs; dbnum++)
	{
		DbInfo	   *old_db = &old_db_arr->dbs[dbnum];
		DbInfo	   *new_db = &new_db_arr->dbs[dbnum];
		FileNameMap *mappings;
		int			n_maps;
		pageCnvCtx *pageConverter = NULL;

		if (strcmp(old_db->db_name, new_db->db_name) != 0)
			pg_log(PG_FATAL, "old and new databases have different names: old \"%s\", new \"%s\"\n",
				   old_db->db_name, new_db->db_name);

		n_maps = 0;
		mappings = gen_db_file_maps(old_db, new_db, &n_maps, old_pgdata,
									new_pgdata);

		if (n_maps)
		{
			print_maps(mappings, n_maps, new_db->db_name);

#ifdef PAGE_CONVERSION
			msg = setupPageConverter(&pageConverter);
#endif
			transfer_single_new_db(pageConverter, mappings, n_maps);

			pg_free(mappings);
		}
	}

	prep_status("");			/* in case nothing printed */
	check_ok();

	return msg;
}
Exemple #6
0
int handler_respuesta(struct MensajeNIPC * mensaje) {

	struct Resultado_Ataque resultado;
	char simbolo;

	memcpy(&resultado, mensaje->payload, mensaje->payload_length);

	if (resultado.resultado == Agua) {
		simbolo = 'c';
	} else {
		simbolo = 'b';
	}

	// Actualizo la interfaz grafica remota
	int x, y;

	x = resultado.coordenadas[0] - VALUE;
	y = resultado.coordenadas[1] - VALUE;

	remote_matrix[x][y] = simbolo;

	system("clear");

	print_maps();

	if (resultado.resultado == Agua) {
		printf("Resultado de ataque a coordenadas %s: Agua.\n",
				resultado.coordenadas);
	} else {
		printf("Resultado de ataque a coordenadas %s: Hundido.\n",
				resultado.coordenadas);
	}

	if (turno) {
		printf("Ingrese coordenadas:\n");
	} else {
		printf("Esperando jugada del oponente...\n");
	}

	return 0;
}
Exemple #7
0
static void lsof_dumpinfo(pid_t pid)
{

    memset(&info, 0, sizeof(info));
	
    info.pid = pid;
    snprintf(info.path, sizeof(info.path), "/proc/%d/", pid);
    info.parent_length = strlen(info.path);

    /* read from stat file instead of cmdline, since reading from cmdline requires scheduling 
         and cannot function in panic */
    get_cmd(pid, info.cmdline, CMD_DISPLAY_MAX+2);

    // Read each of these symlinks
    print_type("cwd", &info);
    print_type("exe", &info);
    print_type("root", &info);

    print_fds(&info);
    print_maps(&info);
}
Exemple #8
0
int handler_ataque(struct MensajeNIPC * mensaje) {

	int i, hundido = 0, repetido = 0;

	// Mensaje que voy a enviar informando el resultado
	struct Resultado_Ataque resultado_ataq;

	// Se fija si las coordenadas del ataque coinciden con alguna posicion
	for (i = 0; i < cantidad_barcos; i++) {

		if (strcmp(posiciones_barcos[i], mensaje->payload) == 0) {
			hundido = 1;
		}
	}

	// Se fija que ese barco no estuviera hundido
	for (i = 0; i < cantidad_barcos_hundidos; i++) {

		if (strcmp(posiciones_barcos_hundidos[i], mensaje->payload) == 0) {
			repetido = 1;
		}
	}

	if (hundido && !repetido) {

		// Seteo mensaje de respuesta
		resultado_ataq.resultado = Hundido;

		// Guardo la posicion del barco undido e incremento la cantidad de undidos

		strcpy(posiciones_barcos_hundidos[cantidad_barcos_hundidos],
				mensaje->payload);
		cantidad_barcos_hundidos++;

		// Actualizo la interfaz grafica
		int x, y;

		x = mensaje->payload[0] - VALUE;
		y = mensaje->payload[1] - VALUE;

		// Cambio la 'X' por '.'
		my_matrix[x][y] = 'a';

		system("clear");

		print_maps();

		printf("Se recibio un ataque a coordenadas %s... Hundido.\n",
				mensaje->payload);
	} else {

		// Seteo respuesta del mensaje
		resultado_ataq.resultado = Agua;

		system("clear");

		print_maps();

		printf("Se recibio un ataque a coordenadas %s... Agua.\n",
				mensaje->payload);

	}

	if (turno) {
		printf("Ingrese coordenadas:\n");
	} else {
		printf("Esperando jugada del oponente...\n");
	}

	// Envio el mensaje con la respuesta de la jugada

	strcpy(resultado_ataq.coordenadas, mensaje->payload);

	struct MensajeNIPC mensaje_resultado;
	mensaje_resultado.tipo = Resultado;
	mensaje_resultado.payload_length = sizeof(struct Resultado_Ataque);
	memcpy(mensaje_resultado.payload, &resultado_ataq,
			sizeof(struct Resultado_Ataque));
	mensaje_resultado.jugadorOrigen = jugador;
	mensaje_resultado.jugadorDestino = partida.jugadorDestino;

	int s = send(sockfd, &mensaje_resultado, sizeof(struct MensajeNIPC), 0);

	if (s == -1) {
		perror("Error al enviar el mensaje.\n");
		return -1;
	}

	if (cantidad_barcos_hundidos == cantidad_barcos) {
		// Logica de fin de la partida

		struct MensajeNIPC mensaje_fin_partida;
		mensaje_fin_partida.tipo = Fin_partida;
		mensaje_fin_partida.payload_length = sizeof(struct Jugador);

		// Envio el jugador ganador

		memcpy(mensaje_fin_partida.payload, &partida.jugadorDestino,
				sizeof(struct Jugador));

		mensaje_fin_partida.jugadorOrigen = jugador;
		mensaje_fin_partida.jugadorDestino = partida.jugadorDestino;

		int s = send(sockfd, &mensaje_fin_partida, sizeof(struct MensajeNIPC),
				0);

		if (s == -1) {
			perror("Error al enviar el mensaje.\n");
			return -1;
		}
	}

	return 0;
}
Exemple #9
0
void * leerJugada(void * args) {

	char pos[10];

	int s, coords_invalidas = 0, inicio = 1;

	bzero(pos, 10);

	while (partida.estado == EnProgreso) {

		print_maps();

		// Muestro mensaje de validacion
		if (coords_invalidas) {

			// Si es el inicio de la partida no muestro ningun mensaje
			if (inicio) {
				inicio = 0;
			} else {
				printf("Las coordenadas ingresadas no son validas.\n");
			}
		}

		// Mustro mensaje segun si el jugador tiene el turno o no
		if (turno) {
			printf("Ingrese coordenadas:\n");
		} else {
			printf("Esperando jugada del oponente...\n");
		}

		// Obtengo input (bloqueo)
		gets(pos);

		system("clear");

		// Si no es el turno del jugador no hago nada aun
		if (!turno) {
			continue;
		}

		//Validacion coordenadas
		if (strlen(pos) != 2) {
			coords_invalidas = 1;
			continue;
		}

		if (pos[0] < '0' || pos[0] > '9' || pos[1] < '0' || pos[1] > '9') {
			coords_invalidas = 1;
			continue;
		}

		coords_invalidas = 0;

		// Fin del turno, debera esperar a que el server lo habilite para jugar de nuevo
		turno = 0;

		// Enviar jugada
		struct MensajeNIPC mensajeJugada;
		mensajeJugada.tipo = Jugada;
		mensajeJugada.jugadorOrigen = partida.jugadorOrigen;
		mensajeJugada.jugadorDestino = partida.jugadorDestino;
		mensajeJugada.payload_length = strlen(pos);
		strcpy(mensajeJugada.payload, pos);

		s = send(sockfd, &mensajeJugada, sizeof(struct MensajeNIPC), 0);

		if (s == -1) {
			perror("Error al enviar el mensaje.\n");
			abort();
		}
	}
}
Exemple #10
0
option parse_options(int argc, char *argv[])
{
	option op;
	int c, i;

	op.nx = op.ny = 301;
	op.nz = 0;
	op.eig = 0;
	op.problem = SIN;
	op.map = MAP_IDENTITY;
	op.periodic = -1;

	opterr = 0;

	while ((c = getopt(argc, argv, "x:y:z:m:p:l:w:s:d:e")) != -1) {
		switch (c) {
		case 'e':
			op.eig = 1;
			break;
		case 'x':
			op.nx = atoi(optarg);
			break;
		case 'y':
			op.ny = atoi(optarg);
			break;
		case 'z':
			op.nz = atoi(optarg);
			break;
		case 's':
			op.nx = op.ny = op.nz = atoi(optarg);
			break;
		case 'd':
			op.nx = op.ny = atoi(optarg);
			break;
		case 'w':
			if (optarg[0] == 'x')
				op.periodic = 0;
			else if (optarg[0] == 'y')
				op.periodic = 1;
			else if (optarg[0] == 'z')
				op.periodic = 2;
			break;
		case 'm':
			for (i = 0; i < NUM_MAPS; i++) {
				if (strcmp(optarg, map_key[i]) == 0)
					op.map = i;
			}
			break;
		case 'p':
			for (i = 0; i < NUM_PROBLEMS; i++) {
				if (strcmp(optarg, problem_key[i]) == 0)
					op.problem = i;
			}
			break;
		case 'l':
			if (optarg[0] == 'p') {
				print_problems();
				MPI_Finalize();
				exit(0);
			} else if (optarg[0] == 'm') {
				print_maps();
				MPI_Finalize();
				exit(0);
			}
			break;
		case '?':
			if (optopt == 'x'
			    || optopt == 'y'
			    || optopt == 'z'
			    || optopt == 'm'
			    || optopt == 'w'
			    || optopt == 's'
			    || optopt == 'd'
			    || optopt == 'p')
				fprintf(stderr, "Option -%c requires an argument.\n", optopt);
			else if (isprint(optopt))
				fprintf(stderr, "Unknown option `-%c'.\n", optopt);
			else
				fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
			return op;
		default:
			abort();
		}
	}

	return op;
}
Exemple #11
0
/*
 * transfer_all_new_dbs()
 *
 * Responsible for upgrading all database. invokes routines to generate mappings and then
 * physically link the databases.
 */
const char *
transfer_all_new_dbs(DbInfoArr *old_db_arr,
				   DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata)
{
	int			old_dbnum,
				new_dbnum;
	const char *msg = NULL;

	prep_status("%s user relation files\n",
	  user_opts.transfer_mode == TRANSFER_MODE_LINK ? "Linking" : "Copying");

	/* Scan the old cluster databases and transfer their files */
	for (old_dbnum = new_dbnum = 0;
		 old_dbnum < old_db_arr->ndbs;
		 old_dbnum++, new_dbnum++)
	{
		DbInfo	   *old_db = &old_db_arr->dbs[old_dbnum],
				   *new_db = NULL;
		FileNameMap *mappings;
		int			n_maps;
		pageCnvCtx *pageConverter = NULL;

		/*
		 * Advance past any databases that exist in the new cluster but not in
		 * the old, e.g. "postgres".  (The user might have removed the
		 * 'postgres' database from the old cluster.)
		 */
		for (; new_dbnum < new_db_arr->ndbs; new_dbnum++)
		{
			new_db = &new_db_arr->dbs[new_dbnum];
			if (strcmp(old_db->db_name, new_db->db_name) == 0)
				break;
		}

		if (new_dbnum >= new_db_arr->ndbs)
			pg_log(PG_FATAL, "old database \"%s\" not found in the new cluster\n",
				   old_db->db_name);

		n_maps = 0;
		mappings = gen_db_file_maps(old_db, new_db, &n_maps, old_pgdata,
									new_pgdata);

		if (n_maps)
		{
			print_maps(mappings, n_maps, new_db->db_name);

#ifdef PAGE_CONVERSION
			msg = setupPageConverter(&pageConverter);
#endif
			transfer_single_new_db(pageConverter, mappings, n_maps);

			pg_free(mappings);
		}
	}

	prep_status(" ");			/* in case nothing printed; pass a space so
								 * gcc doesn't complain about empty format
								 * string */
	check_ok();

	return msg;
}