10 #define VSERVER_CONFIG_PATH "/etc/vservers"
11 #define SLICE_ID_FILE "slice_id"
13 char* get_current_username (unsigned int uid)
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);
21 return (strdup(passwd_entry->pw_name));
24 #define HASH_SIZE (1<<12)
25 #define HASH_TABLE_MASK (HASH_SIZE - 1)
32 struct hash_entry slice_id_hash[HASH_SIZE];
34 void init_slice_id_hash() {
35 memset(slice_id_hash, 0, HASH_SIZE);
37 void set_hash_entry(unsigned int xid, uint32_t slice_id) {
38 int idx = xid & HASH_TABLE_MASK;
41 struct hash_entry *entry = &slice_id_hash[idx];
42 if (entry->xid == 0 || entry->xid ==xid) {
43 entry->slice_id = slice_id;
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;
58 uint32_t xid_to_slice_id_slow(unsigned int xid) {
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");
65 fscanf(fp,"%u",&slice_id);
66 set_hash_entry(xid, slice_id);
69 slice_id = xid; // Let's leave some evidence behind, even if it's imperfect.
74 uint32_t xid_to_slice_id_fast(unsigned int xid) {
75 int idx = xid & HASH_TABLE_MASK;
77 uint32_t slice_id = 0;
79 struct hash_entry *entry = &slice_id_hash[idx];
80 if (entry->xid == xid)
81 return entry->slice_id;
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;
94 uint32_t xid_to_slice_id(unsigned int xid) {
98 else if ((slice_id = xid_to_slice_id_fast(xid)) != 0)
101 return xid_to_slice_id_slow(xid);