Revision: 23408 http://trac.macosforge.org/projects/launchd/changeset/23408 Author: zarzycki@apple.com Date: 2007-09-18 18:00:28 -0700 (Tue, 18 Sep 2007) Log Message: ----------- We need to wrap kill()/killpg() to get ESRCH returned instead of EPERM. EPERM is still returned when appropriate. Modified Paths: -------------- trunk/launchd/src/Makefile.am trunk/launchd/src/Makefile.in trunk/launchd/src/launchd.c trunk/launchd/src/launchd_core_logic.c trunk/launchd/src/launchd_runtime.h Added Paths: ----------- trunk/launchd/src/launchd_runtime_kill.c trunk/launchd/src/launchd_runtime_kill.h Modified: trunk/launchd/src/Makefile.am =================================================================== --- trunk/launchd/src/Makefile.am 2007-09-18 19:59:38 UTC (rev 23407) +++ trunk/launchd/src/Makefile.am 2007-09-19 01:00:28 UTC (rev 23408) @@ -48,7 +48,7 @@ launchd_CFLAGS = -mdynamic-no-pic $(AM_CFLAGS) -Wno-unused-parameter launchd_LDFLAGS = -lbsm -launchd_SOURCES = launchd.c launchd_core_logic.c launchd_unix_ipc.c protocol_vprocServer.c notifyServer.c launchd_internalUser.c launchd_internalServer.c job_replyUser.c launchd_runtime.c +launchd_SOURCES = launchd.c launchd_core_logic.c launchd_unix_ipc.c protocol_vprocServer.c notifyServer.c launchd_internalUser.c launchd_internalServer.c job_replyUser.c launchd_runtime.c launchd_runtime_kill.c launchd_runtime.c:: notifyServer.h launchd_internal.h launchd_core_logic.c:: protocol_vproc.h job_reply.h protocol_vprocServer.h Modified: trunk/launchd/src/Makefile.in =================================================================== --- trunk/launchd/src/Makefile.in 2007-09-18 19:59:38 UTC (rev 23407) +++ trunk/launchd/src/Makefile.in 2007-09-19 01:00:28 UTC (rev 23408) @@ -96,7 +96,7 @@ am__launchd_SOURCES_DIST = launchd.c launchd_core_logic.c \ launchd_unix_ipc.c protocol_vprocServer.c notifyServer.c \ launchd_internalUser.c launchd_internalServer.c \ - job_replyUser.c launchd_runtime.c + job_replyUser.c launchd_runtime.c launchd_runtime_kill.c @LIBS_ONLY_FALSE@am_launchd_OBJECTS = launchd-launchd.$(OBJEXT) \ @LIBS_ONLY_FALSE@ launchd-launchd_core_logic.$(OBJEXT) \ @LIBS_ONLY_FALSE@ launchd-launchd_unix_ipc.$(OBJEXT) \ @@ -105,7 +105,8 @@ @LIBS_ONLY_FALSE@ launchd-launchd_internalUser.$(OBJEXT) \ @LIBS_ONLY_FALSE@ launchd-launchd_internalServer.$(OBJEXT) \ @LIBS_ONLY_FALSE@ launchd-job_replyUser.$(OBJEXT) \ -@LIBS_ONLY_FALSE@ launchd-launchd_runtime.$(OBJEXT) +@LIBS_ONLY_FALSE@ launchd-launchd_runtime.$(OBJEXT) \ +@LIBS_ONLY_FALSE@ launchd-launchd_runtime_kill.$(OBJEXT) launchd_OBJECTS = $(am_launchd_OBJECTS) launchd_LDADD = $(LDADD) launchd_LINK = $(CCLD) $(launchd_CFLAGS) $(CFLAGS) $(launchd_LDFLAGS) \ @@ -252,7 +253,7 @@ @LIBS_ONLY_FALSE@SystemStarter_SOURCES = StartupItems.c IPC.c SystemStarter.c @LIBS_ONLY_FALSE@launchd_CFLAGS = -mdynamic-no-pic $(AM_CFLAGS) -Wno-unused-parameter @LIBS_ONLY_FALSE@launchd_LDFLAGS = -lbsm -@LIBS_ONLY_FALSE@launchd_SOURCES = launchd.c launchd_core_logic.c launchd_unix_ipc.c protocol_vprocServer.c notifyServer.c launchd_internalUser.c launchd_internalServer.c job_replyUser.c launchd_runtime.c +@LIBS_ONLY_FALSE@launchd_SOURCES = launchd.c launchd_core_logic.c launchd_unix_ipc.c protocol_vprocServer.c notifyServer.c launchd_internalUser.c launchd_internalServer.c job_replyUser.c launchd_runtime.c launchd_runtime_kill.c @LIBS_ONLY_FALSE@launchproxy_LDFLAGS = -weak_framework Security @LIBS_ONLY_FALSE@man1_MANS = wait4path.1 launchctl.1 @LIBS_ONLY_FALSE@man5_MANS = launchd.plist.5 launchd.conf.5 @@ -420,6 +421,7 @@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/launchd-launchd_internalServer.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/launchd-launchd_internalUser.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/launchd-launchd_runtime.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/launchd-launchd_runtime_kill.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/launchd-launchd_unix_ipc.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/launchd-notifyServer.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/launchd-protocol_vprocServer.Po@am__quote@ @@ -771,6 +773,20 @@ @AMDEP_TRUE@@am__fastdepCC_FALSE@ source='launchd_runtime.c' object='launchd-launchd_runtime.obj' libtool=no @AMDEPBACKSLASH@ @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(launchd_CFLAGS) $(CFLAGS) -c -o launchd-launchd_runtime.obj `if test -f 'launchd_runtime.c'; then $(CYGPATH_W) 'launchd_runtime.c'; else $(CYGPATH_W) '$(srcdir)/launchd_runtime.c'; fi` + +launchd-launchd_runtime_kill.o: launchd_runtime_kill.c +@am__fastdepCC_TRUE@ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(launchd_CFLAGS) $(CFLAGS) -MT launchd-launchd_runtime_kill.o -MD -MP -MF $(DEPDIR)/launchd-launchd_runtime_kill.Tpo -c -o launchd-launchd_runtime_kill.o `test -f 'launchd_runtime_kill.c' || echo '$(srcdir)/'`launchd_runtime_kill.c +@am__fastdepCC_TRUE@ mv -f $(DEPDIR)/launchd-launchd_runtime_kill.Tpo $(DEPDIR)/launchd-launchd_runtime_kill.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='launchd_runtime_kill.c' object='launchd-launchd_runtime_kill.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(launchd_CFLAGS) $(CFLAGS) -c -o launchd-launchd_runtime_kill.o `test -f 'launchd_runtime_kill.c' || echo '$(srcdir)/'`launchd_runtime_kill.c + +launchd-launchd_runtime_kill.obj: launchd_runtime_kill.c +@am__fastdepCC_TRUE@ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(launchd_CFLAGS) $(CFLAGS) -MT launchd-launchd_runtime_kill.obj -MD -MP -MF $(DEPDIR)/launchd-launchd_runtime_kill.Tpo -c -o launchd-launchd_runtime_kill.obj `if test -f 'launchd_runtime_kill.c'; then $(CYGPATH_W) 'launchd_runtime_kill.c'; else $(CYGPATH_W) '$(srcdir)/launchd_runtime_kill.c'; fi` +@am__fastdepCC_TRUE@ mv -f $(DEPDIR)/launchd-launchd_runtime_kill.Tpo $(DEPDIR)/launchd-launchd_runtime_kill.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='launchd_runtime_kill.c' object='launchd-launchd_runtime_kill.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(launchd_CFLAGS) $(CFLAGS) -c -o launchd-launchd_runtime_kill.obj `if test -f 'launchd_runtime_kill.c'; then $(CYGPATH_W) 'launchd_runtime_kill.c'; else $(CYGPATH_W) '$(srcdir)/launchd_runtime_kill.c'; fi` install-man1: $(man1_MANS) $(man_MANS) @$(NORMAL_INSTALL) test -z "$(man1dir)" || $(MKDIR_P) "$(DESTDIR)$(man1dir)" Modified: trunk/launchd/src/launchd.c =================================================================== --- trunk/launchd/src/launchd.c 2007-09-18 19:59:38 UTC (rev 23407) +++ trunk/launchd/src/launchd.c 2007-09-19 01:00:28 UTC (rev 23408) @@ -277,7 +277,7 @@ sleep(3); - kill(-1, SIGKILL); + runtime_kill(-1, SIGKILL); } void Modified: trunk/launchd/src/launchd_core_logic.c =================================================================== --- trunk/launchd/src/launchd_core_logic.c 2007-09-18 19:59:38 UTC (rev 23407) +++ trunk/launchd/src/launchd_core_logic.c 2007-09-19 01:00:28 UTC (rev 23408) @@ -525,7 +525,7 @@ return; } - job_assumes(j, kill(j->p, SIGTERM) != -1); + job_assumes(j, runtime_kill(j->p, SIGTERM) != -1); j->sent_sigterm_time = mach_absolute_time(); if (j->exit_timeout) { @@ -1971,14 +1971,9 @@ /* * The job is dead. While the PID/PGID is still known to be * valid, try to kill abandoned descendant processes. - * - * We'd use job_assumes(), but POSIX defines consistency over - * correctness, and consequently kill/killpg now returns EPERM - * instead of ESRCH. As luck would have it, ESRCH is the only - * error we can ignore. */ if (!j->abandon_pg) { - killpg(j->p, SIGKILL); + job_assumes(j, runtime_killpg(j->p, SIGKILL) != -1 || errno == ESRCH); } /* @@ -2186,7 +2181,7 @@ return; } - job_assumes(j, kill(j->p, SIGKILL) != -1); + job_assumes(j, runtime_kill(j->p, SIGKILL) != -1); j->sent_sigkill = true; @@ -2690,7 +2685,7 @@ /* Temporarily disabled due to 5423935 and 4946119. */ #if 0 /* Ask the accountless process to exit. */ - job_assumes(j, kill(i_pid, SIGTERM) != -1); + job_assumes(j, runtime_kill(i_pid, SIGTERM) != -1); #endif } @@ -4114,7 +4109,7 @@ /* * The kernel team requested that launchd not do this for Leopard. - * jobmgr_assumes(jm, kill(p_i, SIGKILL) != -1); + * jobmgr_assumes(jm, runtime_kill(p_i, SIGKILL) != -1); */ } @@ -5026,7 +5021,7 @@ return 0; } } else if (otherj->p) { - job_assumes(j, kill(otherj->p, sig) != -1); + job_assumes(j, runtime_kill(otherj->p, sig) != -1); } return 0; Modified: trunk/launchd/src/launchd_runtime.h =================================================================== --- trunk/launchd/src/launchd_runtime.h 2007-09-18 19:59:38 UTC (rev 23407) +++ trunk/launchd/src/launchd_runtime.h 2007-09-19 01:00:28 UTC (rev 23408) @@ -26,6 +26,8 @@ #include <stdbool.h> #include <syslog.h> +#include "launchd_runtime_kill.h" + struct ldcred { uid_t euid; uid_t uid; Added: trunk/launchd/src/launchd_runtime_kill.c =================================================================== --- trunk/launchd/src/launchd_runtime_kill.c (rev 0) +++ trunk/launchd/src/launchd_runtime_kill.c 2007-09-19 01:00:28 UTC (rev 23408) @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2007 Apple Inc. All rights reserved. + * + * @APPLE_APACHE_LICENSE_HEADER_START@ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @APPLE_APACHE_LICENSE_HEADER_END@ + */ + +#define _NONSTD_SOURCE 1 +#include <signal.h> + +#include "launchd_runtime_kill.h" + +/* + * POSIX defines consistency over correctness, and consequently kill/killpg now + * returns EPERM instead of ESRCH. + * + * I've filed 5487498 to get a non-portable kill() variant, but for now, + * defining _NONSTD_SOURCE gets us the old behavior. + */ + +int +runtime_kill(pid_t pid, int sig) +{ + return kill(pid, sig); +} + +int +runtime_killpg(pid_t pgrp, int sig) +{ + return killpg(pgrp, sig); +} Added: trunk/launchd/src/launchd_runtime_kill.h =================================================================== --- trunk/launchd/src/launchd_runtime_kill.h (rev 0) +++ trunk/launchd/src/launchd_runtime_kill.h 2007-09-19 01:00:28 UTC (rev 23408) @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2007 Apple Inc. All rights reserved. + * + * @APPLE_APACHE_LICENSE_HEADER_START@ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @APPLE_APACHE_LICENSE_HEADER_END@ + */ +#ifndef __LAUNCHD_RUNTIME_KILL_H__ +#define __LAUNCHD_RUNTIME_KILL_H__ + +#include <sys/types.h> + +int runtime_kill(pid_t pid, int sig); +int runtime_killpg(pid_t pgrp, int sig); + +#endif