예제 #1
0
void listen_to_connections() {
	/* Print status message */
	pstatus("Start of the listen");

	/* Start listen for connections */
	listen(sock_fd, 5);	

	/* Launch the main loop */
	while(1) {

		/* Get the address length */
		addr_size = sizeof(cli_addr);
		/* Accept connection */
		client_fd = accept(sock_fd, (struct sockaddr*) &cli_addr, &addr_size);

		pstatus("New connection arrived");

		/* Check if everything is correct */
		if (client_fd < 0) {
			error("Error on socket accept");
		}

		create_new_thread(client_fd);

	}

	terminate_connection();
	
}
예제 #2
0
파일: sf.c 프로젝트: 0111sandesh/TinyOS-Sec
void rem_client(struct client_list **c)
{
  struct client_list *dead = *c;

  *c = dead->next;
  num_clients--;
  pstatus();
  close(dead->fd);
  free(dead);
}
예제 #3
0
파일: client.c 프로젝트: jwbenham/academic
/* *****************************************************************************
 * Function definitions.
 * ****************************************************************************/
void 
clean_exit(int exit_code, struct p_conn_params *cp, pthread_t *t) 
{
	MXLOCK(&mx_rdr_life);
	g_rdr = 0;
	MXUNLOCK(&mx_rdr_life);
	
	pthread_join(*t, NULL);
	pstatus("Reader thread shutdown.");
	
	if (cp->active) {
		p_close_socket(&cp->socket);
	}
	pstatus("Connection closed.");
	
	// Exit successfully.
	printf("\n");
	exit(exit_code);
}
예제 #4
0
파일: sf.c 프로젝트: 0111sandesh/TinyOS-Sec
void add_client(int fd)
{
  struct client_list *c = xmalloc(sizeof *c);

  c->next = clients;
  clients = c;
  num_clients++;
  pstatus();

  c->fd = fd;
}
예제 #5
0
int top_main  (int argc, char ** argv) {

	char c;
	heap h = heap_init(1024, (void *) proc_comparer);
	do {
		int allow_zombies = 0;

		if (argc > 1 && !strcmp(argv[1], "zombies")) {
			allow_zombies = 1;
		}

		int len = 0;
		int i = 0;
		int active_pids[PROCESS_MAX];
		int	active_pids_ticks[PROCESS_MAX];
		int active_pids_n = 0;

		for(; i < PROCESS_MAX; ++i)
		{
			active_pids[i] = -1;
			active_pids_ticks[i] = 0;
		}

		int tick_history[PROCESS_HISTORY_SIZE];
		int * _pticks = pticks();

		i = 0;

		for(; i < PROCESS_HISTORY_SIZE; i++) {
			if(_pticks[i] == -1) {
				break;
			}
			tick_history[i] = _pticks[i];
		}


		len = i;
		i = 0;
		int zombs = 0;
		for(; i < len; ++i)
		{
			int pid = tick_history[i];
			int _pid_index = -1, j = 0;
			for(; j < active_pids_n; j++)	{
				if(active_pids[j] == pid) {
					_pid_index = j;
					break;
				}
			}
			if(_pid_index == -1)	{
				_pid_index = active_pids_n;
				active_pids[_pid_index] = pid;
				active_pids_n++;
			}
			if (pstatus(pid) != PROCESS_ZOMBIE) {
				active_pids_ticks[_pid_index]++;
			} else {
				zombs++;
			}
		}

		if (!allow_zombies) {
			len -= zombs;
		}



		i = 0;
		int printed = 0;
		for(; i < PROCESS_MAX; ++i)	{
			int pid = pgetpid_at(i);

			if (pid != -1){
				int j = -1;
				for(; j < active_pids_n; j++)	{
					if(active_pids[j] == pid) {
						break;
					}
				}
				int ticks = ( j == -1 ) ? 0 : active_pids_ticks[j];
				top_data * data = (top_data *) malloc(sizeof(top_data));
				data->ticks = ticks;
				data->pid = pid;
				heap_insert(data, h);

			}
		} 
		i = 0;
		while(!heap_empty(h)) {
			top_data * data = heap_remove_min(h);
			if(0)	{
				break;
			} else {
				i++;
				int pid = data->pid;
				int ticks = data->ticks;

				char * _pname = pname(pid);
				char * status = NULL;

				int stat = pstatus(pid);
				switch(stat){
					case PROCESS_READY:
					status = "READY";
					break;
					case PROCESS_BLOCKED:
					status = "BLOCKED";
					break;
					case PROCESS_ZOMBIE:
					status = "ZOMBIE ";
					break;
					case PROCESS_RUNNING:
					status = "RUNNING";
					break;
					default:
					status = "UNKNOWN";
				}

				int priority = ppriority(pid);

				len = (!len) ? 1 : len;

				if (stat != PROCESS_ZOMBIE || (stat == PROCESS_ZOMBIE && allow_zombies) ) {
					printed++;

					printf("PID: %d \t NAME: %s \t CPU:%% %d \t STATUS: %s \t PRIORITY: %d\n",
						pid, _pname, 
						(100 * ticks) / len, status, priority);
				}
			}
		}
		
		printf("--------------------------------------------------------------------------------\n");
		sleep(1024);
	}
	while(1);
	
	return 0;
}
예제 #6
0
파일: cdcontrol.c 프로젝트: 2asoft/freebsd
static int
run(int cmd, char *arg)
{
	long speed;
	int l, r, rc, count;

	switch (cmd) {

	case CMD_QUIT:
		exit (0);

	case CMD_INFO:
		if (fd < 0 && ! open_cd ())
			return (0);

		return info (arg);

	case CMD_CDID:
		if (fd < 0 && ! open_cd ())
			return (0);

		return cdid ();

	case CMD_STATUS:
		if (fd < 0 && ! open_cd ())
			return (0);

		return pstatus (arg);

	case CMD_NEXT:
	case CMD_PREVIOUS:
		if (fd < 0 && ! open_cd ())
			return (0);

		while (isspace (*arg))
			arg++;

		return next_prev (arg, cmd);

	case CMD_PAUSE:
		if (fd < 0 && ! open_cd ())
			return (0);

		return ioctl (fd, CDIOCPAUSE);

	case CMD_RESUME:
		if (fd < 0 && ! open_cd ())
			return (0);

		return ioctl (fd, CDIOCRESUME);

	case CMD_STOP:
		if (fd < 0 && ! open_cd ())
			return (0);

		rc = ioctl (fd, CDIOCSTOP);

		(void) ioctl (fd, CDIOCALLOW);

		return (rc);

	case CMD_RESET:
		if (fd < 0 && ! open_cd ())
			return (0);

		rc = ioctl (fd, CDIOCRESET);
		if (rc < 0)
			return rc;
		close(fd);
		fd = -1;
		return (0);

	case CMD_DEBUG:
		if (fd < 0 && ! open_cd ())
			return (0);

		if (! strcasecmp (arg, "on"))
			return ioctl (fd, CDIOCSETDEBUG);

		if (! strcasecmp (arg, "off"))
			return ioctl (fd, CDIOCCLRDEBUG);

		warnx("invalid command arguments");

		return (0);

	case CMD_EJECT:
		if (fd < 0 && ! open_cd ())
			return (0);

		(void) ioctl (fd, CDIOCALLOW);
		rc = ioctl (fd, CDIOCEJECT);
		if (rc < 0)
			return (rc);
		return (0);

	case CMD_CLOSE:
		if (fd < 0 && ! open_cd ())
			return (0);

		(void) ioctl (fd, CDIOCALLOW);
		rc = ioctl (fd, CDIOCCLOSE);
		if (rc < 0)
			return (rc);
		close(fd);
		fd = -1;
		return (0);

	case CMD_PLAY:
		if (fd < 0 && ! open_cd ())
			return (0);

		while (isspace (*arg))
			arg++;

		return play (arg);

	case CMD_SET:
		if (! strcasecmp (arg, "msf"))
			msf = 1;
		else if (! strcasecmp (arg, "lba"))
			msf = 0;
		else
			warnx("invalid command arguments");
		return (0);

	case CMD_VOLUME:
		if (fd < 0 && !open_cd ())
			return (0);

		if (! strlen (arg)) {
			char volume[] = "volume";

		    	return pstatus (volume);
		}

		if (! strncasecmp (arg, "left", strlen(arg)))
			return ioctl (fd, CDIOCSETLEFT);

		if (! strncasecmp (arg, "right", strlen(arg)))
			return ioctl (fd, CDIOCSETRIGHT);

		if (! strncasecmp (arg, "mono", strlen(arg)))
			return ioctl (fd, CDIOCSETMONO);

		if (! strncasecmp (arg, "stereo", strlen(arg)))
			return ioctl (fd, CDIOCSETSTERIO);

		if (! strncasecmp (arg, "mute", strlen(arg)))
			return ioctl (fd, CDIOCSETMUTE);

		count = sscanf (arg, "%d %d", &l, &r);
		if (count == 1)
		    return setvol (l, l);
		if (count == 2)
		    return setvol (l, r);
		warnx("invalid command arguments");
		return (0);

	case CMD_SPEED:
		if (fd < 0 && ! open_cd ())
			return (0);

		errno = 0;
		if (strcasecmp("max", arg) == 0)
			speed = CDR_MAX_SPEED;
		else
			speed = strtol(arg, NULL, 10) * 177;
		if (speed <= 0 || speed > INT_MAX) {
			warnx("invalid command arguments %s", arg);
			return (0);
		}
		return ioctl(fd, CDRIOCREADSPEED, &speed);

	default:
	case CMD_HELP:
		help ();
		return (0);

	}
}
예제 #7
0
void terminate_handler(int client) {
	pstatus("Killing current client connection");
	/* Terminate the connection */
	close(client);
}
예제 #8
0
void terminate_connection() {
	pstatus("Killing main socket");
	/* Terminate the socket */
	close(sock_fd);
}
예제 #9
0
파일: client.c 프로젝트: jwbenham/academic
/** 
 * Collects user input and performs the main program loop.
 * @param argc - Number of arguments; should be 3.
 * @param argv - Command line arguments; 1 should be the server; 2 should be the
 * 				port.
 * @return int - Returns 0 if no errors; nonzero otherwise.
 */
