Thread: noob questions, fork and mutex?

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    2

    noob questions, fork and mutex?

    Greetings,

    Have written a program in C that takes input from stdin, and puts an answer on stdout. Program is intended to be called by apache webserver, from a RewriteMap;
    RewriteMap prog prg:/usr/local/bin/prog

    In apache, when RewriteRule condition is matched, the program defined in the RewriteMap is passed a string terminated by "\n". Apache expects the answer to be returned as a string or NULL, terminated by "\n".

    When I test my program from command line, it functions as expected. Feed it a properly terminated string, and get an answer back. Feed it bogus string, get NULL back. Feed it an unterminated string, it sits there until enter is pressed. To my eye, it works as intended.

    When apache starts, my program is started. I see it in the process list. When the RewriteMap feeds the string to my program, the browser hangs, waiting for response from apache. No further log output comes from apache, even though logging is jacked way up -
    Loglevel debug rewrite:trace8 .

    I shutdown apache, and I see it say "child process still did not exit, sending SIGTERM". Now I have a clue. My program has no notion of being a child process, so I will go figure out how to add capability for listening to SIG* signals from a parent process.

    There are a couple of known gotchas regarding this RewriteMap technique.

    Gotcha#1 says that buffering must be disabled, otherwise, apache will hang.. Sounds like behavior I see. In attempt to fix, I changed

    printf("NULL\n");
    to
    fprintf(stdout"NULL\n");

    No difference.

    Gotcha#2 says that when using this technique in apache, Mutex must be defined in apache conf specifically for rewrite-map use, allowing apache to invoke its own lockfile management *for the program defined in the RewriteMap*. Otherwise, results from webclientA may end up in webclientB's browser.. I believe I've handled this part correctly, in apache2.conf;
    Mutex file:/var/lock rewrite-map

    I know the apache conf is correct. I made a bash script that does same thing as my prog - script works fine, no hang, results are returned, no SIGTERM issues, apache logging continues..

    Sorry for all the blah-blah but I thought background would be relevant.

    My questions:

    Should I be concerned with 'fork' at this moment?

    My program is not a daemon (as is apache 'httpd'). It loops forever waiting for input, and exits when Ctrl-C is pressed.

    Should my program be made aware of apache's mutex use?

    Any comments are appreciated. Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    When your stdout is a terminal, it is line buffered.
    When your stdout is a file or a pipe, chances are it is fully buffered. Meaning apache will not likely see it until you either close stdout (seems unlikely if you just want your program to sit there), or the pipe fills. Even then, the remainder data will still be stuck.

    tl;dr
    Put this right at the start of the program (before you print anything)
    setvbuf(stdout, NULL, _IOLBF, 0); // put stdout back into line buffering mode
    You might want to do the same for stdin as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    2
    @Salem, Winner, winner: chicken dinner! Thanks much! setvbuf (on stdout) did the trick.

    Once you explained diffs between terminal and file/pipe with some context, I understood.

    Took a few tries to get it placed correctly; eg, don't put it immediately after while(1) :-D don't put it before int main() :-)

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Questions about fork
    By imoimo9945 in forum C Programming
    Replies: 4
    Last Post: 12-01-2011, 01:06 AM
  2. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  3. Mutex, Cond and Thread questions (pthread, linux)
    By thebearot in forum C Programming
    Replies: 14
    Last Post: 04-23-2010, 12:10 PM
  4. Few more noob questions
    By SlpCtrl in forum C Programming
    Replies: 12
    Last Post: 10-14-2008, 04:32 PM
  5. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 06:58 AM

Tags for this Thread