Example #1
0
/**
	Reads the specified setup file, 
	building a list of strings containing the operations to simulate.<br>
	Assumes that the file has a correct structure.
	@param pathname The setup file's path
	@return The list of operations to compute
*/
static list* parse_file(const char *const pathname) {
	list *result = list_construct();
	char line[50];
	int len, fd;
	
	if(result == NULL) 
		exit(1);

	fd = open(pathname, O_RDONLY);
	if(fd == -1) {
		write_to_fd(2, "Failed to open setup file\n");
		exit(1);
	}
		
	do {
		len = read_line(fd, line, 50);
		if (len > 0)
			list_append(result, line);
	} while(len >= 0);
	
	if (close(fd) == -1) {
		write_to_fd(2, "Failed to close setup file\n");
		exit(1);
	}

	return result;
}
Example #2
0
void *fun2(void *ptr)
{
	int fd = *((int *)ptr);

	/* No work needs to be done, send right away. */
	write_to_fd(fd, "Hello");

	sleep(3);  /* Simulate work being done... */
	write_to_fd(fd, "from ");

	close(fd);
	return NULL;
}
Example #3
0
void *fun1(void *ptr)
{
	int fd = *((int *)ptr);

	sleep(2);  /* Simulate work being done... */
	write_to_fd(fd, "World");

	sleep(3);  /* Simulate work being done... */
	write_to_fd(fd, "CS241");

	close(fd);
	return NULL;
}
Example #4
0
/* Copy file of given length to output, and fail if the file has
 * changed size.
 */
