From 4e912ef83df234d34fff4156cd2bfb165e113674 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Mon, 27 Feb 2012 19:20:27 -0800 Subject: [PATCH] Added postfork --- FishsFish.xcodeproj/project.pbxproj | 4 ++ Makefile.in | 2 +- postfork.cpp | 68 +++++++++++++++++++++++++++++ postfork.h | 26 +++++++++++ 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 postfork.cpp create mode 100644 postfork.h diff --git a/FishsFish.xcodeproj/project.pbxproj b/FishsFish.xcodeproj/project.pbxproj index 8ef0bb0f7..6812233b0 100644 --- a/FishsFish.xcodeproj/project.pbxproj +++ b/FishsFish.xcodeproj/project.pbxproj @@ -8,6 +8,8 @@ /* Begin PBXFileReference section */ D03EE83814DF88B200FC7150 /* lru.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = lru.h; sourceTree = ""; }; + D09B1C1914FC7B5B00F91077 /* postfork.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = postfork.cpp; sourceTree = ""; }; + D09B1C1A14FC7B5B00F91077 /* postfork.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = postfork.h; sourceTree = ""; }; D0A0850313B3ACEE0099B651 /* builtin.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = builtin.h; sourceTree = ""; }; D0A0850413B3ACEE0099B651 /* common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = common.h; sourceTree = ""; }; D0A0850513B3ACEE0099B651 /* complete.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = complete.h; sourceTree = ""; }; @@ -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 */, diff --git a/Makefile.in b/Makefile.in index 5466b67a0..4c14ef46c 100644 --- a/Makefile.in +++ b/Makefile.in @@ -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 diff --git a/postfork.cpp b/postfork.cpp new file mode 100644 index 000000000..2ea88b23d --- /dev/null +++ b/postfork.cpp @@ -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; +} + diff --git a/postfork.h b/postfork.h new file mode 100644 index 000000000..9eaf06e23 --- /dev/null +++ b/postfork.h @@ -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 +#include +#include +#include +#include + +#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