summaryrefslogtreecommitdiffstats
path: root/util/uhash_loader.c
blob: 550067b80a9b5157b5b938ace582b42bcb284573 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* $Id$ */
/* standalone uhash loader -- jochang */
#include "bbs.h"

unsigned string_hash(unsigned char *s);
void userec_add_to_uhash(int n, userec_t *id, int onfly);
void fill_uhash(int onfly);
void load_uhash(void);

SHM_t *SHM;

int main() {
    setgid(BBSGID);
    setuid(BBSUID);
    chdir(BBSHOME);
    load_uhash();
    return 0;
}

void load_uhash(void) {
    int shmid, err;
    shmid = shmget(SHM_KEY, sizeof(SHM_t), IPC_CREAT | IPC_EXCL | 0600);
    err = errno;
    if( err == EEXIST)
    shmid = shmget(SHM_KEY, sizeof(SHM_t), IPC_CREAT | 0600);
    if (shmid < 0)
    {
    perror("shmget");
    exit(1);
    }
    SHM = (void *) shmat(shmid, NULL, 0);
    if (SHM == (void *) -1)
    {
    perror("shmat");
    exit(1);
    }
    if( err  != EEXIST)
    SHM->number=SHM->loaded = 0;


// in case it's not assumed zero, this becomes a race... 
    if(SHM->number==0 && SHM->loaded == 0)
    {
          SHM->loaded = 0;
          fill_uhash(0);
          SHM->loaded = 1;
    }
    else
        {
         fill_uhash(1); 
        }
}

void checkhash(int h)
{
    int *p = &(SHM->hash_head[h]), ch, deep=0;
    while(*p != -1)
    {
       ch = string_hash( SHM->userid[*p]);
       if(ch!=h)
       {
           printf("remove %d %d!=%d %d [%s] next:%d\n", 
            deep, h, ch, *p, SHM->userid[*p],
            SHM->next_in_hash[*p]);
          *p = SHM->next_in_hash[*p]; //remove from link
          // *p=-1;  Ptt: cut it?
      //return;
       }
       else
          p = &(SHM->next_in_hash[*p]);
       deep++;
    }
}
void fill_uhash(int onfly)
{
    int fd, usernumber;
    usernumber = 0;

    for (fd = 0; fd < (1 << HASH_BITS); fd++)
      if(!onfly)
          SHM->hash_head[fd] = -1;
      else
      checkhash(fd);
    
    if ((fd = open(FN_PASSWD, O_RDWR)) > 0)
    {
    struct stat stbuf;
    caddr_t fimage, mimage;

    fstat(fd, &stbuf);
    fimage = mmap(NULL, stbuf.st_size, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);
    if (fimage == (char *) -1)
    {
        perror("mmap");
        exit(1);
    }
    close(fd);
    fd = stbuf.st_size / sizeof(userec_t);
    if (fd > MAX_USERS)
        fd = MAX_USERS;
            
    for (mimage = fimage; usernumber < fd; mimage += sizeof(userec_t))
    {
        userec_add_to_uhash(usernumber, (userec_t *)mimage, onfly);
        usernumber++;
    }
    munmap(fimage, stbuf.st_size);
    }
    else
    {
    perror("open");
    exit(1);
    }
    SHM->number = usernumber;

    printf("total %d names %s.\n", usernumber, onfly ? "checked":"loaded");
}
unsigned string_hash(unsigned char *s)
{
    unsigned int v = 0;
    while (*s)
    {
    v = (v << 8) | (v >> 24);
    v ^= toupper(*s++); /* note this is case insensitive */
    }
    return (v * 2654435769U) >> (32 - HASH_BITS);
}

void userec_add_to_uhash(int n, userec_t *user, int onfly)
{
    int *p, h, l=0;

    h = string_hash(user->userid);
    
    p = &(SHM->hash_head[h]);
    if(!onfly || SHM->userid[n][0] != user->userid[0] || 
           strncmp(SHM->userid[n], user->userid, IDLEN-1))
    {
       strcpy(SHM->userid[n], user->userid);
       SHM->money[n] = user->money;
       if(onfly)
           printf("add %s\n", user->userid);
    }
    while (*p != -1)
    {
    if(onfly && *p==n )  // already in hash
         return;
    l++;
    p = &(SHM->next_in_hash[*p]);
    }
    if(onfly)
       printf("add %d %d %d [%s] in hash\n", l, h, n, user->userid);
    SHM->next_in_hash[*p = n] = -1;
}