You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by ig...@apache.org on 2012/12/04 00:19:13 UTC

[2/4] git commit: Revert "cleanup: get rid of ink_killall implementation"

Revert "cleanup: get rid of ink_killall implementation"

This reverts commit 47aca7e93b8a7cf9c40dbc2c9d566c3d9ff47393.


Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/d60c4369
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/d60c4369
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/d60c4369

Branch: refs/heads/3.2.x
Commit: d60c4369b679f10e69e6913e5d9cfbb80daab47e
Parents: 98111a1
Author: Igor Galić <i....@brainsware.org>
Authored: Tue Dec 4 00:18:51 2012 +0100
Committer: Igor Galić <i....@brainsware.org>
Committed: Tue Dec 4 00:18:51 2012 +0100

----------------------------------------------------------------------
 cop/TrafficCop.cc     |    1 +
 lib/ts/Makefile.am    |    2 +
 lib/ts/ink_killall.cc |  149 ++++++++++++++++++++++++++++++++++++++++++++
 lib/ts/ink_killall.h  |   63 +++++++++++++++++++
 lib/ts/libts.h        |    1 +
 lib/ts/lockfile.cc    |    9 +++-
 6 files changed, 224 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/d60c4369/cop/TrafficCop.cc
----------------------------------------------------------------------
diff --git a/cop/TrafficCop.cc b/cop/TrafficCop.cc
index 60964fc..860825f 100644
--- a/cop/TrafficCop.cc
+++ b/cop/TrafficCop.cc
@@ -29,6 +29,7 @@
 
 #if defined(linux)
 #include "sys/utsname.h"
+#include "ink_killall.h"
 #include <sys/types.h>
 #include <sys/ipc.h>
 #include <sys/sem.h>

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/d60c4369/lib/ts/Makefile.am
----------------------------------------------------------------------
diff --git a/lib/ts/Makefile.am b/lib/ts/Makefile.am
index 107fc4c..4195d3d 100644
--- a/lib/ts/Makefile.am
+++ b/lib/ts/Makefile.am
@@ -71,6 +71,8 @@ libtsutil_la_SOURCES = \
   ink_inet.cc \
   ink_inet.h \
   ink_inout.h \
