Using MySQL C API inside varnish

Roberto Moutinho robertomoutinho at gmail.com
Wed Jul 27 02:50:20 CEST 2011


Hello,

As I said on an earlier thread... I'm trying to adapt varnish to
authenticate users for my website API and I'm bumping into some troubles...

I receive a header named x-api-key on every request made to the API and I
need to authenticate those api keys before I start delivering cached content
to my clients.

I start with a variable that receive the value of *VRT_GetHdr(sp, HDR_REQ,
"\012X-API-Key:")*  and I would like to consult my database to find out if
this key is valid.

So I started writing a little C code to do it...

*unsigned int num_rows;
MYSQL *conn;
MYSQL_RES *result;
MYSQL_ROW row;
conn = mysql_init(NULL);
mysql_real_connect(conn, "Server IP", "username", "password", "database", 0,
NULL, 0);
mysql_query(conn, query);
result = mysql_store_result(conn);
num_rows = mysql_num_rows(result);
row = mysql_fetch_row(result);
char *limit = row[0];
if (num_rows == 0) {
   VRT_error(sp, 403, "Forbidden");
   VRT_done(sp, VCL_RET_ERROR);
}
mysql_free_result(result);
mysql_close(conn);
}*

If the databse return no rows for the key provided on the request that means
that the key is invalid and I should return a 403 forbidden to the client.

The C code works perfectly when compiled with GCC on ubuntu/centos and also
compile inside the VCL.

My daemon startup options are (staging server):

*varnishd -s malloc,1G -T 127.0.0.1:2000 -P /www/varnishd.pid -f
/etc/varnish/default.vcl -p 'cc_command=exec cc -fpic -shared -Wl,-x
-L/usr/local/include/libmemcached/memcached.h -L/usr/lib64/mysql
-lmysqlclient -lmemcached -o %o %s' -p 'cli_timeout=10' -p
'thread_pool_max=5000' -p 'thread_pools=4' -p 'thread_pool_min=200' -p
'thread_pool_add_delay=1ms' -p 'cli_timeout=1000s' -p 'ping_interval=1' -p
'cli_buffer=16384' -p 'session_linger=20ms' -p 'lru_interval=360s' -p
'listen_depth=8192' -h 'classic,500009';*


and this is what I get from /var/log/messages:

*Jul 25 19:59:09 api varnishd[6318]: child (6319) Started
Jul 25 19:59:09 api varnishd[6318]: Child (6319) said
Jul 25 19:59:09 api varnishd[6318]: Child (6319) said Child starts
Jul 25 19:59:09 api varnishd[6318]: Child (6319) said managed to mmap
68230176768 bytes of 68230176768
Jul 25 16:59:16 api kernel: varnishd[6726]: segfault at 0000000000000000 rip
0000003839e6f0f1 rsp 00002abb50cb9d90 error 6
Jul 25 19:59:17 api varnishd[6318]: Child (6319) died signal=11
Jul 25 19:59:17 api varnishd[6318]: child (7128) Started
Jul 25 19:59:17 api varnishd[6318]: Child (7128) said
Jul 25 19:59:17 api varnishd[6318]: Child (7128) said Child starts
Jul 25 19:59:17 api varnishd[6318]: Child (7128) said managed to mmap
68230176768 bytes of 68230176768*


and if I start the daemon with a "-d -d" option I get this:

*Varnish on Linux,2.6.18-194.el5,x86_64,-smalloc,-hcritbit
200 233
-----------------------------
Varnish Cache CLI 1.0
-----------------------------
Linux,2.6.18-194.el5,x86_64,-smalloc,-hcritbit

Type 'help' for command list.
Type 'quit' to close CLI session.
Type 'start' to launch worker process.

start
child (13163) Started
200 0
Child (13184) died signal=11
Child cleanup complete
child (13203) Started
Child (13203) said
Child (13203) said Child starts*

I understand that signal 11 means that the program accessed a memory
location that was not assigned...and probably died because of it.
Is it really a bug in my code or am I doing something wrong in trying to
make MySQL C API work with varnish ?

ps: sorry for the long message.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20110726/55594a84/attachment-0003.html>


More information about the varnish-misc mailing list