Add dixCreatePrivateKey API

Keys need to persist through server reset so that the private system
can be cleaned up in dixResetPrivates. In particular, this means that
keys cannot live in objects freed at reset time. This API provides
suitable object lifetime by having the privates code free the key in
the reset path.

Signed-off-by: Keith Packard <keithp@keithp.com>
Reviewed-by: Jamey Sharp <jamey@minilop.net>
This commit is contained in:
Keith Packard 2010-04-30 19:36:33 -07:00
parent 495fc3eb2d
commit 34db537907
2 changed files with 42 additions and 2 deletions

View File

@ -229,12 +229,37 @@ dixRegisterPrivateKey(DevPrivateKey key, DevPrivateType type, unsigned size)
key->size = size; key->size = size;
key->initialized = TRUE; key->initialized = TRUE;
key->type = type; key->type = type;
key->allocated = FALSE;
key->next = keys[type].key; key->next = keys[type].key;
keys[type].key = key; keys[type].key = key;
return TRUE; return TRUE;
} }
/*
* Allocate a new private key.
*
* This manages the storage of the key object itself, freeing it when the
* privates system is restarted at server reset time. All other keys
* are expected to be statically allocated as the privates must be
* reset after all objects have been freed
*/
DevPrivateKey
dixCreatePrivateKey(DevPrivateType type, unsigned size)
{
DevPrivateKey key;
key = calloc(sizeof (DevPrivateKeyRec), 1);
if (!key)
return NULL;
if (!dixRegisterPrivateKey(key, type, size)) {
free(key);
return NULL;
}
key->allocated = TRUE;
return key;
}
/* /*
* Initialize privates by zeroing them * Initialize privates by zeroing them
*/ */
@ -444,13 +469,16 @@ dixResetPrivates(void)
DevPrivateType t; DevPrivateType t;
for (t = PRIVATE_XSELINUX; t < PRIVATE_LAST; t++) { for (t = PRIVATE_XSELINUX; t < PRIVATE_LAST; t++) {
DevPrivateKey key; DevPrivateKey key, next;
for (key = keys[t].key; key; key = key->next) { for (key = keys[t].key; key; key = next) {
next = key->next;
key->offset = 0; key->offset = 0;
key->initialized = FALSE; key->initialized = FALSE;
key->size = 0; key->size = 0;
key->type = 0; key->type = 0;
if (key->allocated)
free(key);
} }
if (keys[t].created) { if (keys[t].created) {
ErrorF("%d %ss still allocated at reset\n", ErrorF("%d %ss still allocated at reset\n",

View File

@ -60,6 +60,7 @@ typedef struct _DevPrivateKeyRec {
int offset; int offset;
int size; int size;
Bool initialized; Bool initialized;
Bool allocated;
DevPrivateType type; DevPrivateType type;
struct _DevPrivateKeyRec *next; struct _DevPrivateKeyRec *next;
} DevPrivateKeyRec, *DevPrivateKey; } DevPrivateKeyRec, *DevPrivateKey;
@ -98,6 +99,17 @@ dixPrivateKeyRegistered(DevPrivateKey key)
return key->initialized; return key->initialized;
} }
/*
* Allocate a new private key.
*
* This manages the storage of the key object itself, freeing it when the
* privates system is restarted at server reset time. All other keys
* are expected to be statically allocated as the privates must be
* reset after all objects have been freed
*/
extern _X_EXPORT DevPrivateKey
dixCreatePrivateKey(DevPrivateType type, unsigned size);
/* /*
* Get the address of the private storage. * Get the address of the private storage.
* *