Merge branch 'master' of ssh://git.planet-lab.org/git/fprobe-ulog
[fprobe-ulog.git] / src / vserver-retired.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 init_slice_id_hash() {
35     memset(slice_id_hash, 0, HASH_SIZE);
36 }
37 void set_hash_entry(unsigned int xid, uint32_t slice_id) {
38     int idx = xid & HASH_TABLE_MASK;
39     int i;
40
41     struct hash_entry *entry = &slice_id_hash[idx];
42     if (entry->xid == 0 || entry->xid ==xid) {
43         entry->slice_id = slice_id;
44         entry->xid = xid;
45         return;
46     }
47
48     for (i = idx+1;i!=idx;i=(i+1) & HASH_TABLE_MASK) {
49         entry = &slice_id_hash[i];
50         if (entry->xid == 0 || entry->xid == xid) {
51             entry->slice_id = slice_id;
52             entry->xid = xid;
53             break;
54         }
55     }
56 }
57
58 uint32_t xid_to_slice_id_slow(unsigned int xid) {
59     uint32_t slice_id;
60     char *slice_name = get_current_username (xid);
61     char *slice_path = (char *) malloc(sizeof(VSERVER_CONFIG_PATH) + strlen(slice_name) + sizeof(SLICE_ID_FILE) + sizeof("//"));
62     sprintf(slice_path,"%s/%s/%s",VSERVER_CONFIG_PATH,slice_name,SLICE_ID_FILE);
63     FILE *fp = fopen(slice_path, "r");
64     if (fp) {
65         fscanf(fp,"%u",&slice_id);
66         set_hash_entry(xid, slice_id);
67     }
68     else
69         slice_id = xid; // Let's leave some evidence behind, even if it's imperfect.
70     fclose(fp);
71     return slice_id;
72 }
73
74 uint32_t xid_to_slice_id_fast(unsigned int xid) {
75     int idx = xid & HASH_TABLE_MASK;
76     int i;
77     uint32_t slice_id = 0;
78
79     struct hash_entry *entry = &slice_id_hash[idx];
80     if (entry->xid == xid)
81         return entry->slice_id;
82
83     for (i = idx+1;i!=idx;i=(i+1) & HASH_TABLE_MASK) {
84         entry = &slice_id_hash[i];
85         if (entry->xid == xid) {
86             slice_id = entry->slice_id;
87             break;
88         }
89     }
90
91     return slice_id;
92 }
93
94 uint32_t xid_to_slice_id(unsigned int xid) {
95     uint32_t slice_id;
96     if (xid == 0)
97         return 0;
98     else if ((slice_id = xid_to_slice_id_fast(xid)) != 0)
99         return slice_id;
100     else
101         return xid_to_slice_id_slow(xid);
102 }
103
104
105 #endif