Revision 5833b7e6 lib/uidpool.py

b/lib/uidpool.py
328 328
  except OSError, err:
329 329
    raise errors.LockError("Failed to remove user-id lockfile"
330 330
                           " for user-id %s: %s" % (uid, err))
331

  
332

  
333
def ExecWithUnusedUid(fn, all_uids, *args, **kwargs):
334
  """Execute a callable and provide an unused user-id in its kwargs.
335

  
336
  This wrapper function provides a simple way to handle the requesting,
337
  unlocking and releasing a user-id.
338
  "fn" is called by passing a "uid" keyword argument that
339
  contains an unused user-id (as a string) selected from the set of user-ids
340
  passed in all_uids.
341
  If there is an error while executing "fn", the user-id is returned
342
  to the pool.
343

  
344
  @param fn: a callable
345
  @param all_uids: a set containing all user-ids in the user-id pool
346

  
347
  """
348
  uid = RequestUnusedUid(all_uids)
349
  kwargs["uid"] = str(uid)
350
  try:
351
    return_value = fn(*args, **kwargs)
352
  except:
353
    # The failure of "callabe" means that starting a process with the uid
354
    # failed, so let's put the uid back into the pool.
355
    ReleaseUid(uid)
356
    raise
357
  uid.Unlock()
358
  return return_value

Also available in: Unified diff