~ps10gel/ubuntu/xenial/trafficserver/6.2.0

« back to all changes in this revision

Viewing changes to lib/ts/ink_killall.cc

  • Committer: Bazaar Package Importer
  • Author(s): Arno Toell
  • Date: 2011-01-13 11:49:18 UTC
  • Revision ID: james.westby@ubuntu.com-20110113114918-vu422h8dknrgkj15
Tags: upstream-2.1.5-unstable
ImportĀ upstreamĀ versionĀ 2.1.5-unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** @file
 
2
 
 
3
  A brief file description
 
4
 
 
5
  @section license License
 
6
 
 
7
  Licensed to the Apache Software Foundation (ASF) under one
 
8
  or more contributor license agreements.  See the NOTICE file
 
9
  distributed with this work for additional information
 
10
  regarding copyright ownership.  The ASF licenses this file
 
11
  to you under the Apache License, Version 2.0 (the
 
12
  "License"); you may not use this file except in compliance
 
13
  with the License.  You may obtain a copy of the License at
 
14
 
 
15
      http://www.apache.org/licenses/LICENSE-2.0
 
16
 
 
17
  Unless required by applicable law or agreed to in writing, software
 
18
  distributed under the License is distributed on an "AS IS" BASIS,
 
19
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
20
  See the License for the specific language governing permissions and
 
21
  limitations under the License.
 
22
 */
 
23
 
 
24
/*-------------------------------------------------------------------------
 
25
  -------------------------------------------------------------------------*/
 
26
 
 
27
#include "libts.h"
 
28
 
 
29
#if defined(linux)
 
30
 
 
31
#include "ink_killall.h"
 
32
#include "ink_resource.h"
 
33
 
 
34
#define PROC_BASE "/proc"
 
35
 
 
36
#define INITIAL_PIDVSIZE 32
 
37
 
 
38
int
 
39
ink_killall(const char *pname, int sig)
 
40
{
 
41
 
 
42
  int err;
 
43
  pid_t *pidv;
 
44
  int pidvcnt;
 
45
 
 
46
  if (ink_killall_get_pidv_xmalloc(pname, &pidv, &pidvcnt) < 0) {
 
47
    return -1;
 
48
  }
 
49
 
 
50
  if (pidvcnt == 0) {
 
51
    xfree(pidv);
 
52
    return 0;
 
53
  }
 
54
 
 
55
  err = ink_killall_kill_pidv(pidv, pidvcnt, sig);
 
56
 
 
57
  xfree(pidv);
 
58
 
 
59
  return err;
 
60
 
 
61
}
 
62
 
 
63
int
 
64
ink_killall_get_pidv_xmalloc(const char *pname, pid_t ** pidv, int *pidvcnt)
 
65
{
 
66
 
 
67
  DIR *dir;
 
68
  FILE *fp;
 
69
  struct dirent *de;
 
70
  pid_t pid, self;
 
71
  char buf[LINE_MAX], *p, *comm;
 
72
  int pidvsize = INITIAL_PIDVSIZE;
 
73
 
 
74
  if (!pname || !pidv || !pidvcnt)
 
75
    goto l_error;
 
76
 
 
77
  self = getpid();
 
78
  if (!(dir = opendir(PROC_BASE)))
 
79
    goto l_error;
 
80
 
 
81
  *pidvcnt = 0;
 
82
  if (!(*pidv = (pid_t *) xmalloc(pidvsize * sizeof(pid_t))))
 
83
    goto l_error;
 
84
 
 
85
  while ((de = readdir(dir))) {
 
86
    if (!(pid = (pid_t) atoi(de->d_name)) || pid == self)
 
87
      continue;
 
88
    snprintf(buf, sizeof(buf), PROC_BASE "/%d/stat", pid);
 
89
    if ((fp = fopen(buf, "r"))) {
 
90
      if (fgets(buf, sizeof buf, fp) == 0)
 
91
        goto l_close;
 
92
      if ((p = strchr(buf, '('))) {
 
93
        comm = p + 1;
 
94
        if ((p = strchr(comm, ')')))
 
95
          *p = '\0';
 
96
        else
 
97
          goto l_close;
 
98
        if (strcmp(comm, pname) == 0) {
 
99
          if (*pidvcnt >= pidvsize) {
 
100
            pid_t *pidv_realloc;
 
101
            pidvsize *= 2;
 
102
            if (!(pidv_realloc = (pid_t *) xrealloc(*pidv, pidvsize * sizeof(pid_t)))) {
 
103
              xfree(*pidv);
 
104
              goto l_error;
 
105
            } else {
 
106
              *pidv = pidv_realloc;
 
107
            }
 
108
          }
 
109
          (*pidv)[*pidvcnt] = pid;
 
110
          (*pidvcnt)++;
 
111
        }
 
112
      }
 
113
    l_close:
 
114
      fclose(fp);
 
115
    }
 
116
  }
 
117
  closedir(dir);
 
118
 
 
119
  if (*pidvcnt == 0) {
 
120
    xfree(*pidv);
 
121
    *pidv = 0;
 
122
  }
 
123
 
 
124
  return 0;
 
125
 
 
126
l_error:
 
127
  *pidv = NULL;
 
128
  *pidvcnt = 0;
 
129
  return -1;
 
130
}
 
131
 
 
132
int
 
133
ink_killall_kill_pidv(pid_t * pidv, int pidvcnt, int sig)
 
134
{
 
135
 
 
136
  int err = 0;
 
137
 
 
138
  if (!pidv || (pidvcnt <= 0))
 
139
    return -1;
 
140
 
 
141
  while (pidvcnt > 0) {
 
142
    pidvcnt--;
 
143
    if (kill(pidv[pidvcnt], sig) < 0)
 
144
      err = -1;
 
145
  }
 
146
 
 
147
  return err;
 
148
 
 
149
}
 
150
 
 
151
#endif  // linux check