static void
write_file_len_to_fd (const char *filename, size_t len)
{
  char buffer[BUFFER_SIZE];
  size_t count = 0;

  if (verbose >= 2)
    fprintf (stderr, "write_file_to_fd %s -> %d\n", filename, out_fd);

  int fd2 = open (filename, O_RDONLY);
  if (fd2 == -1)
    error (EXIT_FAILURE, errno, "open: %s", filename);
  for (;;) {
    ssize_t r = read (fd2, buffer, sizeof buffer);
    if (r == 0)
      break;
    if (r == -1) {
      if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
        continue;
      error (EXIT_FAILURE, errno, "read: %s", filename);
    }
    write_to_fd (buffer, r);
    count += r;
    if (count > len)
      error (EXIT_FAILURE, 0, "write_file_len_to_fd: %s: file has increased in size\n", filename);
  }

  if (close (fd2) == -1)
    error (EXIT_FAILURE, errno, "close: %s", filename);

  if (count != len)
    error (EXIT_FAILURE, 0, "febootstrap-supermin-helper: write_file_len_to_fd: %s: file has changed size\n", filename);
}
Example #5
0
/* Copy contents of file to out_fd. */
static void
write_file_to_fd (const char *filename)
{
  char buffer[BUFFER_SIZE];
  int fd2;
  ssize_t r;

  if (verbose >= 2)
    fprintf (stderr, "write_file_to_fd %s -> %d\n", filename, out_fd);

  fd2 = open (filename, O_RDONLY);
  if (fd2 == -1)
    error (EXIT_FAILURE, errno, "open: %s", filename);
  for (;;) {
    r = read (fd2, buffer, sizeof buffer);
    if (r == 0)
      break;
    if (r == -1) {
      if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
        continue;
      error (EXIT_FAILURE, errno, "read: %s", filename);
    }
    write_to_fd (buffer, r);
  }

  if (close (fd2) == -1)
    error (EXIT_FAILURE, errno, "close: %s", filename);
}
Example #6
0
/* Sets errno on error (!= 0), ENOSPC on short write */
int
copy_file_data (int sfd,
                int dfd)
{
  char buffer[BUFSIZE];
  ssize_t bytes_read;

  while (TRUE)
    {
      bytes_read = read (sfd, buffer, BUFSIZE);
      if (bytes_read == -1)
        {
          if (errno == EINTR)
            continue;

          return -1;
        }

      if (bytes_read == 0)
        break;

      if (write_to_fd (dfd, buffer, bytes_read) != 0)
        return -1;
    }

  return 0;
}
Example #7
0
/**
	Searches the processor state array for a free processor (state <= 0).
	@param states The array of processor states. Each cell <i>i</i> contains a 
	key such that:<br>
	<ul><li>If <b>key < 0</b>, processor has completed the |key|-th operation
	<li>If <b>key == 0</b>, processor has not received an operation yet
	<li>If <b>key > 0</b>, processor is currently working on the key-th operation</ul>
	@return The ID of a free processor
*/
static int find_proc(int *states) {
	int i = 0;

	sem_p(2 * processors);
	write_to_fd(1, "Looking for a free processor\n");
	while(states[i++] > 0);
	sem_v(2 * processors);
	write_with_int(1, "Found processor ", i);
	return i - 1;
}
Example #8
0
/* Write 'len' bytes of zeroes out. */
static void
write_padding (size_t len)
{
  static const char buffer[512] = { 0 };

  while (len > 0) {
    size_t n = len < sizeof buffer ? len : sizeof buffer;
    write_to_fd (buffer, n);
    len -= n;
  }
}
Example #9
0
static gboolean
dump_container_properties(gs_container_t * container)
{
	GSList *properties = NULL, *list = NULL;
        struct metacnx_ctx_s cnx;
	gchar path[1024];
	int output_fd;
	GError *error = NULL;

	metacnx_clear(&cnx);
        if (!metacnx_init_with_addr(&cnx, &(container->meta2_addr), &error)) {
		PRINT_ERROR("Invalid META2 address : %s", error->message);
		g_clear_error(&error);
               	return FALSE; 
        }

	/* Change default timeout */
	cnx.timeout.req = 10000;
	cnx.timeout.cnx = 10000;

	if (!meta2_remote_list_all_container_properties(&cnx, container->cID, &properties, &error)) {
		PRINT_ERROR("Failed to list container properties : %s", error->message);
		g_clear_error(&error);
		return FALSE;
	}
        metacnx_close(&cnx);
        metacnx_clear(&cnx);

	/* open container properties file */
	memset(path, '\0', sizeof(path));
	g_snprintf(path, sizeof(path), "%s/container.properties", base_dir);
	output_fd = open(path, O_EXCL | O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
	if (output_fd < 0) {
		PRINT_ERROR("Failed to open [%s] as the container properties target : %s\n", path, strerror(errno));
		return FALSE;
	}
	/*  Print container properties to file */
	if (g_slist_length(properties)>0) {
		for (list = properties; list && list->data; list = list->next) {
			gchar *txt = g_strdup_printf("%s\n", (char*)list->data);
			write_to_fd(&output_fd, txt, strlen(txt));
			g_free(txt);
		}
	}
	/*then adjust the file rights */
	fchmod(output_fd, S_IRGRP | S_IRUSR | S_IWUSR);
	close(output_fd);

	g_slist_foreach(properties, gslist_free_element, g_free);
	g_slist_free(properties);

	return TRUE;
}
Example #10
0
/**
	Forks and executes the required number of processors.
*/
static void start_processors(void) {
	int i, pid;
	char proc_id[8], nsems[8];

	if(itoa((2 * processors) + 2, nsems, 8) == -1) {
		write_to_fd(2, "Failed to convert number of semaphores\n");	
		kill_group(SIGTERM);
	}
	for (i = 0; i < processors; ++i) {
		pid = fork();
		if (pid == -1) {
			write_to_fd(2, "Failed to fork processor\n");
			kill_group(SIGTERM);
		}
		if (pid == 0) {
			if(itoa(i, proc_id, 8) != -1)
				execl("processor.x", "processor.x", proc_id, nsems, (char *) NULL);
			kill_group(SIGTERM);
		}
	}
}
Example #11
0
/* Sets errno on error (!= 0), ENOSPC on short write */
int
create_file (const char *path,
             mode_t      mode,
             const char *content)
{
  int fd;
  int res;
  int errsv;

  fd = creat (path, mode);
  if (fd == -1)
    return -1;

  res = 0;
  if (content)
    res = write_to_fd (fd, content, strlen (content));

  errsv = errno;
  close (fd);
  errno = errsv;

  return res;
}
Example #12
0
/* Sets errno on error (!= 0), ENOSPC on short write */
int
write_file_at (int         dirfd,
               const char *path,
               const char *content)
{
  int fd;
  bool res;
  int errsv;

  fd = openat (dirfd, path, O_RDWR | O_CLOEXEC, 0);
  if (fd == -1)
    return -1;

  res = 0;
  if (content)
    res = write_to_fd (fd, content, strlen (content));

  errsv = errno;
  close (fd);
  errno = errsv;

  return res;
}
Example #13
0
/**
	Carries out simulation setup and management.
	@param argc The number of arguments
	@param argv The array of arguments
*/
int main(int argc, char *argv[]) {
	int *results, *shm_states;
	int i, op_count, proc_id;
	char *tmp_operator, *cmd;
	list *commands;
	operation *shm_operations;
	
	if(signal(SIGTERM, &stop_execution) == SIG_ERR) {
		write_to_fd(2, "Failed to register signal\n");
		exit(1);
	}
	if(argc != 3) {
		write_to_fd(2, "Usage: main.x <source file> <results file>\n");
		exit(1);
	}
	commands = parse_file(argv[1]);
	processors = atoi(list_extract(commands));
	if (processors <= 0) {
		write_to_fd(2, "Invalid number of processors\n");
		exit(1);
	}
	write_with_int(1, "Number of processors: ", processors);
	op_count = list_count(commands);
	if (op_count == 0) {
		write_to_fd(2, "No operations provided\n");
		exit(1);
	}
	write_with_int(1, "Number of operations: ", op_count);		
	results = (int *) malloc(op_count * sizeof(int));
	if (results == NULL) {
		write_to_fd(2, "Failed to allocate results array\n");
		exit(1);	
	}
	
	init_ipc(2 * processors + 2, processors * sizeof(operation), processors * sizeof(int), 0666 | IPC_CREAT | IPC_EXCL);
	write_with_int(1, "Created semaphore set with ID ", ipc_id[0]);
	write_with_int(1, "Created shm for operations with ID ", ipc_id[1]);
	write_with_int(1, "Created shm for states with ID ", ipc_id[2]);
	init_sems(processors);
	shm_operations = (operation *) shm_attach(ipc_id[1]);
	shm_states = (int *) shm_attach(ipc_id[2]);
	
	for (i = 0; i < processors; ++i)
		shm_states[i] = 0;
	
	start_processors();
	for (i = 1; list_count(commands) > 0; ++i) {
		cmd = list_extract(commands);
		write_with_int(1, "\nOperation #", i);
		proc_id = atoi(strtok(cmd, " "));
		sem_p(2 * processors + 1);
		if (proc_id-- == 0) {
			proc_id = find_proc(shm_states);
		}
		write_with_int(1, "Waiting for processor ", proc_id + 1);
		sem_p(2 * proc_id);
		write_with_int(1, "Delivering operation to processor ", proc_id + 1);
		if (shm_states[proc_id] != 0) {
			results[(shm_states[proc_id] + 1) * -1] = shm_operations[proc_id].num1;
			write_with_int(1, "Previous result: ", shm_operations[proc_id].num1);
		}
		shm_operations[proc_id].num1 = atoi(strtok(NULL, " "));
		tmp_operator = strtok(NULL, " ");
		shm_operations[proc_id].op = *tmp_operator;
		shm_operations[proc_id].num2 = atoi(strtok(NULL, " "));
		shm_states[proc_id] = i;
		write_with_int(1, "Operation delivered. Unblocking processor ", proc_id + 1);
		sem_v((2 * proc_id) + 1);
		free(cmd);
	}
	
	list_destruct(commands);
	
	for (i = 0; i < processors; ++i) {
		sem_p(2 * i);
		write_with_int(1, "\nPassing termination command to processor #", i + 1);
		if (shm_states[i] != 0) {
			results[(shm_states[i] + 1) * -1] = shm_operations[i].num1;
			write_with_int(1, "Last result: ", shm_operations[i].num1);
		}
		shm_operations[i].op = 'K';
		sem_v((2 * i) + 1);
	}

	for (i = 0; i < processors; ++i) 
		if(wait(NULL) == -1)
			write_to_fd(2, "Wait failed\n");
			
	write_to_fd(1, "\nAll processors exited. Writing output file\n");
	write_results(argv[2], results, op_count);
	free(results);
	write_to_fd(1, "Closing IPCs\n");
	shm_detach((void *) shm_operations);
	shm_detach((void *) shm_states);
	close_ipc();
	exit(0);
}
Example #14
0
/**
	Handles the @c SIGTERM signal by closing all IPCs.
	@param signum The received signal
*/
static void stop_execution(int signum) {
	write_to_fd(2, "SIGTERM received. Closing IPCs\n");
	close_ipc();
	exit(1);
}
int suspend_test(void)
{
	char wakelock_dmask[16] = "";
	char earlysuspend_dmask[16] = "";
	time_t now;
	time_t expiration;
	int success_count = 0;
	int rv;
	int i = 0;
	int k;
	int num_success = 0;
	int ret;
	unsigned int core1_status;
	unsigned int core2_status;
	unsigned int core3_status;
	unsigned int dual_core = 0;
	signed long usb_active;
	int num_cores = 0;
	char *ss_pid;


	fprintf(stdout, "Determine if wakelock node is there \n");
	g_wakelock_exists = file_exists(WAKELOCK_NODE);

	if (g_wakelock_exists) {

		fprintf(stdout, "reset the sensors data setting \n");
		rv = write_string_to_file(NULL, SENSOR_SETTINGS, "0\n");
		if (rv < 0) {
			fprintf(stdout, "unable to set sensor settings: %s\n", strerror(-rv));
			goto suspend_test_early_bailout;
		}

		fprintf(stdout, "sleep for sensor daemon to pick up the setting \n");
		sleep(2);

		fprintf(stdout, "Save current wakelock debug mask\n");
		rv = read_from_file(
			NULL,
			WAKELOCK_DEBUG_MASK_NODE,
			wakelock_dmask,
			sizeof(wakelock_dmask) - 1);

		if (rv < 0) {
			fprintf(stdout,
				"cannot read %s: %s\n",
				WAKELOCK_DEBUG_MASK_NODE,
				strerror(-rv));
			goto suspend_test_bailout;
		}

		wakelock_dmask[rv] = '\0';

		fprintf(stdout,
			"Turn on additional wakelock debug info\n");
		rv = write_string_to_file(
			NULL, WAKELOCK_DEBUG_MASK_NODE, "22\n");
		if (rv < 0) {
			fprintf(stdout,
				"cannot write to %s: %s\n",
				WAKELOCK_DEBUG_MASK_NODE,
				strerror(-rv));
			goto suspend_test_bailout;
		}

		fprintf(stdout, "Add wakelock to hold off suspend till we suspend ourselves\n");
		rv = write_string_to_file(
			NULL, WAKELOCK_LOCK_NODE, SUSPENDPC_WAKELOCK);

		if (rv < 0) {
			fprintf(stdout, "cannot write to %s: %s\n",
				WAKELOCK_LOCK_NODE, strerror(-rv));
			goto suspend_test_bailout;
		}
	}

	fprintf(stdout, "Save current earlysuspend debug mask\n");
	rv = read_from_file(
		NULL,
		EARLYSUSPEND_DEBUG_MASK_NODE,
		earlysuspend_dmask,
		sizeof(earlysuspend_dmask) - 1);

	if (rv < 0) {
		fprintf(stdout,
			"cannot read %s: %s\n",
			EARLYSUSPEND_DEBUG_MASK_NODE,
			strerror(-rv));
		goto suspend_test_bailout;
	}

	earlysuspend_dmask[rv] = '\0';

	fprintf(stdout, "turn on early suspend logs\n");
	rv = write_string_to_file(
		NULL, EARLYSUSPEND_DEBUG_MASK_NODE, "5\n");

	if (rv < 0) {
		fprintf(stdout,
			"cannot write to %s: %s\n",
			EARLYSUSPEND_DEBUG_MASK_NODE,
			strerror(-rv));
		goto suspend_test_bailout;
	}


	fprintf(stdout, "Check whether power management is up\n");
	if (!file_exists(PM_STATS_NODE)) {
		fprintf(stdout, "power management is not available\n");
		goto suspend_test_bailout;
	}

	fprintf(stdout, "Determine the power management module\n");

	if (directory_exists(SYS_PM_8x60)) {
		g_sys_pm = SYS_PM_8x60;
		if (file_exists_with_prefix(SYS_PM_8x60, SLEEP_MODE_NODE_CORE_3))
			num_cores = 4;
		else if (file_exists_with_prefix(SYS_PM_8x60, SLEEP_MODE_NODE_CORE_1))
			num_cores = 2;
		else
			num_cores = 1;
	}
	else {
		fprintf(stdout, "power management is not available\n");
		goto suspend_test_bailout;
	}

	fprintf(stdout, "Delay tests for some time for usb cable to be plugged out\n");
	if (g_delay_test_sec) {
		unsigned int seconds = g_delay_test_sec;
		while (seconds)
			seconds = sleep(seconds);
	}

	/*
	 *  check if the usb wakelock has been released before
	 *  proceeding with the test
	 */
	if (g_wakelock_exists) {
		/*
		 *  capture the usb wakelock stats
		 */
		rv = read_from_file(NULL, WAKELOCK_NODE, g_wakelock_stats,
				    sizeof(g_wakelock_stats) - 1);

		usb_active = parse_wakelock_stats_for_active_wl(
			g_wakelock_stats, "\"msm_otg\"");

		if (usb_active != 0) {
			fprintf(stdout, " the usb wakelock is still held by the system\n");
			goto suspend_test_bailout;
		}
	}

	/*
	 *  determine resume command
	 */
	do {
		rv = write_string_to_file(NULL, POWER_NODE, POWER_STANDBY);
		if (rv > 0) {
			g_resume_command = POWER_STANDBY;
			break;
		}

		rv = write_string_to_file(
			NULL, POWER_NODE, POWER_ON);

		if (rv > 0) {
			g_resume_command = POWER_ON;
			break;
		}

		fprintf(stdout, "cannot write to %s: %s\n",
			POWER_NODE, strerror(-rv));

		goto suspend_test_bailout;
	} while (0);

	fprintf(stdout, "Set suspend configuration\n");
	if (num_cores == 4) {
		write_int_to_file(g_sys_pm, SLEEP_MODE_NODE_CORE_0, 1);
		write_int_to_file(g_sys_pm, SLEEP_MODE_NODE_CORE_1, 1);
		write_int_to_file(g_sys_pm, SLEEP_MODE_NODE_CORE_2, 1);
		write_int_to_file(g_sys_pm, SLEEP_MODE_NODE_CORE_3, 1);
	}
	else if (num_cores == 2) {
		write_int_to_file(g_sys_pm, SLEEP_MODE_NODE_CORE_0, 1);
		write_int_to_file(g_sys_pm, SLEEP_MODE_NODE_CORE_1, 1);
	}
	else if (num_cores == 1) {
		write_int_to_file(g_sys_pm, SLEEP_MODE_NODE_CORE_0, 1);
	}

	fprintf(stdout, "Turn off idle power collapse and mp-decision\n");
	if ((num_cores == 2) || (num_cores == 4)) {
		if (num_cores == 4) {
			write_int_to_file(g_sys_pm, IDLE_SLEEP_MODE_NODE_CORE_2, 0);
			write_int_to_file(g_sys_pm, IDLE_SLEEP_MODE_NODE_CORE_3, 0);
			write_int_to_file(g_sys_pm, IDLE_SLEEP_MODE_NODE_STD_CORE_2, 0);
			write_int_to_file(g_sys_pm, IDLE_SLEEP_MODE_NODE_STD_CORE_3, 0);
		}

		write_int_to_file(g_sys_pm, IDLE_SLEEP_MODE_NODE_CORE_0, 0);
		write_int_to_file(g_sys_pm, IDLE_SLEEP_MODE_NODE_CORE_1, 0);
		write_int_to_file(g_sys_pm, IDLE_SLEEP_MODE_NODE_STD_CORE_0, 0);
		write_int_to_file(g_sys_pm, IDLE_SLEEP_MODE_NODE_STD_CORE_1, 0);

		fprintf(stdout, "Turn off mp-decision before testing suspend\n");
		rv = fork_exec("stop", "mpdecision", NULL, NULL, 0);
		if (rv < 0) {
			fprintf(stdout, "cannot turn off mp-decision: %s\n", strerror(-rv));
			goto suspend_test_bailout;
		}

		fprintf(stdout, "Now explicitly turn ON core 1 if not ON already\n");
		core1_status = read_unsigned_int_from_file(NULL, HOTPLUG_NODE_CORE_1);
		fprintf(stdout, "core1_status = %d\n", core1_status);
		if (!core1_status) {
			fprintf(stdout, "turning on core 1\n");
			rv = write_int_to_file(NULL, HOTPLUG_NODE_CORE_1, 1);
		}

		if (num_cores == 4) {
			fprintf(stdout, "Now explicitly turn ON core 2 if not ON already\n");
			core2_status = read_unsigned_int_from_file(NULL, HOTPLUG_NODE_CORE_2);
			fprintf(stdout, "core2_status = %d\n", core2_status);
			if (!core2_status) {
				fprintf(stdout, "turning on core 2\n");
				rv = write_int_to_file(NULL, HOTPLUG_NODE_CORE_2, 1);
			}

			fprintf(stdout, "Now explicitly turn ON core 3 if not ON already\n");
			core3_status = read_unsigned_int_from_file(NULL, HOTPLUG_NODE_CORE_3);
			fprintf(stdout, "core3_status = %d\n", core3_status);
			if (!core3_status) {
				fprintf(stdout, "turning on core 3\n");
				rv = write_int_to_file(NULL, HOTPLUG_NODE_CORE_3, 1);
			}
		}
	}
	else if (num_cores == 1) {
		write_int_to_file(g_sys_pm, IDLE_SLEEP_MODE_NODE_CORE_0, 0);
		write_int_to_file(g_sys_pm, IDLE_SLEEP_MODE_NODE_STD_CORE_0, 0);
	}

	fprintf(stdout, "Clear kernel log\n");
	rv = fork_exec("dmesg", "-c", NULL, NULL, 1);
	if (rv < 0) {
		fprintf(stdout,
			"dmesg -c failed: %s\n", strerror(-rv));
		goto suspend_test_bailout;
	}

	fprintf(stdout, "Clear power management stats\n");
	rv = write_string_to_file(NULL, PM_STATS_NODE, "reset\n");
	if (rv < 0) {
		fprintf(stdout,	"cannot write to %s: %s\n",
			PM_STATS_NODE, strerror(-rv));
		goto suspend_test_bailout;
	}

	fprintf(stdout, "Set wakeup time\n");
	rv = write_int_to_file(
		g_sys_pm, SLEEP_TIME_OVERRIDE_NODE, g_wakeup_sec);
	if (rv < 0) {
		fprintf(stdout,	"cannot write to %s%s: %s\n", g_sys_pm,
			SLEEP_TIME_OVERRIDE_NODE, strerror(-rv));
		goto suspend_test_bailout;
	}

	if (g_wakelock_exists) {
		fprintf(stdout, "Remove the suspend wakelock before issuing the suspend command\n");
		rv = write_string_to_file(
			NULL, WAKELOCK_UNLOCK_NODE, SUSPENDPC_WAKELOCK);

		if (rv < 0) {
			fprintf(stdout, "cannot write to %s: %s\n",
				WAKELOCK_UNLOCK_NODE, strerror(-rv));
			goto suspend_test_bailout;
		}
	}

	for (i = 0; i < g_num_iter; i++) {

		msg("beginning iteration %d for suspend\n", i);
		fprintf(stdout, "Determine expiration time\n");
		now = time(NULL);
		if (now == (time_t)-1) {
			fprintf(stdout,
				"time() failed: %s\n", strerror(errno));
			continue;
		}

		expiration = now  + g_timeout_sec;

		fprintf(stdout, "Issue suspend command\n");
		rv = write_string_to_file(NULL, POWER_NODE, "mem\n");
		if (rv < 0) {
			fprintf(stdout,	"cannot write to %s: %s\n",
				POWER_NODE, strerror(-rv));
			i++;
			goto suspend_test_late_bailout;
		}

		k = 0;
		do {
			usleep(300 * 1000);

			if (k % 10 == 0) {
				fprintf(stdout,
					"load power management stats\n");
			}

			rv = read_from_file(NULL, PM_STATS_NODE, g_pm_stats,
					    sizeof(g_pm_stats) - 1);

			if (rv < 0) {
				fprintf(stdout,	"cannot read %s: %s\n",
					PM_STATS_NODE, strerror(-rv));
				goto suspend_test_bailout_cur_iter;
			}

			if (rv == sizeof(g_pm_stats) - 1) {
				fprintf(stdout,	"buffer too small for %s\n",
					PM_STATS_NODE);
				i++;
				goto suspend_test_late_bailout;
			}

			g_pm_stats[rv] = '\0';

			if (k % 10 == 0) {
				fprintf(stdout, "Look for suspend event\n");
			}

			success_count = parse_pm_stats_count(g_pm_stats,
							     "\n[cpu 0] suspend:\n  count: ");

			if (success_count < 0 ) {
				fprintf(stdout,	"bad count(s) from "
					"power management stats\n");
				i++;
				goto suspend_test_late_bailout;
			}

			if (success_count > num_success)
				break;

			if (k % 10 == 0) {
				fprintf(stdout, "Check timeout\n");
			}

			now = time(NULL);
			if (now == (time_t)-1) {
				fprintf(stdout,	"time() failed: %s\n",
					strerror(errno));
				goto suspend_test_bailout_cur_iter;
			}

			k++;
		} while (now < expiration);

suspend_test_bailout_cur_iter:

		if (g_resume_command != NULL) {
			fprintf(stdout, "Issue resume command\n");
			rv = write_string_to_file(
				NULL, POWER_NODE, g_resume_command);

			if (rv < 0) {
				fprintf(stdout,	"cannot write to %s: %s\n",
					POWER_NODE, strerror(-rv));
				i++;
				goto suspend_test_late_bailout;
			}
		}

		if (success_count > num_success) {
			fprintf(stdout, "Suspend/resume succeeded on iteration %d\n", i + 1);
			msg("Suspend/resume succeeded on iteration %d\n", i + 1);
			num_success++;
		}

		if (now >= expiration) {
			fprintf(stdout, "Suspend/resume timed out on iteration %d\n", i + 1);
			msg("Suspend/resume timed out on iteration %\n", i + 1);
			i++;
			break;
		}
	}


	fprintf(stdout, "====BEGIN power management stats====\n");
	fflush(stdout);
	rv = write_to_fd(STDOUT_FILENO, g_pm_stats, strlen(g_pm_stats));
	if (rv < 0)
		fprintf(stdout,	"cannot write out power management stats\n");
	fprintf(stdout, "====END power management stats====\n");

	if (g_wakelock_exists) {
		fprintf(stdout, "====BEGIN wakelock stats====\n");
		fflush(stdout);
		rv = fork_exec("cat", WAKELOCK_NODE, NULL, NULL, 0);
		if (rv < 0)
			fprintf(stdout, "cannot dump %s\n", WAKELOCK_NODE);
		fprintf(stdout, "====END wakelock stats====\n");
	}


suspend_test_late_bailout:

	if (g_resume_command != NULL) {
		fprintf(stdout, "Issue resume command\n");
		rv = write_string_to_file(
			NULL, POWER_NODE, g_resume_command);

		if (rv < 0) {
			fprintf(stdout,	"cannot write to %s: %s\n",
				POWER_NODE, strerror(-rv));
			goto suspend_test_bailout;
		}
	}

suspend_test_bailout:

	fprintf(stdout, "Unset wakeup time\n");
	rv = write_string_to_file(
		g_sys_pm, SLEEP_TIME_OVERRIDE_NODE, "0\n");

	if ((num_cores == 2) || (num_cores == 4)) {
		/* restore idle power collapse */

		if (num_cores == 4) {

			write_int_to_file(g_sys_pm, IDLE_SLEEP_MODE_NODE_CORE_2, 1);
			write_int_to_file(g_sys_pm, IDLE_SLEEP_MODE_NODE_CORE_3, 1);
			write_int_to_file(g_sys_pm, IDLE_SLEEP_MODE_NODE_STD_CORE_2, 1);
			write_int_to_file(g_sys_pm, IDLE_SLEEP_MODE_NODE_STD_CORE_3, 1);
		}

		write_int_to_file(g_sys_pm, IDLE_SLEEP_MODE_NODE_CORE_0, 1);
		write_int_to_file(g_sys_pm, IDLE_SLEEP_MODE_NODE_CORE_1, 1);
		write_int_to_file(g_sys_pm, IDLE_SLEEP_MODE_NODE_STD_CORE_0, 1);
		write_int_to_file(g_sys_pm, IDLE_SLEEP_MODE_NODE_STD_CORE_1, 1);

		fprintf(stdout, "Turn mp-decision back on\n");
		rv = fork_exec("start", "mpdecision", NULL, NULL, 0);
	}
	else if (num_cores == 1) {

		write_int_to_file(g_sys_pm, IDLE_SLEEP_MODE_NODE_CORE_0, 1);
		write_int_to_file(g_sys_pm, IDLE_SLEEP_MODE_NODE_STD_CORE_0, 1);
	}


	fprintf(stdout, "Release suspend wakelock\n");
	rv = write_string_to_file(
		NULL, WAKELOCK_UNLOCK_NODE, SUSPENDPC_WAKELOCK);

	fprintf(stdout, "Restore wakelock debug mask\n");
	if (g_wakelock_exists) {
		rv = write_string_to_file(NULL,
					  WAKELOCK_DEBUG_MASK_NODE, wakelock_dmask);
		if (rv < 0) {
			fprintf(stdout,
				"cannot write to %s: %s\n",
				WAKELOCK_DEBUG_MASK_NODE,
				strerror(-rv));
		}
	}

	fprintf(stdout, "Restore earlysuspend debug mask\n");
	rv = write_string_to_file(NULL,
				  EARLYSUSPEND_DEBUG_MASK_NODE, earlysuspend_dmask);
	if (rv < 0) {
		fprintf(stdout,
			"cannot write to %s: %s\n",
			EARLYSUSPEND_DEBUG_MASK_NODE,
			strerror(-rv));
	}

	fprintf(stdout, "Restore the sensor settings\n");

	fprintf(stdout, "set the sensors data setting\n");
	rv = write_string_to_file(NULL, SENSOR_SETTINGS, "1\n");

	fprintf(stdout, "sleep for sometime for the sensors daemon to pick up the setting\n");
	sleep(2);

suspend_test_early_bailout:


	fprintf(stdout, "====BEGIN kernel log====\n");
	fflush(stdout);
	rv = fork_exec("dmesg", NULL, NULL, NULL, 0);
	if (rv < 0)
		fprintf(stdout,	"cannot dump kernel log\n");
	fprintf(stdout, "====END kernel log====\n");

	fprintf(stdout, "====BEGIN userspace log====\n");
	fflush(stdout);
	rv = fork_exec("logcat", "-d", "-v", "time", 0);
	if (rv < 0)
		fprintf(stdout,	"cannot dump userspace log\n");
	fprintf(stdout, "====END userspace log====\n");

	fprintf(stdout, "\n Suspend/resume test succeeded %d out of %d times\n", num_success, i);


	if (num_success == g_num_iter)
		return EXIT_SUCCESS;
	else
		return EXIT_FAILURE;

}
void MplayerDisplaySurface::flip( ) {
    write_to_fd(_fd);
}
Example #17
0
static void
cpio_append_stat (const char *filename, const struct stat *statbuf)
{
  const char *orig_filename = filename;

  if (*filename == '/')
    filename++;
  if (*filename == '\0')
    filename = ".";

  if (verbose >= 2)
    fprintf (stderr, "cpio_append_stat %s 0%o -> %d\n",
             orig_filename, statbuf->st_mode, out_fd);

  /* Regular files and symlinks are the only ones that have a "body"
   * in this cpio entry.
   */
  int has_body = S_ISREG (statbuf->st_mode) || S_ISLNK (statbuf->st_mode);

  size_t len = strlen (filename) + 1;

  char header[CPIO_HEADER_LEN + 1];
  snprintf (header, sizeof header,
            "070701"            /* magic */
            "%08X"              /* inode */
            "%08X"              /* mode */
            "%08X" "%08X"       /* uid, gid */
            "%08X"              /* nlink */
            "%08X"              /* mtime */
            "%08X"              /* file length */
            "%08X" "%08X"       /* device holding file major, minor */
            "%08X" "%08X"       /* for specials, device major, minor */
            "%08X"              /* name length (including \0 byte) */
            "%08X",             /* checksum (not used by the kernel) */
            (unsigned) statbuf->st_ino, statbuf->st_mode,
            statbuf->st_uid, statbuf->st_gid,
            (unsigned) statbuf->st_nlink, (unsigned) statbuf->st_mtime,
            has_body ? (unsigned) statbuf->st_size : 0,
            major (statbuf->st_dev), minor (statbuf->st_dev),
            major (statbuf->st_rdev), minor (statbuf->st_rdev),
            (unsigned) len, 0);

  /* Write the header. */
  write_to_fd (header, CPIO_HEADER_LEN);

  /* Follow with the filename, and pad it. */
  write_to_fd (filename, len);
  size_t padding_len = PADDING (CPIO_HEADER_LEN + len);
  write_padding (padding_len);

  /* Follow with the file or symlink content, and pad it. */
  if (has_body) {
    if (S_ISREG (statbuf->st_mode))
      write_file_len_to_fd (orig_filename, statbuf->st_size);
    else if (S_ISLNK (statbuf->st_mode)) {
      char tmp[PATH_MAX];
      if (readlink (orig_filename, tmp, sizeof tmp) == -1)
        error (EXIT_FAILURE, errno, "readlink: %s", orig_filename);
      write_to_fd (tmp, statbuf->st_size);
    }

    padding_len = PADDING (statbuf->st_size);
    write_padding (padding_len);
  }
}