Added postfork

This commit is contained in:
ridiculousfish 2012-02-27 19:20:27 -08:00
parent b17dfff3fd
commit 4e912ef83d
4 changed files with 99 additions and 1 deletions

View file

@ -8,6 +8,8 @@
/* Begin PBXFileReference section */
D03EE83814DF88B200FC7150 /* lru.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = lru.h; sourceTree = "<group>"; };
D09B1C1914FC7B5B00F91077 /* postfork.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = postfork.cpp; sourceTree = "<group>"; };
D09B1C1A14FC7B5B00F91077 /* postfork.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = postfork.h; sourceTree = "<group>"; };
D0A0850313B3ACEE0099B651 /* builtin.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = builtin.h; sourceTree = "<group>"; };
D0A0850413B3ACEE0099B651 /* common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = common.h; sourceTree = "<group>"; };
D0A0850513B3ACEE0099B651 /* complete.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = complete.h; sourceTree = "<group>"; };
@ -182,6 +184,8 @@
D0A0855413B3ACEE0099B651 /* parser.cpp */,
D0A0851E13B3ACEE0099B651 /* path.h */,
D0A0855513B3ACEE0099B651 /* path.cpp */,
D09B1C1A14FC7B5B00F91077 /* postfork.h */,
D09B1C1914FC7B5B00F91077 /* postfork.cpp */,
D0A0851F13B3ACEE0099B651 /* print_help.h */,
D0A0855613B3ACEE0099B651 /* print_help.cpp */,
D0A0852013B3ACEE0099B651 /* proc.h */,

View file

@ -96,7 +96,7 @@ FISH_OBJS := function.o builtin.o complete.o env.o exec.o expand.o \
tokenizer.o wildcard.o wgetopt.o wutil.o input.o output.o intern.o \
env_universal.o env_universal_common.o input_common.o event.o \
signal.o io.o parse_util.o common.o screen.o path.o autoload.o \
parser_keywords.o iothread.o builtin_scripts.o color.o
parser_keywords.o iothread.o builtin_scripts.o color.o postfork.o
FISH_INDENT_OBJS := fish_indent.o print_help.o common.o \
parser_keywords.o wutil.o tokenizer.o

68
postfork.cpp Normal file
View file

@ -0,0 +1,68 @@
/** \file postfork.cpp
Functions that we may safely call after fork().
*/
#include "postfork.h"
/**
This function should be called by both the parent process and the
child right after fork() has been called. If job control is
enabled, the child is put in the jobs group, and if the child is
also in the foreground, it is also given control of the
terminal. When called in the parent process, this function may
fail, since the child might have already finished and called
exit. The parent process may safely ignore the exit status of this
call.
Returns 0 on sucess, -1 on failiure.
*/
int set_child_group( job_t *j, process_t *p, int print_errors )
{
int res = 0;
if( job_get_flag( j, JOB_CONTROL ) )
{
if (!j->pgid)
{
j->pgid = p->pid;
}
if( setpgid (p->pid, j->pgid) )
{
if( getpgid( p->pid) != j->pgid && print_errors )
{
debug( 1,
_( L"Could not send process %d, '%ls' in job %d, '%ls' from group %d to group %d" ),
p->pid,
p->argv0(),
j->job_id,
j->command_cstr(),
getpgid( p->pid),
j->pgid );
wperror( L"setpgid" );
res = -1;
}
}
}
else
{
j->pgid = getpid();
}
if( job_get_flag( j, JOB_TERMINAL ) && job_get_flag( j, JOB_FOREGROUND ) )
{
if( tcsetpgrp (0, j->pgid) && print_errors )
{
debug( 1, _( L"Could not send job %d ('%ls') to foreground" ),
j->job_id,
j->command_cstr() );
wperror( L"tcsetpgrp" );
res = -1;
}
}
return res;
}

26
postfork.h Normal file
View file

@ -0,0 +1,26 @@
/** \file postfork.h
Functions that we may safely call after fork(), of which there are very few. In particular we cannot allocate memory, since we're insane enough to call fork from a multithreaded process.
*/
#ifndef FISH_POSTFORK_H
#define FISH_POSTFORK_H
#include <wchar.h>
#include <signal.h>
#include <unistd.h>
#include <sys/time.h>
#include <list>
#include "config.h"
#include "common.h"
#include "util.h"
#include "proc.h"
#include "wutil.h"
#include "io.h"
int set_child_group( job_t *j, process_t *p, int print_errors );
int setup_child_process( job_t *j, process_t *p );
#endif