~ubuntu-branches/ubuntu/maverick/dbus/maverick-security

« back to all changes in this revision

Viewing changes to dbus/dbus-resources.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-27 13:06:32 UTC
  • mfrom: (1.1.23 upstream)
  • Revision ID: james.westby@ubuntu.com-20100927130632-bqs145trvchd2lmf
Tags: 1.4.0-0ubuntu1
* New upstream release
 - Fixes https://bugs.freedesktop.org/show_bug.cgi?id=17754 Race condition in protected_change_timeout
 - Requested by various upstream KDE developers http://lists.kde.org/?t=128514970000004&r=1&w=2

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21
21
 *
22
22
 */
 
23
 
 
24
#include <config.h>
23
25
#include <dbus/dbus-resources.h>
24
26
#include <dbus/dbus-internals.h>
25
27
 
29
31
 * @brief DBusCounter and other stuff related to resource limits
30
32
 *
31
33
 * Types and functions related to tracking resource limits,
32
 
 * such as the maximum amount of memory a connection can use
 
34
 * such as the maximum amount of memory/unix fds a connection can use
33
35
 * for messages, etc.
34
36
 */
35
37
 
53
55
{
54
56
  int refcount;  /**< reference count */
55
57
 
56
 
  long value;    /**< current counter value */
57
 
 
58
 
  long notify_guard_value; /**< call notify function when crossing this value */
 
58
  long size_value;       /**< current size counter value */
 
59
  long unix_fd_value;    /**< current unix fd counter value */
 
60
 
 
61
  long notify_size_guard_value;    /**< call notify function when crossing this size value */
 
62
  long notify_unix_fd_guard_value; /**< call notify function when crossing this unix fd value */
 
63
 
59
64
  DBusCounterNotifyFunction notify_function; /**< notify function */
60
65
  void *notify_data; /**< data for notify function */
61
66
};
83
88
    return NULL;
84
89
  
85
90
  counter->refcount = 1;
86
 
  counter->value = 0;
 
91
  counter->size_value = 0;
 
92
  counter->unix_fd_value = 0;
87
93
 
88
 
  counter->notify_guard_value = 0;
 
94
  counter->notify_size_guard_value = 0;
 
95
  counter->notify_unix_fd_guard_value = 0;
89
96
  counter->notify_function = NULL;
90
97
  counter->notify_data = NULL;
91
98
  
129
136
}
130
137
 
131
138
/**
132
 
 * Adjusts the value of the counter by the given
133
 
 * delta which may be positive or negative.
134
 
 * Calls the notify function from _dbus_counter_set_notify()
135
 
 * if that function has been specified.
136
 
 *
137
 
 * @param counter the counter
138
 
 * @param delta value to add to the counter's current value
139
 
 */
140
 
void
141
 
_dbus_counter_adjust (DBusCounter *counter,
142
 
                      long         delta)
143
 
{
144
 
  long old = counter->value;
145
 
  
146
 
  counter->value += delta;
147
 
 
148
 
#if 0
149
 
  _dbus_verbose ("Adjusting counter %ld by %ld = %ld\n",
150
 
                 old, delta, counter->value);
151
 
#endif
152
 
  
153
 
  if (counter->notify_function != NULL &&
154
 
      ((old < counter->notify_guard_value &&
155
 
        counter->value >= counter->notify_guard_value) ||
156
 
       (old >= counter->notify_guard_value &&
157
 
        counter->value < counter->notify_guard_value)))
158
 
    (* counter->notify_function) (counter, counter->notify_data);
159
 
}
160
 
 
161
 
/**
162
 
 * Gets the current value of the counter.
163
 
 *
164
 
 * @param counter the counter
165
 
 * @returns its current value
166
 
 */
167
 
long
168
 
_dbus_counter_get_value (DBusCounter *counter)
169
 
