Exemplo n.º 1
0
void tc_server_exec(void)
{
	/* Wait for anything to be received */
	while (true) {
		/* Check if there is any reception event */
		struct pollfd fds[2];	
		fds[0].fd = tc_server_fd;
		fds[0].events = POLLIN;
		fds[1].fd = TC_MSG_QUEUE_POLLFD(&tc_server_queue);
		fds[1].events = POLLIN;
		int r = poll(fds, 2, -1);
		if (fds[0].revents & POLLIN) {
			/* We have received a message */
			struct sockaddr_in src;
			socklen_t src_len = sizeof(src);
			char buf[257];
			ssize_t r = recvfrom(tc_server_fd, buf, sizeof(buf)-1,
			                     0, (struct sockaddr *)&src, &src_len);
			if (r > 0) {
				buf[r] = 0;
				tc_log(TC_LOG_INFO, "Command: \"%s\"", buf);
				int ret = tc_cmd(buf, r);
				if (ret > 0)
					break;
				if (ret < 0)
					tc_log(TC_LOG_ERR, "Error in command: \"%s\"", buf);
			}
		}
		if (fds[1].revents & POLLIN) {
			/* We have received an event */
			tc_msg_t msg;
			if (tc_msg_recv(&tc_server_queue, &msg)) {
				tc_log(TC_LOG_ERR, "server: Error reading from event pipe");
				break;
			}
			/* Execute the event */
			tc_log(TC_LOG_INFO, "Event: \"%s\"", msg.buf);
			int ret = tc_cmd((const char *)msg.buf, msg.len);
			if (ret > 0)
				break;
			if (ret < 0)
				tc_log(TC_LOG_ERR, "Error in event: \"%s\"", msg.buf);
		}
	}
}
Exemplo n.º 2
0
/**
 *  Internal function to execute an scripting command.
 *
 *  \param cmd  Pointer to the command to execute.
 *  \param buf  Buffer with the command to execute.
 *  \param len  Length of the command to execute
 *  \retval 0 on success of normal command.
 *  \retval 1 on exit command.
 *  \retval -1 on error in command.
 */
static int tc_cmd_script_exec(tc_cmd_t *cmd, const char *buf, uint32_t len)
{
	tc_cmd_script_t *s = tc_containerof(cmd, tc_cmd_script_t, cmd);
	tc_cmd_script_entry_t *e = s->entry;
	while (e) {
		tc_log(TC_LOG_INFO, "Subcommand \"%s\"", e->cmd);
		int r = tc_cmd(e->cmd, strlen(e->cmd));
		if (r) {
			if (r < 0)
				tc_log(TC_LOG_ERR, "Error in subcommand \"%s\"",
				       e->cmd);
			return r;
		}
		e = e->next;
	}
	return 0;
}
Exemplo n.º 3
0
/**
 *  Load a file to add new commands 
 *
 *  \param path   Path of the file with the new commands.
 *  \retval -1 on error (with a log entry).
 *  \retval 1 on file not possible to be read.
 *  \retval 0 on success.
 */
static int tc_cmd_load(const char *path)
{
	/* Try to open the file */
	FILE *f = fopen(path, "rt");
	if (!f) {
		tc_log(TC_LOG_WARN, "Script file \"%s\" not present",
		       path);
		return 1;
	}
	tc_log(TC_LOG_INFO, "Executing script file \"%s\"", path);

	/* Try to read a line until it finishes */
	int result = 0;
	while (true) {
		/* Get a new line without carry return */
		char buf[256];
		char *r = fgets(buf, sizeof(buf), f);
		if (!r)
			break;
		int buf_len = strlen(buf);
		if (!buf_len)
			continue;
		if (buf[buf_len-1] == '\n') {
			buf[--buf_len] = 0;
			if (!buf_len)
				continue;
		}

		/* Execute the commands */
		tc_log(TC_LOG_INFO, "Command \"%s\"", strndupa(buf, buf_len));
		int nr = tc_cmd(buf, buf_len);
		if (nr) {
			if (nr < 0)
				result = -1;
			break;
		}
	}

	/* Close the file */
	fclose(f);

	/* Return the result */
	return result;
}