Lumiera  0.pre.03
»edit your freedom«
luid.c
Go to the documentation of this file.
1 /*
2  LUID - Lumiera unique identifiers
3 
4  Copyright (C)
5  2008, Christian Thaeter <ct@pipapo.org>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
14 
20 #include "lib/luid.h"
21 
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <time.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <stdint.h>
29 
30 
31 
32 void
33 lumiera_uid_set_ptr (lumiera_uid* luid, void* ptr)
34 {
35  memset (luid, 0, 16);
36  *(void**)luid = ptr;
37 }
38 
39 
40 void*
42 {
43  return *(void**)luid;
44 }
45 
46 
47 void
49 {
50  static int fd = -2;
51  if (!luid)
52  return;
53 
54  if (fd == -2)
55  {
56  fd = open ("/dev/urandom", O_RDONLY);
57  /* on linux /dev/random would be way to slow for our purpose, so we comment that out for now.
58  other unixiods offer a /dev/random which has the same semantics as linux /dev/urandom has,
59  configuration should do this right some day.
60  if (fd == -1)
61  fd = open ("/dev/random", O_RDONLY);
62  */
63  if (fd >= 0)
64  fcntl (fd, F_SETFD, FD_CLOEXEC);
65  else
66  srand (getpid () + time (NULL));
67  }
68 
69  do
70  {
71  if (fd < 0)
72  {
73  for (int i = 0; i < 16; ++i)
74  ((unsigned char*)luid)[i] = (unsigned char)(rand()>>7);
75  }
76  else
77  {
78  if (read (fd, luid, 16) < 16)
79  abort ();
80  }
81  }
82  /* we identify generic pointers by having some zeros in the LUID,
83  * Accidentally, but very unlikely this might happen within a random LUID;
84  * just regenerate in this case */
85  while (!*(((intptr_t*)luid)+1));
86 }
87 
88 
89 void
91 {
92  memcpy (dest, src, 16);
93 }
94 
95 
96 int
97 lumiera_uid_eq (const lumiera_uid* luida, const lumiera_uid* luidb)
98 {
99  return !memcmp (luida, luidb, 16);
100 }
101 
102 size_t
104 {
105  return *(size_t*)luid;
106 }
107 
108 /*
109 // Local Variables:
110 // mode: C
111 // c-file-style: "gnu"
112 // indent-tabs-mode: nil
113 // End:
114 */
size_t lumiera_uid_hash(const lumiera_uid *luid)
Generate a hash sum over an luid.
Definition: luid.c:103
Common functions for handling of time values.
void * lumiera_uid_ptr_get(const lumiera_uid *luid)
Retrieve a generic pointer stored in a luid.
Definition: luid.c:41
Lumiera unique object identifier.
void lumiera_uid_gen(lumiera_uid *luid)
Generate a new luid.
Definition: luid.c:48
void lumiera_uid_copy(lumiera_uid *dest, lumiera_uid *src)
Copy an luid.
Definition: luid.c:90
int lumiera_uid_eq(const lumiera_uid *luida, const lumiera_uid *luidb)
Test 2 luid&#39;s for equality.
Definition: luid.c:97
unsigned char lumiera_uid[16]
storage for a Lumiera unique ID, based on a 128bit random number
Definition: hash-value.h:40
void lumiera_uid_set_ptr(lumiera_uid *luid, void *ptr)
Store a generic pointer in a luid.
Definition: luid.c:33