int 
main(int argc, char *argv[]) 
{	
	struct p_conn_params conn;	// Holds information for the connection.
	int errcode;	// Hold return error codes.
	int running = 1;	// 0 if program should exit.
	int unsent_msg = 0; // Indicates if last user message was not sent.
	pthread_t rdr_id;

	// Check proper usage.
	if (argc != 3) {
		return p_perror(P_EUSAGE, NULL);
	}

	// Initialize the connection.
	p_init_conn_params(&conn);
	conn.server = argv[1];
	conn.port = argv[2];	
	p_init_addrinfo(&conn.params, 0, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
	
	// Start up the reader.
	g_rdr = 1;
	if (pthread_create(&rdr_id, NULL, &rdr_task, (void *) &conn)) {
		return p_perror(P_EREADER, NULL);
	}
	
	
	// Main program loop.
	pstatus("Client program. Enter *quit to quit.");
	
	do {
		// Reconnect if necessary.
		if (sync_get_active(&conn, &mx_active) != 1) {
			pstatus("Attempting to establish connection..");
			
			MXLOCK(&mx_socket);
			MXLOCK(&mx_active);
			
			errcode = p_connect(&conn);	// Try to connect.
			
			MXUNLOCK(&mx_active);
			MXUNLOCK(&mx_socket);

			if (errcode) {	// Connection failed.
				p_perror(errcode, "In main loop, after (re)connect block");
				clean_exit(errcode, &conn, &rdr_id);
			}
			
			pstatus("Connection opened.\n");
		}
		
		// Get a line of input from the user, unless the previous message was
		// unsent (e.g. because of disconnect).
		fflush(stdout);
		if (unsent_msg) {
			pstatus("Last message was unsent. Now sending:");
			printf("\n%s\n", INBUF);
			unsent_msg = 0;
		}
		else {
			printf(">>");
			running = get_input();
		}	
	
		// Continue and send if the user had not chosen to quit.
		if (running) {
			
			// Check connection.
			errcode = p_test_conn(conn.socket);
			if (errcode == 0) {
				pstatus("The server closed the connection.\n");
				sync_set_active(&conn, &mx_active, 0);
				p_close_socket(&conn.socket);
				unsent_msg = 1;
			}
		
			// Send the message if the connection is ok.
			if (sync_get_active(&conn, &mx_active)) { 
				MXLOCK(&mx_socket);

				if (p_send_msg(&conn.socket, INBUF, sizeof(char)*strlen(INBUF)) == 0) {
					p_perror(P_ESENDF, strerror(errno));
					running = 0;
				} 
			
				MXUNLOCK(&mx_socket);
			}
		}		
			
	} while(running);
	
	
	// Close the socket and join with the reader thread.
	clean_exit(0, &conn, &rdr_id);
}