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 set_hash_entry(unsigned int xid, uint32_t slice_id) {
35 int idx = xid & HASH_TABLE_MASK;
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;
46 uint32_t xid_to_slice_id_slow(unsigned int xid) {
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");
53 fscanf(fp,"%u",&slice_id);
54 set_hash_entry(xid, slice_id);
57 slice_id = xid; // Let's leave some evidence behind, even if it's imperfect.
62 uint32_t xid_to_slice_id_fast(unsigned int xid) {
63 int idx = xid & HASH_TABLE_MASK;
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;
76 uint32_t xid_to_slice_id(unsigned int xid) {
80 else if ((slice_id = xid_to_slice_id_fast(xid)) != 0)
83 return xid_to_slice_id_slow(xid);