#include <sys/types.h>
#include <sys/device.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/queue.h>
#include <sys/un.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <regex.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <pthread.h>
#include <libprop/proplib.h>
#include <sys/udev.h>
#include "udevd.h"
static int64_t udev_generation;
TAILQ_HEAD(pdev_array_list_head, pdev_array_entry) pdev_array_list;
void
pdev_array_entry_ref(struct pdev_array_entry *pae)
{
++pae->refs;
}
void
pdev_array_entry_unref(struct pdev_array_entry *pae)
{
if (--pae->refs == 0) {
TAILQ_REMOVE(&pdev_array_list, pae, link);
prop_object_release(pae->pdev_array);
free(pae);
}
}
void
pdev_array_entry_insert(prop_array_t pa)
{
struct pdev_array_entry *pae, *opae = NULL;
if (pa == NULL)
errx(1, "null prop_array in insert_pdev_array");
pae = malloc(sizeof(struct pdev_array_entry));
if (pae == NULL)
errx(1, "insert_pdev_array could not allocate mem");
memset(pae, 0, sizeof(struct pdev_array_entry));
pae->pdev_array = pa;
pae->generation = udev_generation++;
pae->refs = 1;
if (!TAILQ_EMPTY(&pdev_array_list))
opae = TAILQ_LAST(&pdev_array_list, pdev_array_list_head);
TAILQ_INSERT_TAIL(&pdev_array_list, pae, link);
if (opae != NULL)
pdev_array_entry_unref(opae);
}
void
pdev_array_clean(void)
{
struct pdev_array_entry *pae;
while ((pae = TAILQ_LAST(&pdev_array_list, pdev_array_list_head)) != NULL) {
TAILQ_REMOVE(&pdev_array_list, pae, link);
prop_object_release(pae->pdev_array);
free(pae);
}
}
struct pdev_array_entry *
pdev_array_entry_get(int64_t generation)
{
struct pdev_array_entry *pae;
int found = 0;
TAILQ_FOREACH(pae, &pdev_array_list, link) {
if (pae->generation == generation) {
found = 1;
break;
}
}
if (!found)
return NULL;
pdev_array_entry_ref(pae);
return pae;
}
struct pdev_array_entry *
pdev_array_entry_get_last(void)
{
struct pdev_array_entry *pae;
pae = TAILQ_LAST(&pdev_array_list, pdev_array_list_head);
pdev_array_entry_ref(pae);
return pae;
}