Пример #1
0
static void message_deep_delete(message_t * message) {
	if (message->next != NULL) {
		message_deep_delete(message->next);
	}

	message_delete(message);
}
Пример #2
0
void msgqueue_undo(msgqueue_t *msgqueue) {

	bool ints = kset_ints(FALSE);

	message_t *message = msgqueue->last;

	if (message == NULL) {
		kset_ints(ints);
		return;
	}

	msgqueue->last = message->prev;

	if (msgqueue->last == NULL) {
		msgqueue->first = NULL;
	}

	msgqueue->size--;

	message_delete(message);

	//kdebug("Deshaciendo agragado de mensaje. Nuevo size: ");
	// kdebug_base(msgqueue->size, 10);
	// kdebug_nl();

	kset_ints(ints);
}
Пример #3
0
/* note:  save_new_message -must- abort if this funx returns 0.  */
unsigned int
get_new_message_id(unsigned int quadno)
{
    room_t *p;

    (void) mono_lock_rooms(1);

    /* for convenience */
    p = &shm->rooms[quadno];

    /* first step: increase highest postnumber */
    p->highest++;

    if (!p->highest) {  
	if (p->lowest > p->highest)    /* rollover */
	    log_it("forums", "forum %u has rolled over..  convert it!", quadno);
	return 0;
    }	

    /* next, check if we should increase the lowest number */
    if ((p->highest - p->lowest) > p->maxmsg) {
	message_delete(quadno, p->lowest);
	p->lowest++;
    }
    (void) mono_lock_rooms(0);

    mono_sql_t_updated_highest( quadno, p->highest );
    return p->highest;
}
Пример #4
0
void* msgqueue_deq(msgqueue_t *msgqueue) {

	void* msg;
	message_t *message;

	bool ints = kset_ints(FALSE);

	while (msgqueue->first == NULL);

	message = msgqueue->first;

	msg = malloc(message->size);
	memcpy(msg, message->msg, message->size);

	msgqueue->first = message->next;

	if (msgqueue->first == NULL) {
		//kdebug("Ultimo mensaje de la cola eliminado.\n");
		msgqueue->last = NULL;
	}

	message_delete(message);

	msgqueue->size--;

	//kdebug("Desencolando mensaje. Size: ");
	// kdebug_base(msgqueue->size, 10);
	// kdebug_nl();

	kset_ints(ints);

	return msg;
}
Пример #5
0
static void message_send_and_delete(DsmeDbusMessage* msg)
{
  // TODO: check for errors and log them
  dbus_connection_send(msg->connection, msg->msg, 0);
  dbus_connection_flush(msg->connection);

  message_delete(msg);
}
Пример #6
0
void *message_travel(void *arg)
{
	struct message_t *msg;
	while(1) {
		sleep(argument.msgitv);
		if(head == NULL) continue;
		while((msg = message_delete())) {
			msg_proc((void *)msg->data, msg->len, msg->proto);
			message_free(msg);
		}
		sys_debug("Message travel pthreads (next %d second later)\n", 
			argument.msgitv);
	}
	return NULL;
}
Пример #7
0
void pthread_receive_from_tty()
{
	int fd;
	int nread;
	int i = 0;
	char buff[500];
	char *pbuf;
	char *dev = "/dev/ttyUSB0";
	int baud = 9600;

	DBG("\ninit tty begins\n");
	fd = tty_open_dev(dev);
	if(fd == -1)
	{
		DBG("\nOpen dev failed\n");
		exit(-1);
	}
	DBG("\nopen success\n");

	if(tty_set_speed(fd, baud) != 0)
	{
		DBG("\nSet speed failed\n");
		exit(-1);
	}
	DBG("\nset speed success\n");

	if(tty_set_parity(fd, 8, 1, 'N') != 0)
	{
		DBG("\nSet parity failed\n");
	}
	DBG("\nset parity success\n");

	_tty_fd = fd;

	while(1)
	{
		pbuf = buff;		//reset the pbuf point to the beginning of buff
		int n;
		while(1)
		{
			n = read(fd, pbuf, 1);
			if(*pbuf++ == '$')
			{
				*pbuf = '\0';
				break;
			}
		}
		printf("%s, the size is %ld\n", buff, pbuf - buff);

		if(is_ack_msg(buff, pbuf - buff))	//if this is an ACK message, read another message 
		{
			send_sockfd = find_client_to_send(head_message, buff, strlen(buff));
			pbuf = buff;		//reset the pbuf point to the beginning of buff
			while(1)
			{
				read(fd, pbuf, 1);
				if(*pbuf++ == '$')
				{
					*pbuf = '\0';
					break;
				}
			}
			//send to client
			int vv = client_send(send_sockfd, buff, strlen(buff));

			printf("second time: %s, the size is %ld\n", buff, pbuf - buff);
			printf("second time: %d, the send_sockfd is %d\n", vv, send_sockfd);

			pthread_mutex_lock(&mutex_head_message);	//lock the gloable variable head_message
			message_delete(head_message, send_sockfd);
			pthread_mutex_unlock(&mutex_head_message);	//unlock the gloable variable head_message
		}
	}
}
Пример #8
0
int main(int argc, char *argv[])
{
	int errn;
	string *str;
	int complete = -1;
	int i;
	long ans;
	long logintimeout = 20;
	char *s;
	char *password;
	char *username;

	if(argc < 4)
	{
		fprintf(stderr, "Usage: %s user password connecstring\n", argv[0]);
		exit(1);
	}

	if((mes = new_message(0)) == NULL)
	{
		fprintf(stderr, "%s: Cannot open message queue\n", argv[0]);
		exit(1);
	}


	/*
	 * I believe a fork duplicates malloced stuff, I am in the poo if it does not :-)
	 */
	errn = fork();

	if(errn == -1)
	{
		fprintf(stderr, "%s: Cannot fork child process\n", argv[0]);
		exit(1);
	}
	else if(errn)
	{
/*
 * This is the parent - which behaves like clien
 */

		/*
		 * Initially just a status message is snt back, zero string length 
		 *
		 * Still needs a string to put it into
		 */


		str = new_string();
		
		if(message_receive(mes, str, &complete, MES_SERVER_TO_CLIENT) < 0)
		{
			fprintf(stderr, "%s: %s\n", argv[0], string_s(str));
			message_destroy(mes);
			message_delete(mes);
			string_delete(str);
			exit(1);
		}
		else
		{
			printf("%d\n",message_id(mes)) ;
			message_delete(mes);
			string_delete(str);
			exit(0);
		}
	}
/*
 * All parents have exited now
 */

/*
 * This is all a child
 */

	/*
	 * I think I need to do this here...
	 * I do not fully understand why, it just works!
	 * otherwise shell parent hangs!
	 */

	setsid();
	if(fork()) exit(0);
	close(0);
	close(1);
	close(2);

	/*
	 * We are in daemon mode now
	 */

	/*
	 * open the database
	 */

	str = new_string();

	for(i=3;i<argc;i++)
	{
		string_cat(str, argv[i]);
		if(i < argc - 1) string_cat_c(str, ' ');
	}

	username = strdup(argv[1]);
	password = strdup(argv[2]);

	wipe(argv[2]);
	wipe(argv[1]);

	ans = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &(env));
	if((ans != SQL_SUCCESS) && (ans != SQL_SUCCESS_WITH_INFO))
	{
		message_status(mes, 127, "ODBC: Cannot allocate environment handle\n", MES_SERVER_TO_CLIENT);
		message_delete(mes);
		free(username);
		free(password);
		exit(1);
	}

	ans = SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0); 

	if ((ans != SQL_SUCCESS) && (ans != SQL_SUCCESS_WITH_INFO))
	{
		SQLFreeHandle(SQL_HANDLE_ENV, env);
		message_status(mes, 127, "ODBC: Cannot set environment handle attributes\n", MES_SERVER_TO_CLIENT);
		message_delete(mes);
		exit(1);
	}

	/* 2. allocate connection handle, set timeout */

	ans = SQLAllocHandle(SQL_HANDLE_DBC, env, &(hdbc)); 

	if ((ans != SQL_SUCCESS) && (ans != SQL_SUCCESS_WITH_INFO))
	{
		SQLFreeHandle(SQL_HANDLE_ENV, env);
		message_status(mes, 127, "ODBC: Cannot allocate database handle\n", MES_SERVER_TO_CLIENT);
		message_delete(mes);
		free(username);
		free(password);
		exit(1);
	}

	/*
 	 * TODO - Parameterize ODBC_TIMEOUT as environment variable
	 */

	s = getenv("ODBC_TIMEOUT");
	if(s != NULL)
	{
		if(*s)
		{
			logintimeout = atol(s);
		}
	}

	SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)logintimeout, 0);

	/* 3. Connect to the datasource  */

	ans = SQLConnect(hdbc, (SQLCHAR*) string_s(str), SQL_NTS,
                                     (SQLCHAR*) username, SQL_NTS,
                                     (SQLCHAR*) password, SQL_NTS);

	if ((ans != SQL_SUCCESS) && (ans != SQL_SUCCESS_WITH_INFO))
	{
		message_status(mes, 127, "ODBC: Cannot connect to database\n", MES_SERVER_TO_CLIENT);
		message_delete(mes);
/*
		SQLGetDiagRec(SQL_HANDLE_DBC, V_OD_hdbc,1, 
		              V_OD_stat, &V_OD_err,V_OD_msg,100,&V_OD_mlen);
		printf("%s (%d)\n",V_OD_msg,V_OD_err);
 */
		SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
		SQLFreeHandle(SQL_HANDLE_ENV, env);
		free(username);
		free(password);
		exit(1);
	}


	/*
	 * Transmit to parent that we are hunky dory
	 */

	message_status(mes, 0, "", MES_SERVER_TO_CLIENT);

	/*
	 * We are open for business - Lets go
 	 */

	mainloop();

	/*
	 * At the end, tidy up
	 */

	SQLDisconnect(hdbc);
	SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
	SQLFreeHandle(SQL_HANDLE_ENV, env);

	message_destroy(mes);
	message_delete(mes);

	free(username);
	free(password);

	exit(0);
}
Пример #9
0
int main(int argc, char *argv[])
{
	int errn;
	string *str;
	int complete = -1;
	int i;

	if(argc < 2)
	{
		fprintf(stderr, "Usage: %s connecstring\n", argv[0]);
		exit(1);
	}

	if((mes = new_message(0)) == NULL)
	{
		fprintf(stderr, "%s: Cannot open message queue\n", argv[0]);
		exit(1);
	}


	/*
	 * I believe a fork duplicates malloced stuff, I am in the poo if it does not :-)
	 */
	errn = fork();

	if(errn == -1)
	{
		fprintf(stderr, "%s: Cannot fork child process\n", argv[0]);
		exit(1);
	}
	else if(errn)
	{
/*
 * This is the parent - which behaves like clien
 */

		/*
		 * Initially just a status message is snt back, zero string length 
		 *
		 * Still needs a string to put it into
		 */


		str = new_string();
		
		if(message_receive(mes, str, &complete, MES_SERVER_TO_CLIENT) < 0)
		{
			fprintf(stderr, "%s: %s\n", argv[0], string_s(str));
			message_destroy(mes);
			message_delete(mes);
			string_delete(str);
			exit(1);
		}
		else
		{
			printf("%d\n",message_id(mes)) ;
			message_delete(mes);
			string_delete(str);
			exit(0);
		}
	}
/*
 * All parents have exited now
 */

/*
 * This is all a child
 */

	/*
	 * I think I need to do this here...
	 * I do not fully understand why, it just works!
	 * otherwise shell parent hangs!
	 */

	setsid();
	if(fork()) exit(0);
	close(0);
	close(1);
	close(2);

	/*
	 * We are in daemon mode now
	 */

	/*
	 * open the database
	 */

	str = new_string();

	for(i=1;i<argc;i++)
	{
		string_cat(str, argv[i]);
		if((!strncasecmp(argv[i], "password", 8))
		 || (!strncasecmp(argv[i], "user", 4))
		 || (!strncasecmp(argv[i], "useriname", 8)))
			wipe(argv[i]);
		if(i < argc - 1) string_cat_c(str, ' ');
	}

	sqldb = PQconnectdb(string_s(str));
	string_delete(str);

	if(sqldb == NULL)
	{
		message_status(mes, 127, "Error allocating connection\n", MES_SERVER_TO_CLIENT);
		message_delete(mes);
		exit(1);
	}
	if (PQstatus(sqldb) != CONNECTION_OK)
	{
		char ws[64];
		snprintf(ws, 63, "Error opening postgres %s/%s\n", PQhost(sqldb), PQdb(sqldb));
		ws[63] = 0;
		message_status(mes, 127, ws, MES_SERVER_TO_CLIENT);
		PQfinish(sqldb);
		message_delete(mes);
		exit(1);
	}

	/*
	 * Transmit to parent that we are hunky dory
	 */

	message_status(mes, 0, "", MES_SERVER_TO_CLIENT);

	/*
	 * We are open for business - Lets go
 	 */

	mainloop();

	/*
	 * At the end, tidy up
	 */

	PQfinish(sqldb);
	message_destroy(mes);
	message_delete(mes);
	exit(0);
}
Пример #10
0
int main(int argc, char *argv[])
{
	int errn;
	string *str;
	int complete = -1;
	int i;

	
	char *sqlname[] = {
		"_dummy",
		"host",
		"user",
		"password",
		"dbname",
		"port",
		"socket",
		"flag"
	};

	sqlarg *sarg;


	/*
	 * Username and password may be in the connection string
	 */

	long flag = 0;
	unsigned int port = 0;

	MYSQL *tdb;

	if(argc < 2)
	{
		fprintf(stderr, "Usage: %s connecstring\n", argv[0]);
		exit(1);
	}

	if((mes = new_message(0)) == NULL)
	{
		fprintf(stderr, "%s: Cannot open message queue\n", argv[0]);
		exit(1);
	}


	/*
	 * I believe a fork duplicates malloced stuff, I am in the poo if it does not :-)
	 */
	errn = fork();

	if(errn == -1)
	{
		fprintf(stderr, "%s: Cannot fork child process\n", argv[0]);
		exit(1);
	}
	else if(errn)
	{
/*
 * This is the parent - which behaves like clien
 */

		/*
		 * Initially just a status message is snt back, zero string length 
		 *
		 * Still needs a string to put it into
		 */


		str = new_string();
		
		if(message_receive(mes, str, &complete, MES_SERVER_TO_CLIENT) < 0)
		{
			fprintf(stderr, "%s: %s\n", argv[0], string_s(str));
			message_destroy(mes);
			message_delete(mes);
			string_delete(str);
			exit(1);
		}
		else
		{
			printf("%d\n",message_id(mes)) ;
			message_delete(mes);
			string_delete(str);
			exit(0);
		}
	}
/*
 * All parents have exited now
 */

/*
 * This is all a child
 */

	/*
	 * I think I need to do this here...
	 * I do not fully understand why, it just works!
	 * otherwise shell parent hangs!
	 */

	setsid();
	if(fork()) exit(0);
	close(0);
	close(1);
	close(2);

	/*
	 * We are in daemon mode now
	 */

	/*
	 * open the database
	 */

	str = new_string();

	for(i=1;i<argc;i++)
	{
		string_cat(str, argv[i]);
		if((!strncasecmp(argv[i], "password", 8))
		 || (!strncasecmp(argv[i], "user", 4))
		 || (!strncasecmp(argv[i], "username", 8)))
			wipe(argv[i]);
		if(i < argc - 1) string_cat_c(str, ' ');
	}

	sarg = new_sqlarg(sqlname, string_s(str), SMYSQL_PARAMETERS);

	string_delete(str);

	if(sarg == NULL)
	{
		message_status(mes, 127, "Error allocating string parameters\n", MES_SERVER_TO_CLIENT);
		message_delete(mes);
		exit(1);
	}

	if(sqlargi(sarg, SMYSQL_PORT) != NULL) port = atoi(sqlargi(sarg, SMYSQL_PORT));
	if(sqlargi(sarg, SMYSQL_FLAG) != NULL) flag = atol(sqlargi(sarg, SMYSQL_FLAG));


	sqldb = mysql_init(NULL);
	if(sqldb == NULL)
	{
		message_status(mes, 127, "Error allocating connection\n", MES_SERVER_TO_CLIENT);
		sqlarg_delete(sarg);
		message_delete(mes);
		exit(1);
	}

	tdb = mysql_real_connect(sqldb, sqlargi(sarg, SMYSQL_HOST), sqlargi(sarg, SMYSQL_USER), sqlargi(sarg, SMYSQL_PASSWORD),
					  sqlargi(sarg, SMYSQL_DBNAME), port, sqlargi(sarg, SMYSQL_SOCKET), flag);

	sqlarg_delete(sarg);

	

	if(tdb == NULL)
	{
		char ws[128];
		snprintf(ws, 127, "Error opening mysql: %s\n", mysql_error(sqldb));
		ws[127] = 0;
		message_status(mes, 127, ws, MES_SERVER_TO_CLIENT);
		mysql_close(sqldb);
		message_delete(mes);
		exit(1);
	}

	/*
	 * Transmit to parent that we are hunky dory
	 */

	message_status(mes, 0, "", MES_SERVER_TO_CLIENT);

	/*
	 * We are open for business - Lets go
 	 */

	mainloop();

	/*
	 * At the end, tidy up
	 */

	mysql_close(sqldb);
	message_destroy(mes);
	message_delete(mes);
	exit(0);
}