Revision c7e4b037

b/lib/uidpool.py
182 182
def _IsUidUsed(uid):
183 183
  """Check if there is any process in the system running with the given user-id
184 184

  
185
  @type uid: integer
186
  @param uid: the user-id to be checked.
187

  
185 188
  """
186 189
  pgrep_command = [constants.PGREP, "-u", uid]
187 190
  result = utils.RunCmd(pgrep_command)
......
218 221
  def GetUid(self):
219 222
    return self._uid
220 223

  
221
  def __str__(self):
224
  def AsStr(self):
222 225
    return "%s" % self._uid
223 226

  
224 227

  
......
255 258
      <stop the process>
256 259
      uidpool.ReleaseUid(uid)
257 260

  
261
  @type all_uids: set of integers
258 262
  @param all_uids: a set containing all the user-ids in the user-id pool
259 263
  @return: a LockedUid object representing the unused uid. It's the caller's
260 264
           responsibility to unlock the uid once an instance is started with
......
269 273

  
270 274
  # Get list of currently used uids from the filesystem
271 275
  try:
272
    taken_uids = set(os.listdir(constants.UIDPOOL_LOCKDIR))
273
    # Filter out spurious entries from the directory listing
274
    taken_uids = all_uids.intersection(taken_uids)
276
    taken_uids = set()
277
    for taken_uid in os.listdir(constants.UIDPOOL_LOCKDIR):
278
      try:
279
        taken_uid = int(taken_uid)
280
      except ValueError, err:
281
        # Skip directory entries that can't be converted into an integer
282
        continue
283
      taken_uids.add(taken_uid)
275 284
  except OSError, err:
276 285
    raise errors.LockError("Failed to get list of used user-ids: %s" % err)
277 286

  
287
  # Filter out spurious entries from the directory listing
288
  taken_uids = all_uids.intersection(taken_uids)
289

  
278 290
  # Remove the list of used uids from the list of all uids
279 291
  unused_uids = list(all_uids - taken_uids)
280 292
  if not unused_uids:
......
330 342
  if isinstance(uid, LockedUid):
331 343
    # Make sure we release the exclusive lock, if there is any
332 344
    uid.Unlock()
345
    uid_filename = uid.AsStr()
346
  else:
347
    uid_filename = str(uid)
348

  
333 349
  try:
334
    uid_path = utils.PathJoin(constants.UIDPOOL_LOCKDIR, str(uid))
350
    uid_path = utils.PathJoin(constants.UIDPOOL_LOCKDIR, uid_filename)
335 351
    os.remove(uid_path)
336 352
  except OSError, err:
337 353
    raise errors.LockError("Failed to remove user-id lockfile"
338
                           " for user-id %s: %s" % (uid, err))
354
                           " for user-id %s: %s" % (uid_filename, err))
339 355

  
340 356

  
341 357
def ExecWithUnusedUid(fn, all_uids, *args, **kwargs):
......
344 360
  This wrapper function provides a simple way to handle the requesting,
345 361
  unlocking and releasing a user-id.
346 362
  "fn" is called by passing a "uid" keyword argument that
347
  contains an unused user-id (as a string) selected from the set of user-ids
363
  contains an unused user-id (as an integer) selected from the set of user-ids
348 364
  passed in all_uids.
349 365
  If there is an error while executing "fn", the user-id is returned
350 366
  to the pool.
351 367

  
352
  @param fn: a callable
368
  @param fn: a callable that accepts a keyword argument called "uid"
369
  @type all_uids: a set of integers
353 370
  @param all_uids: a set containing all user-ids in the user-id pool
354 371

  
355 372
  """
356 373
  uid = RequestUnusedUid(all_uids)
357
  kwargs["uid"] = str(uid)
374
  kwargs["uid"] = uid.GetUid()
358 375
  try:
359 376
    return_value = fn(*args, **kwargs)
360 377
  except:

Also available in: Unified diff