File: ipmiconsole_garbage_collector.c

package info (click to toggle)
freeipmi 1.4.11-1.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 21,952 kB
  • ctags: 18,990
  • sloc: ansic: 299,490; sh: 11,087; makefile: 1,958; perl: 1,078
file content (197 lines) | stat: -rw-r--r-- 6,772 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*****************************************************************************\
 *  $Id: ipmiconsole_garbage_collector.c,v 1.22 2010-02-08 22:02:30 chu11 Exp $
 *****************************************************************************
 *  Copyright (C) 2007-2014 Lawrence Livermore National Security, LLC.
 *  Copyright (C) 2006-2007 The Regents of the University of California.
 *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
 *  Written by Albert Chu <chu11@llnl.gov>
 *  UCRL-CODE-221226
 *
 *  This file is part of Ipmiconsole, a set of IPMI 2.0 SOL libraries
 *  and utilities.  For details, see http://www.llnl.gov/linux/.
 *
 *  Ipmiconsole is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 3 of the License, or (at your
 *  option) any later version.
 *
 *  Ipmiconsole is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with Ipmiconsole.  If not, see <http://www.gnu.org/licenses/>.
\*****************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdio.h>
#include <stdlib.h>
#if STDC_HEADERS
#include <string.h>
#endif /* STDC_HEADERS */
#if HAVE_PTHREAD_H
#include <pthread.h>
#endif /* HAVE_PTHREAD_H */
#if TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#else  /* !TIME_WITH_SYS_TIME */
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#else /* !HAVE_SYS_TIME_H */
#include <time.h>
#endif  /* !HAVE_SYS_TIME_H */
#endif /* !TIME_WITH_SYS_TIME */
#include <assert.h>
#include <errno.h>

#include "ipmiconsole.h"
#include "ipmiconsole_defs.h"

#include "ipmiconsole_ctx.h"
#include "ipmiconsole_debug.h"
#include "ipmiconsole_engine.h"

#include "freeipmi-portability.h"
#include "list.h"

extern List console_engine_ctxs_to_destroy;
extern pthread_mutex_t console_engine_ctxs_to_destroy_mutex;
extern int garbage_collector_notifier[2];

/*
 * When the engine is in teardown, there is a tiny race condition that
 * is possible for the garbage collector to still be running while the
 * engine is being torn down.  This set of variables is to protect
 * against that situation.
 */

int garbage_collector_active = 0;
pthread_mutex_t garbage_collector_active_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t garbage_collector_active_cond = PTHREAD_COND_INITIALIZER;

#define IPMICONSOLE_GARBAGE_COLLECTOR_SPIN_TIME 30

void *
ipmiconsole_garbage_collector (void *arg)
{
  int perr;

  assert (!garbage_collector_active);

  /* What do we do if a mutex lock/unlock fails here?  Ignore for
   * now.
   */
  
  if ((perr = pthread_mutex_lock (&garbage_collector_active_mutex)))
    IPMICONSOLE_DEBUG (("pthread_mutex_lock: %s", strerror (perr)));
  
  garbage_collector_active++;
  
  if ((perr = pthread_cond_signal(&garbage_collector_active_cond)))
    IPMICONSOLE_DEBUG (("pthread_cond_signal: %s", strerror (perr)));

  if ((perr = pthread_mutex_unlock (&garbage_collector_active_mutex)) != 0)
    IPMICONSOLE_DEBUG (("pthread_mutex_unlock: %s", strerror (perr)));
 
  while (1)
    {
      ListIterator itr = NULL;
      ipmiconsole_ctx_t c;
      struct timeval tv;
      int perr;
      int n;
      fd_set rds;

      FD_ZERO (&rds);
      FD_SET (garbage_collector_notifier[0], &rds);

      tv.tv_sec = IPMICONSOLE_GARBAGE_COLLECTOR_SPIN_TIME;
      tv.tv_usec = 0;

      /* What happens if anythings fails?
       *
       * Quit - well that's bad.
       *
       * Mem-leak - that's bad too.
       *
       * For now, just log.
       */

      if ((n = select (garbage_collector_notifier[0] + 1, &rds, NULL, NULL, &tv)) < 0)
        {
          IPMICONSOLE_DEBUG (("select: %s", strerror (errno)));
          continue;
        }

      /* Signal from ipmiconsole_engine_teardown() to quit */
      if (n && FD_ISSET (garbage_collector_notifier[0], &rds))
        break;

      /* Note: the code in __ipmiconsole_ctx_connection_cleanup() and
       * ipmiconsole_garbage_collector() may look like it may race and
       * could deadlock.  (ABBA and BAAB deadlock situation).  However,
       * the context mutex c->signal.destroyed_mutex is accessed in
       * __ipmiconsole_ctx_connection_cleanup() when trying to add this item
       * to the console_engine_ctxs_to_destroy list.  It is accessed in
       * ipmiconsole_garbage_collector() only on the items already in the
       * console_engine_ctxs_to_destroy list.  So the
       * c->signal.destroyed_mutex can never be raced against in these two
       * functions.
       */

      if ((perr = pthread_mutex_lock (&(console_engine_ctxs_to_destroy_mutex))) != 0)
        IPMICONSOLE_DEBUG (("pthread_mutex_lock: %s", strerror (perr)));

      if (!(itr = list_iterator_create (console_engine_ctxs_to_destroy)))
        {
          IPMICONSOLE_DEBUG (("list_iterator_create: %s", strerror (errno)));
          goto end_loop;
        }

      while ((c = (ipmiconsole_ctx_t)list_next (itr)))
        {
          if ((perr = pthread_mutex_lock (&(c->signal.destroyed_mutex))) != 0)
            IPMICONSOLE_DEBUG (("pthread_mutex_lock: %s", strerror (perr)));

          /* Be careful, if the user requested to destroy the context, we can
           * destroy it here.  But if we destroy it, there is no mutex to
           * unlock.
           */
          if (c->signal.user_has_destroyed)
            {
              if (!list_delete (itr))
                IPMICONSOLE_DEBUG (("list_delete: %s", strerror (errno)));
            }
          else
            {
              if ((perr = pthread_mutex_unlock (&(c->signal.destroyed_mutex))) != 0)
                IPMICONSOLE_DEBUG (("pthread_mutex_unlock: %s", strerror (perr)));
            }
        }

      if ((perr = pthread_mutex_unlock (&(console_engine_ctxs_to_destroy_mutex))) != 0)
        IPMICONSOLE_DEBUG (("pthread_mutex_unlock: %s", strerror (perr)));

    end_loop:
      if (itr)
        list_iterator_destroy (itr);
    }

  if ((perr = pthread_mutex_lock (&garbage_collector_active_mutex)))
    IPMICONSOLE_DEBUG (("pthread_mutex_lock: %s", strerror (perr)));

  garbage_collector_active = 0;

  if ((perr = pthread_cond_signal(&garbage_collector_active_cond)))
    IPMICONSOLE_DEBUG (("pthread_cond_signal: %s", strerror (perr)));

  if ((perr = pthread_mutex_unlock (&garbage_collector_active_mutex)) != 0)
    IPMICONSOLE_DEBUG (("pthread_mutex_unlock: %s", strerror (perr)));

  return (NULL);
}