logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Building & Member Downloads View previous topic :: View next topic
Reply to topic   Topic: [Apache 2.4] Segmentation fault on writting a file
Author
DoHITB



Joined: 16 Feb 2017
Posts: 8
Location: Catalonia, Lleida

PostPosted: Fri 23 Apr '21 21:03    Post subject: [Apache 2.4] Segmentation fault on writting a file Reply with quote

Hello:

I'm working on a custom module that writes a information file (just to check if working as desired).

My main function looks like

Code:

static int myhandler(request_rec *r){
  int mci = 0;
  int isRef = 0;
  const char *ref;

  FILE *tmp;

  tmp = fopen("/var/www/conf/my-file.log", "w");

  if(tmp == 0){
    ap_rprintf(r, " tmp is null ");

    return OK;
  }

  fprintf(tmp, "Getting data...\n");

  ref = apr_table_get(r->headers_in, "custom-header");

  //If there's not custom-header, just end
  if(ref == NULL){
    fprintf(tmp, "No custom-header\n");
    fclose(tmp);

    return OK;
  }

  fprintf(tmp, "custom-header: %s\n", ref);

  fclose(tmp);

  return OK;
}


So, if I access "http://my-web..../somepage", file "/var/www/conf/my-file.log" contains information, but if I access same URL via AJAX, it writes nothing (not even the first line), and Apache log shows a "segmentation fault (11)".

I did "chown www-data:www-data /var/www/conf" and "chmod 777 /var/www/conf" to allow file writting.

Also, as I said ,when accessing via URL it writes, so maybe there's something I'm missing...

May anyone please help?

Thanks!
Back to top
tangent
Moderator


Joined: 16 Aug 2020
Posts: 348
Location: UK

PostPosted: Fri 23 Apr '21 22:47    Post subject: Reply with quote

You've only provided a snippet of your module code (for example, myhandler() defines mci and isRef variables, which aren't used), so it's challenging to say what's causing your segmentation fault (indicating your module is accessing unallocated memory).

I'd also question that you've chosen standard C file handling functions rather than apr ones such as apr_file_open(), etc., which will cope with concurrency.

Is the file handling core to your module function, or just an aid to debugging?
Back to top
DoHITB



Joined: 16 Feb 2017
Posts: 8
Location: Catalonia, Lleida

PostPosted: Sun 25 Apr '21 14:20    Post subject: Reply with quote

Hello @tangent

Thanks for replying. I was using standard C functions as I only left the file for a test (I needed to trace some info without putting it on screen).

Also, didn't know about apr_file_open, so I will take a look if I need to concurrently open the same file Smile

About the segfault, I already figured it out (I was commenting code lines to see where the error was). I thought that the error was on the write because the output file shows empty; but the error was way after (I suposse the write was cached, so that's why I saw an empty file). The problem was obvious once I move forward to the write itself...

Thanks for letting me know about apr functions! I'm a bit new to this world and every ounce of knowledge is good Smile
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Sun 25 Apr '21 19:39    Post subject: Reply with quote

You can find some good examples at https://github.com/apache/httpd/tree/trunk/modules/examples

and for the writing with apr see https://github.com/JBlond/mod_log_rotate/blob/master/src/mod_log_rotate.c around line 277 IIRC
Back to top


Reply to topic   Topic: [Apache 2.4] Segmentation fault on writting a file View previous topic :: View next topic
Post new topic   Forum Index -> Building & Member Downloads