6037db3d5164a8725ee23ac7c6c1162062dcca54
[fprobe-ulog.git] / src / vserver.h
1 #ifndef __VSERVER_H__
2 #define __VSERVER_H__
3
4 #include <pwd.h>
5 #include <stdint.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9
10 #define VSERVER_CONFIG_PATH "/etc/vservers"
11 #define SLICE_ID_FILE       "slice_id"
12
13 char* get_current_username (unsigned int uid)
14 {
15     struct passwd *passwd_entry;
16     if ((passwd_entry = getpwuid(uid)) == NULL) {
17         fprintf(stderr, "Could not look up user record for %d\n", uid);
18         return NULL; 
19     }
20
21     return (strdup(passwd_entry->pw_name));
22 }
23
24 #define HASH_SIZE           (1<<12)
25 #define HASH_TABLE_MASK     (HASH_SIZE - 1)
26
27 struct hash_entry {
28     unsigned int xid;
29     uint32_t slice_id;
30 };
31
32 struct hash_entry slice_id_hash[HASH_SIZE];
33
34 void set_hash_entry(unsigned int xid, uint32_t slice_id) {
35     int idx = xid & HASH_TABLE_MASK;
36     int i;
37     for (i = idx;i!=idx;i=(i+1) & HASH_TABLE_MASK) {
38         struct hash_entry *entry = &slice_id_hash[i];
39         if (entry->xid == 0 || entry->xid == xid) {
40             entry->xid = slice_id;
41             break;
42         }
43     }
44 }
45
46 uint32_t xid_to_slice_id_slow(unsigned int xid) {
47     uint32_t slice_id;
48     char *slice_name = get_current_username (xid);
49     char *slice_path = (char *) malloc(sizeof(VSERVER_CONFIG_PATH) + strlen(slice_name) + sizeof(SLICE_ID_FILE) + sizeof("//"));
50     sprintf(slice_path,"%s/%s/%s",VSERVER_CONFIG_PATH,slice_name,SLICE_ID_FILE);
51     FILE *fp = fopen(slice_path, "r");
52     if (fp) {
53         fscanf(fp,"%u",&slice_id);
54         set_hash_entry(xid, slice_id);
55     }
56     else
57         slice_id = xid; // Let's leave some evidence behind, even if it's imperfect.
58     fclose(fp);
59     return slice_id;
60 }
61
62 uint32_t xid_to_slice_id_fast(unsigned int xid) {
63     int idx = xid & HASH_TABLE_MASK;
64     int i;
65     uint32_t slice_id;
66     for (i = idx;i!=idx;i=(i+1) & HASH_TABLE_MASK) {
67         struct hash_entry *entry = &slice_id_hash[i];
68         if (entry->xid == xid) {
69             entry->xid = slice_id;
70             break;
71         }
72     }
73     
74 }
75
76 uint32_t xid_to_slice_id(unsigned int xid) {
77     uint32_t slice_id;
78     if (xid == 0)
79         return 0;
80     else if ((slice_id = xid_to_slice_id_fast(xid)) != 0)
81         return slice_id;
82     else
83         return xid_to_slice_id_slow(xid);
84 }
85
86 #endif