c - Segmentation fault in libev after fork -


i want make example application libev. want have watcher on keyboard input parse several commands "start" "stop" "exit". on "start" want create pipe , fork application start worker(like ffmpeg exec() or loop prints characters) in child , set watcher on pipe in parent print output console. on "stop" command want kill child , remove watcher.

i have implemented program after fork segfault on keyboard input when child running.

at first thought because if stdin can shared between child , parent. i've tried detach child, close stdin in child. tried dup stdin , close default stdin in parent @ beginning , set watcher on duplicated stdin. tried close stdout/err descriptors before forking , restore them in parent after fork.

i stopped , start user_input watcher in user_input callback in case help.

then tried execute ev_default_fork() , ev_loop_fork() in child (this not necessary because want exec() after fork or in case child loop never control) no success.

i tried use different back-ends (select instead of epoll).

also tried ignore signals sighup sigpipe sigchild.

i noticed input after fork() causes segfault , bash command if (with ">" denote self-hand written input , "<" program , system output):

> $ ./libev_example > start < debug: fork data got:  < [data got child through pipe]  > asd < segmentation fault (core dumped) < $ asd < bash: asd: command not found... 

then built libev sources , tried debug. segfault happens in ev.c:1698

  if (expect_false (w_->pending))       pendings [pri][w_->pending - 1].events |= revents;   else 

the pri value 4 , understand it's priority. pendings[4] 0x0 segfault happens. when program doesn't crash code goes in else branch.

the fd epoll returns 0, didn't use 0 fd in event. 0 there watcher in loop->anfds has callback user_input callback. in previous iteration, when enter string there no event 0. checked pipe fds , have number grater 0.

i can't figure whats happen here , i'm doing wrong. can place here code, there nothing special there. post quite big enough if asks code i'll post later.

thanks.

ok. time gdb solved problem. found couple of errors in code has no straight connection libev or fork.

and problem caused such strange behavior mistype didn't noticed due inattention. subclass standard ev_io struct reason made this:

typedef struct lee_user_input_event_t{     struct ev_io *event;     struct lee_process_data_t *child_process_data; ... }; 

instead of this:

typedef struct lee_user_input_event_t{     struct ev_io event;     struct lee_process_data_t *child_process_data; ... } 

so casting event pointer in callback own struct complete , huge disaster.


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -