HI list.
I want to make the controls files (first virtualdomains) load directly from
a mysql database.
I found that all the reads to control/* files is done by
control.c
so I start modifing it. (attach included)
The first thing I want (before I try to do mysql things) is
hard-code a virtualdomain entry, only for testing.
I think the problem is in the stralloc_copys.
stralloc_copy(sa,&me);
stralloc_0(sa);
stralloc_cats(sa,"szysz.com.ar:alias");
I made a flagme=2 in the qmail-send call to read_file to virtualdomains to
identify this reading from others.
switch(control_readfile(&vdoms,"control/virtualdomains",2))
Now It does not work, but I don't get any errors, and don't read the
control/virtualdomain file.
If someone want's to help with this, please mail me.
I'm doing varius hacks to qmail to integrate with mysql, I've ready a
forwardmysql program.
Javier Szyszlican
web.net.ar
#include "readwrite.h"
#include "open.h"
#include "getln.h"
#include "stralloc.h"
#include "substdio.h"
#include "error.h"
#include "control.h"
#include "alloc.h"
#include "scan.h"
#include <mysql/mysql.h>
#include <stdio.h>
#define TRY_N 10
static char inbuf[64];
static stralloc line = {0};
static stralloc me = {0};
static int meok = 0;
MYSQL real_mysql, *mysql = NULL;
MYSQL_RES *result;
int Mysql_Reconnect()
{
int i ;
i = 0;
while(i<TRY_N) {
mysql_init(&real_mysql);
if (!(mysql=mysql_real_connect(&real_mysql, "localhost", "qmail", "qmail"
,NULL, 3306, NULL, 0))) {
mysql = NULL;
} else {
mysql = &real_mysql;
break;
}
i++;
sleep(3);
}
if (mysql != NULL) {
i = 0;
while(i<TRY_N) {
if (mysql_select_db(mysql, "subdominios")) {
} else {
return(0);
}
i++;
sleep(3);
}
}
return(-1);
}
void My_Mysql_Close(int keepopen_flag)
{
if(keepopen_flag == 0) {
if (mysql != NULL) {
mysql_close(mysql);
}
mysql = NULL;
}
}
static void striptrailingwhitespace(sa)
stralloc *sa;
{
while (sa->len > 0)
switch(sa->s[sa->len - 1])
{
case '\n': case ' ': case '\t':
--sa->len;
break;
default:
return;
}
}
int control_init()
{
int r;
r = control_readline(&me,"control/me");
if (r == 1) meok = 1;
return r;
}
int control_rldef(sa,fn,flagme,def)
stralloc *sa;
char *fn;
int flagme;
char *def;
{
int r;
r = control_readline(sa,fn);
if (r) return r;
if (flagme) if (meok) return stralloc_copy(sa,&me) ? 1 : -1;
if (def) return stralloc_copys(sa,def) ? 1 : -1;
return r;
}
int control_readline(sa,fn)
stralloc *sa;
char *fn;
{
substdio ss;
int fd;
int match;
fd = open_read(fn);
if (fd == -1) { if (errno == error_noent) return 0; return -1; }
substdio_fdbuf(&ss,read,fd,inbuf,sizeof(inbuf));
if (getln(&ss,sa,&match,'\n') == -1) { close(fd); return -1; }
striptrailingwhitespace(sa);
close(fd);
return 1;
}
int control_readint(i,fn)
int *i;
char *fn;
{
unsigned long u;
switch(control_readline(&line,fn))
{
case 0: return 0;
case -1: return -1;
}
if (!stralloc_0(&line)) return -1;
if (!scan_ulong(line.s,&u)) return 0;
*i = u;
return 1;
}
int control_readfile(sa,fn,flagme)
stralloc *sa;
char *fn;
int flagme;
{
substdio ss;
int fd;
int match;
char buf1[1024];
MYSQL_ROW row;
char *b;
if (!stralloc_copys(sa,"")) return -1;
if (flagme == 2) {
/*
Mysql_Reconnect();
sprintf(buf1, "SELECT url FROM clientes");
mysql_query(mysql, buf1) ;
result=mysql_store_result(mysql);
row=mysql_fetch_row(result);
sprintf(buf1,"%s:alias",row[0]);
stralloc_cat(sa,&buf1);
*/
stralloc_copy(sa,&me);
stralloc_0(sa);
stralloc_cats(sa,"szysz.com.ar:alias");
return 1;
//mysql_free_result(result);
//My_Mysql_Close(0);
} else {
fd = open_read(fn);
if (fd == -1)
{
if (errno == error_noent)
{
if (flagme && meok)
{
if (!stralloc_copy(sa,&me)) return -1;
if (!stralloc_0(sa)) return -1;
return 1;
}
return 0;
}
return -1;
}
substdio_fdbuf(&ss,read,fd,inbuf,sizeof(inbuf));
for (;;)
{
if (getln(&ss,&line,&match,'\n') == -1) break;
if (!match && !line.len) { close(fd); return 1; }
striptrailingwhitespace(&line);
if (!stralloc_0(&line)) break;
if (line.s[0])
if (line.s[0] != '#')
if (!stralloc_cat(sa,&line)) break;
if (!match) { close(fd); return 1; }
}
close(fd);
}
return -1;
}