Пример #1
0
//
// Run a simple shell command, and return the output to webOS.
//
static bool simple_command(LSHandle* lshandle, LSMessage *message, char *command) {
  LSError lserror;
  LSErrorInit(&lserror);

  char run_command_buffer[MAXBUFLEN];

  // Initialise the output buffer
  strcpy(run_command_buffer, "{\"stdOut\": [");

  // Run the command
  if (run_command(command, true, run_command_buffer)) {

    // Finalise the message ...
    strcat(run_command_buffer, "], \"returnValue\": true}");

    // fprintf(stderr, "Message is %s\n", run_command_buffer);

    // and send it to webOS.
    if (!LSMessageReply(lshandle, message, run_command_buffer, &lserror)) goto error;
  }
  else {

    // Finalise the command output ...
    strcat(run_command_buffer, "]");

    // and use it in a failure report message.
    if (!report_command_failure(lshandle, message, command, run_command_buffer+11, NULL)) goto end;
  }

  return true;
 error:
  LSErrorPrint(&lserror, stderr);
  LSErrorFree(&lserror);
 end:
  return false;
}
Пример #2
0
bool get_file_method(LSHandle* lshandle, LSMessage *message, void *ctx) {
  LSError lserror;
  LSErrorInit(&lserror);

  char run_command_buffer[MAXBUFLEN];
  char command[MAXLINLEN];

  json_t *object = json_parse_document(LSMessageGetPayload(message));
  json_t *id;

  // Extract the filename argument from the message
  id = json_find_first_label(object, "filename");
  if (!id || (id->child->type != JSON_STRING) ||
      (strlen(id->child->text) >= MAXNAMLEN) ||
      (strspn(id->child->text, ALLOWED_CHARS) != strlen(id->child->text))) {
    if (!LSMessageRespond(message,
			"{\"returnValue\": false, \"errorCode\": -1, "
			"\"errorText\": \"Invalid or missing filename parameter\", "
			"\"stage\": \"failed\"}",
			&lserror)) goto error;
    return true;
  }
  char filename[MAXNAMLEN];
  sprintf(filename, "/media/internal/.temp/%s", id->child->text);

  // Extract the url argument from the message
  id = json_find_first_label(object, "url");               
  if (!id || (id->child->type != JSON_STRING) ||
      (strlen(id->child->text) >= MAXLINLEN)) {
    if (!LSMessageRespond(message,
			"{\"returnValue\": false, \"errorCode\": -1, "
			"\"errorText\": \"Invalid or missing url parameter\", "
			"\"stage\": \"failed\"}",
			&lserror)) goto error;
    return true;
  }
  char url[MAXLINLEN];
  strcpy(url, id->child->text);

  if (!strncmp(url, "file://", 7)) {
    strcpy(filename, url+7);
  }
  else {

    /* Download the package */

    snprintf(command, MAXLINLEN,
	     "/usr/bin/curl --create-dirs --insecure --location --fail --show-error --output %s %s 2>&1", filename, url);

    strcpy(run_command_buffer, "{\"stdOut\": [");
    if (run_command(command, true, run_command_buffer)) {
      strcat(run_command_buffer, "], \"returnValue\": true, \"stage\": \"download\"}");
      if (!LSMessageRespond(message, run_command_buffer, &lserror)) goto error;
    }
    else {
      strcat(run_command_buffer, "]");
      if (!report_command_failure(message, command, run_command_buffer+11, "\"stage\": \"failed\"")) goto end;
      return true;
    }
  }

  return read_file(message, filename, true);

 error:
  LSErrorPrint(&lserror, stderr);
  LSErrorFree(&lserror);
 end:
  return false;
}
Пример #3
0
//
// Dump the contents of an sqlite3 database table
//
static bool dump_sqlite(LSMessage *message, char *database, char *table) {
  LSError lserror;
  LSErrorInit(&lserror);

  char buffer[MAXBUFLEN];
  char esc_buffer[MAXBUFLEN];

  char line[MAXLINLEN];

  // Local buffer to store the command
  char command[MAXLINLEN];

  sprintf(command, "sqlite3 %s .dump 2>&1", database);

  // Is this the first line of output?
  bool first = true;

  // Was there an error in accessing any of the files?
  bool error = false;

  // Length of buffer before the last command
  int lastlen = 0;

  // Start execution of the command to list the config files.
  FILE *fp = popen(command, "r");

  // If the command cannot be started
  if (!fp) {

    // then report the error to webOS.
    if (!report_command_failure(message, command, NULL, NULL)) goto end;

    // The error report has been sent, so return to webOS.
    return true;
  }

  if (!LSMessageRespond(message, "{\"stage\": \"start\", \"returnValue\": true}", &lserror)) goto error;

  // Initialise the output message.
  strcpy(buffer, "{");
  lastlen = strlen(buffer);

  // Loop through the list of files in the scripts directory.
  while (fgets( line, sizeof line, fp)) {

    // Chomp the newline
    char *nl = strchr(line,'\n'); if (nl) *nl = 0;

    if ((strlen(line) <= 13+strlen(table)+9) ||
	strncmp(line, "INSERT INTO \"", 13) ||
	strncmp(line+13, table, strlen(table)) ||
	strncmp(line+13+strlen(table), "\" VALUES(", 9) ||
	strncmp(line+strlen(line)-2, ");", 2)) {
      continue;
    }

    *(line+strlen(line)-2) = 0;

    // Push out a partial chunk
    if (strlen(buffer) >= CHUNKSIZE) {

      // Terminate the JSON array
      if (!first) {
	strcat(buffer, "], ");
      }

      strcat(buffer, "\"stage\": \"middle\", ");

      // Check the error status, and return the current error status
      if (error) {
	strcat(buffer, "\"returnValue\": false}");
      }
      else {
	strcat(buffer, "\"returnValue\": true}");
      }
      
      // fprintf(stderr, "Message is %s\n", buffer);

      // Return the results to webOS.
      if (!LSMessageRespond(message, buffer, &lserror)) goto error;

      // This is now the first line of output
      first = true;

      // Initialise the output message.
      strcpy(buffer, "{");
      lastlen = strlen(buffer);
    }

    // Start or continue the JSON array
    if (first) {
      strcat(buffer, "\"results\": [");
      lastlen = strlen(buffer);
      first = false;
    }
    else if (strlen(buffer) > lastlen) {
      strcat(buffer, ", ");
      lastlen = strlen(buffer);
    }

    // Store the command output
    strcat(buffer, "\"");
    strcat(buffer, json_escape_str(line+13+strlen(table)+9, esc_buffer));
    strcat(buffer, "\"");
  }

  // Terminate the JSON array
  if (!first) {
    strcat(buffer, "], ");
  }

  strcat(buffer, "\"stage\": \"end\", ");

  // Check the close status of the process, and return the combined error status
  if (pclose(fp) || error) {
    strcat(buffer, "\"returnValue\": false}");
  }
  else {
    strcat(buffer, "\"returnValue\": true}");
  }

  // fprintf(stderr, "Message is %s\n", buffer);

  // Return the results to webOS.
  if (!LSMessageRespond(message, buffer, &lserror)) goto error;

  return true;
 error:
  LSErrorPrint(&lserror, stderr);
  LSErrorFree(&lserror);
 end:
  return false;
}