+  ink_killall.cc \
+  ink_killall.h \
   ink_llqueue.h \
   ink_lockfile.h \
   INK_MD5.h \

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/d60c4369/lib/ts/ink_killall.cc
----------------------------------------------------------------------
diff --git a/lib/ts/ink_killall.cc b/lib/ts/ink_killall.cc
new file mode 100644
index 0000000..81fab9c
--- /dev/null
+++ b/lib/ts/ink_killall.cc
@@ -0,0 +1,149 @@
+/** @file
+
+  A brief file description
+
+  @section license License
+
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you 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.
+ */
+
+/*-------------------------------------------------------------------------
+  -------------------------------------------------------------------------*/
+
+#include "libts.h"
+
+#if defined(linux)
+
+#include "ink_killall.h"
+#include "ink_resource.h"
+
+#define PROC_BASE "/proc"
+
+#define INITIAL_PIDVSIZE 32
+
+int
+ink_killall(const char *pname, int sig)
+{
+
+  int err;
+  pid_t *pidv;
+  int pidvcnt;
+
+  if (ink_killall_get_pidv_xmalloc(pname, &pidv, &pidvcnt) < 0) {
+    return -1;
+  }
+
+  if (pidvcnt == 0) {
+    ats_free(pidv);
+    return 0;
+  }
+
+  err = ink_killall_kill_pidv(pidv, pidvcnt, sig);
+  ats_free(pidv);
+
+  return err;
+
+}
+
+int
+ink_killall_get_pidv_xmalloc(const char *pname, pid_t ** pidv, int *pidvcnt)
+{
+
+  DIR *dir;
+  FILE *fp;
+  struct dirent *de;
+  pid_t pid, self;
+  char buf[LINE_MAX], *p, *comm;
+  int pidvsize = INITIAL_PIDVSIZE;
+
+  if (!pname || !pidv || !pidvcnt)
+    goto l_error;
+
+  self = getpid();
+  if (!(dir = opendir(PROC_BASE)))
+    goto l_error;
+
+  *pidvcnt = 0;
+  *pidv = (pid_t *)ats_malloc(pidvsize * sizeof(pid_t));
+
+  while ((de = readdir(dir))) {
+    if (!(pid = (pid_t) atoi(de->d_name)) || pid == self)
+      continue;
+    snprintf(buf, sizeof(buf), PROC_BASE "/%d/stat", pid);
+    if ((fp = fopen(buf, "r"))) {
+      if (fgets(buf, sizeof buf, fp) == 0)
+        goto l_close;
+      if ((p = strchr(buf, '('))) {
+        comm = p + 1;
+        if ((p = strchr(comm, ')')))
+          *p = '\0';
+        else
+          goto l_close;
+        if (strcmp(comm, pname) == 0) {
+          if (*pidvcnt >= pidvsize) {
+            pid_t *pidv_realloc;
+            pidvsize *= 2;
+            if (!(pidv_realloc = (pid_t *)ats_realloc(*pidv, pidvsize * sizeof(pid_t)))) {
+              ats_free(*pidv);
+              goto l_error;
+            } else {
+              *pidv = pidv_realloc;
+            }
+          }
+          (*pidv)[*pidvcnt] = pid;
+          (*pidvcnt)++;
+        }
+      }
+    l_close:
+      fclose(fp);
+    }
+  }
+  closedir(dir);
+
+  if (*pidvcnt == 0) {
+    ats_free(*pidv);
+    *pidv = 0;
+  }
+
+  return 0;
+
+l_error:
+  *pidv = NULL;
+  *pidvcnt = 0;
+  return -1;
+}
+
+int
+ink_killall_kill_pidv(pid_t * pidv, int pidvcnt, int sig)
+{
+
+  int err = 0;
+
+  if (!pidv || (pidvcnt <= 0))
+    return -1;
+
+  while (pidvcnt > 0) {
+    pidvcnt--;
+    if (kill(pidv[pidvcnt], sig) < 0)
+      err = -1;
+  }
+
+  return err;
+
+}
+
+#endif  // linux check

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/d60c4369/lib/ts/ink_killall.h
----------------------------------------------------------------------
diff --git a/lib/ts/ink_killall.h b/lib/ts/ink_killall.h
new file mode 100644
index 0000000..9b9f581
--- /dev/null
+++ b/lib/ts/ink_killall.h
@@ -0,0 +1,63 @@
+/** @file
+
+  A brief file description
+
+  @section license License
+
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you 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.
+ */
+
+/*-------------------------------------------------------------------------
+  -------------------------------------------------------------------------*/
+
+
+#ifndef _INK_KILLALL_H_
+#define _INK_KILLALL_H_
+
+#include "ink_config.h"
+
+#if defined(linux)
+
+/*-------------------------------------------------------------------------
+   ink_killall
+   - Sends signal 'sig' to all processes with the name 'pname'
+   - Returns: -1 error
+               0 okay
+  -------------------------------------------------------------------------*/
+int ink_killall(const char *pname, int sig);
+
+/*-------------------------------------------------------------------------
+   ink_killall_get_pidv_xmalloc
+   - Get all pid's named 'pname' and stores into ats_malloc'd
+     pid_t array, 'pidv'
+   - Returns: -1 error (pidv: set to NULL; pidvcnt: set to 0)
+               0 okay (pidv: ats_malloc'd pid vector; pidvcnt: number of pid's;
+	               if pidvcnt is set to 0, then pidv will be set to NULL)
+  -------------------------------------------------------------------------*/
+int ink_killall_get_pidv_xmalloc(const char *pname, pid_t ** pidv, int *pidvcnt);
+
+/*-------------------------------------------------------------------------
+   ink_killall_kill_pidv
+   - Kills all pid's in 'pidv' with signal 'sig'
+   - Returns: -1 error
+               0 okay
+  -------------------------------------------------------------------------*/
+int ink_killall_kill_pidv(pid_t * pidv, int pidvcnt, int sig);
+
+#endif
+
+#endif

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/d60c4369/lib/ts/libts.h
----------------------------------------------------------------------
diff --git a/lib/ts/libts.h b/lib/ts/libts.h
index 1006bc9..833cdf9 100644
--- a/lib/ts/libts.h
+++ b/lib/ts/libts.h
@@ -60,6 +60,7 @@
 #include "ink_hash_table.h"
 #include "ink_hrtime.h"
 #include "ink_inout.h"
+#include "ink_killall.h"
 #include "ink_llqueue.h"
 #include "ink_lockfile.h"
 #include "ink_memory.h"

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/d60c4369/lib/ts/lockfile.cc
----------------------------------------------------------------------
diff --git a/lib/ts/lockfile.cc b/lib/ts/lockfile.cc
index e3da24d..c2d34c4 100644
--- a/lib/ts/lockfile.cc
+++ b/lib/ts/lockfile.cc
@@ -24,6 +24,10 @@
 #include "ink_platform.h"
 #include "ink_lockfile.h"
 
+#if defined(linux)
+#include "ink_killall.h"
+#endif
+
 #define LOCKFILE_BUF_LEN 16     // 16 bytes should be enought for a pid
 
 int
@@ -191,7 +195,10 @@ Lockfile::Close(void)
 // killed): Unfortunately, it's possible on Linux that the main PID of
 // the process has been successfully killed (and is waiting to be
 // reaped while in a defunct state), while some of the other threads
-// of the process just don't want to go away.
+// of the process just don't want to go away.  Integrate ink_killall
+// into Kill() and KillAll() just to make sure we really kill
+// everything and so that we don't spin hard while trying to kill a
+// defunct process.
 //-------------------------------------------------------------------------
 
 static void