<font size="2"><font face="georgia,serif">mysql_fetch_row() returns NULL for no rows.  Your unused "char *limit = row[0]" will fault immediately (row==NULL).<br clear="all"></font></font><font class="Apple-style-span" face="georgia, serif">-- <br>

kb<br></font><br><div class="gmail_quote">On Tue, Jul 26, 2011 at 17:50, Roberto Moutinho <span dir="ltr"><<a href="mailto:robertomoutinho@gmail.com">robertomoutinho@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

Hello,<br><br>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...<br><br>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.<br>


<br>I start with a variable that receive the value of <u>VRT_GetHdr(sp, HDR_REQ, "\012X-API-Key:")</u>   and I would like to consult my database to find out if this key is valid.<br><br>So I started writing a little C code to do it...<br>


<br><i>unsigned int num_rows;<br>MYSQL *conn;<br>MYSQL_RES *result;<br>MYSQL_ROW row;<br>conn = mysql_init(NULL);<br>mysql_real_connect(conn, "Server IP", "username", "password", "database", 0, NULL, 0);<br>


mysql_query(conn, query);<br>result = mysql_store_result(conn);<br>num_rows = mysql_num_rows(result);<br>row = mysql_fetch_row(result);<br>char *limit = row[0];<br>if (num_rows == 0) {<br>   VRT_error(sp, 403, "Forbidden");
<br>   VRT_done(sp, VCL_RET_ERROR);
<br>}<br>mysql_free_result(result);<br>mysql_close(conn);<br>}</i><br><br>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.<br>


<br>The C code works perfectly when compiled with GCC on ubuntu/centos and also compile inside the VCL.<br><br>My daemon startup options are (staging server):<br><br><i>varnishd -s malloc,1G -T <a href="http://127.0.0.1:2000" target="_blank">127.0.0.1:2000</a> -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';</i><br>


<br><br>and this is what I get from /var/log/messages:<br><br><i>Jul 25 19:59:09 api varnishd[6318]: child (6319) Started<br>Jul 25 19:59:09 api varnishd[6318]: Child (6319) said <br>Jul 25 19:59:09 api varnishd[6318]: Child (6319) said Child starts<br>


Jul 25 19:59:09 api varnishd[6318]: Child (6319) said managed to mmap 68230176768 bytes of 68230176768<br>Jul 25 16:59:16 api kernel: varnishd[6726]: segfault at 0000000000000000 rip 0000003839e6f0f1 rsp 00002abb50cb9d90 error 6<br>


Jul 25 19:59:17 api varnishd[6318]: Child (6319) died signal=11<br>Jul 25 19:59:17 api varnishd[6318]: child (7128) Started<br>Jul 25 19:59:17 api varnishd[6318]: Child (7128) said <br>Jul 25 19:59:17 api varnishd[6318]: Child (7128) said Child starts<br>


Jul 25 19:59:17 api varnishd[6318]: Child (7128) said managed to mmap 68230176768 bytes of 68230176768</i><br><br><br>and if I start the daemon with a "-d -d" option I get this:<br><br><i>Varnish on Linux,2.6.18-194.el5,x86_64,-smalloc,-hcritbit<br>


200 233     <br>-----------------------------<br>Varnish Cache CLI 1.0<br>-----------------------------<br>Linux,2.6.18-194.el5,x86_64,-smalloc,-hcritbit<br><br>Type 'help' for command list.<br>Type 'quit' to close CLI session.<br>


Type 'start' to launch worker process.<br><br>start<br>child (13163) Started<br>200 0 <br>Child (13184) died signal=11<br>Child cleanup complete<br>child (13203) Started<br>Child (13203) said <br>Child (13203) said Child starts</i><br>


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


<br><br><br>
<br>_______________________________________________<br>
varnish-misc mailing list<br>
<a href="mailto:varnish-misc@varnish-cache.org">varnish-misc@varnish-cache.org</a><br>
<a href="https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc" target="_blank">https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc</a><br></blockquote></div><br>