{
170
 
  return counter->value;
 
139
 * Adjusts the value of the size counter by the given
 
140
 * delta which may be positive or negative.
 
141
 * Calls the notify function from _dbus_counter_set_notify()
 
142
 * if that function has been specified.
 
143
 *
 
144
 * @param counter the counter
 
145
 * @param delta value to add to the size counter's current value
 
146
 */
 
147
void
 
148
_dbus_counter_adjust_size (DBusCounter *counter,
 
149
                           long         delta)
 
150
{
 
151
  long old = counter->size_value;
 
152
 
 
153
  counter->size_value += delta;
 
154
 
 
155
#if 0
 
156
  _dbus_verbose ("Adjusting counter %ld by %ld = %ld\n",
 
157
                 old, delta, counter->size_value);
 
158
#endif
 
159
 
 
160
  if (counter->notify_function != NULL &&
 
161
      ((old < counter->notify_size_guard_value &&
 
162
        counter->size_value >= counter->notify_size_guard_value) ||
 
163
       (old >= counter->notify_size_guard_value &&
 
164
        counter->size_value < counter->notify_size_guard_value)))
 
165
    (* counter->notify_function) (counter, counter->notify_data);
 
166
}
 
167
 
 
168
/**
 
169
 * Adjusts the value of the unix fd counter by the given
 
170
 * delta which may be positive or negative.
 
171
 * Calls the notify function from _dbus_counter_set_notify()
 
172
 * if that function has been specified.
 
173
 *
 
174
 * @param counter the counter
 
175
 * @param delta value to add to the unix fds counter's current value
 
176
 */
 
177
void
 
178
_dbus_counter_adjust_unix_fd (DBusCounter *counter,
 
179
                              long         delta)
 
180
{
 
181
  long old = counter->unix_fd_value;
 
182
  
 
183
  counter->unix_fd_value += delta;
 
184
 
 
185
#if 0
 
186
  _dbus_verbose ("Adjusting counter %ld by %ld = %ld\n",
 
187
                 old, delta, counter->unix_fd_value);
 
188
#endif
 
189
  
 
190
  if (counter->notify_function != NULL &&
 
191
      ((old < counter->notify_unix_fd_guard_value &&
 
192
        counter->unix_fd_value >= counter->notify_unix_fd_guard_value) ||
 
193
       (old >= counter->notify_unix_fd_guard_value &&
 
194
        counter->unix_fd_value < counter->notify_unix_fd_guard_value)))
 
195
    (* counter->notify_function) (counter, counter->notify_data);
 
196
}
 
197
 
 
198
/**
 
199
 * Gets the current value of the size counter.
 
200
 *
 
201
 * @param counter the counter
 
202
 * @returns its current size value
 
203
 */
 
204
long
 
205
_dbus_counter_get_size_value (DBusCounter *counter)
 
206
{
 
207
  return counter->size_value;
 
208
}
 
209
 
 
210
/**
 
211
 * Gets the current value of the unix fd counter.
 
212
 *
 
213
 * @param counter the counter
 
214
 * @returns its current unix fd value
 
215
 */
 
216
long
 
217
_dbus_counter_get_unix_fd_value (DBusCounter *counter)
 
218
{
 
219
  return counter->unix_fd_value;
171
220
}
172
221
 
173
222
/**
174
223
 * Sets the notify function for this counter; the notify function is
175
 
 * called whenever the counter's value crosses the guard value in
 
224
 * called whenever the counter's values cross the guard values in
176
225
 * either direction (moving up, or moving down).
177
226
 *
178
227
 * @param counter the counter
179
 
 * @param guard_value the value we're notified if the counter crosses
 
228
 * @param size_guard_value the value we're notified if the size counter crosses
 
229
 * @param unix_fd_guard_value the value we're notified if the unix fd counter crosses
180
230
 * @param function function to call in order to notify
181
231
 * @param user_data data to pass to the function
182
232
 */
183
233
void
184
234
_dbus_counter_set_notify (DBusCounter               *counter,
185
 
                          long                       guard_value,
 
235
                          long                       size_guard_value,
 
236
                          long                       unix_fd_guard_value,
186
237
                          DBusCounterNotifyFunction  function,
187
238
                          void                      *user_data)
188
239
{
189
 
  counter->notify_guard_value = guard_value;
 
240
  counter->notify_size_guard_value = size_guard_value;
 
241
  counter->notify_unix_fd_guard_value = unix_fd_guard_value;
190
242
  counter->notify_function = function;
191
243
  counter->notify_data = user_data;
192
244
}