summaryrefslogtreecommitdiffstats
path: root/util/shmctl.c
blob: f2ed297314b91ba9bfa7d6821a1e7c8233f46a4d (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <errno.h>
#include "config.h"
#include "pttstruct.h"
#include "util.h"
#include "proto.h"

extern struct utmpfile_t *utmpshm;
extern struct pttcache_t *ptt;
extern bcache_t *brdshm;

int logout_friend_online(userinfo_t *utmp)
{
    int i, j, k;
    int offset=(int) (utmp - &utmpshm->uinfo[0]);
    userinfo_t *ui;
    while(utmp->friendtotal){
    i = utmp->friendtotal-1;
    j = (utmp->friend_online[i] & 0xFFFFFF);
    if( !(0 <= j && j < MAX_ACTIVE) )
        printf("\tonline friend error(%d)\n", j);
    else{
        utmp->friend_online[i]=0;
        ui = &utmpshm->uinfo[j]; 
        if(ui->pid && ui!=utmp){
        for(k=0; k<ui->friendtotal && 
            (int)(ui->friend_online[k] & 0xFFFFFF) !=offset; k++);
        if(k<ui->friendtotal){
            ui->friendtotal--;
            ui->friend_online[k]=ui->friend_online[ui->friendtotal];
            ui->friend_online[ui->friendtotal]=0;
        }
        }
    }
    utmp->friendtotal--;
    utmp->friend_online[utmp->friendtotal]=0;
    }
    return 0;
}

void purge_utmp(userinfo_t *uentp)
{
    logout_friend_online(uentp);
    //memset(uentp, 0, sizeof(userinfo_t));
}

int utmpfix(int argc, char **argv)
{
    int     i, fast = 0;
    time_t  now, timeout = -1;
    char    *clean, buf[1024], ch;

    while( (ch = getopt(argc, argv, "nt:")) != -1 )
    switch( ch ){
    case 'n':
        fast = 1;
        break;
    case 't':
        timeout = atoi(optarg);
        break;
    default:
        printf("usage:\tshmctl\tutmpfix[-n] [-t timeout]\n");
        return 1;
    }

    time(&now);
    for( i = 0 ; i < 5 ; ++i )
    if( !utmpshm->busystate )
        break;
    else{
        puts("utmpshm is busy....");
        sleep(1);
    }
    printf("starting scaning... %s \n", (fast ? "(fast mode)" : ""));
    utmpshm->busystate = 1;
    for( i = 0 ; i < USHM_SIZE ; ++i )
    if( utmpshm->uinfo[i].pid ){
        clean = NULL;
        if( !isalpha(utmpshm->uinfo[i].userid[0]) )
        clean = "userid error";
        else if( kill(utmpshm->uinfo[i].pid, 0) < 0 ){
        clean = "process error";
        purge_utmp(&utmpshm->uinfo[i]);
        }
        else if( !fast ){
#ifdef DOTIMEOUT
        if( now - utmpshm->uinfo[i].lastact > 
            (timeout == -1 ? IDLE_TIMEOUT : timeout) ){
            sprintf(buf, "timeout(%s",
                ctime(&utmpshm->uinfo[i].lastact));
            buf[strlen(buf) - 1] = 0;
            strcat(buf, ")");
            clean = buf;
            kill(utmpshm->uinfo[i].pid, SIGHUP);
            purge_utmp(&utmpshm->uinfo[i]);
        }
        else
#endif
            if( searchuser(utmpshm->uinfo[i].userid) == 0 ){
            clean = "user not exist";
            } 
        }
        
        if( clean ){
        printf("clean %06d(%s), userid: %s\n",
               i, clean, utmpshm->uinfo[i].userid);
        memset(&utmpshm->uinfo[i], 0, sizeof(userinfo_t));
        }
        else if ( utmpshm->uinfo[i].mind > 40 ){
        printf("mind fix: %06d, userid: %s, mind: %d\n",
               i, utmpshm->uinfo[i].userid, utmpshm->uinfo[i].mind);
        utmpshm->uinfo[i].mind %= 40;
        }
    }
    utmpshm->busystate = 0;
    return 0;
}

char *CTIME(char *buf, time_t t)
{
    strcpy(buf, ctime(&t));
    buf[strlen(buf) - 1] = 0;
    return buf;
}

int utmpstate(int argc, char **argv)
{
    time_t  now;
    char    upbuf[64], nowbuf[64];
    now = time(NULL);
    CTIME(upbuf,  utmpshm->uptime);
    CTIME(nowbuf, time(NULL));
    printf("now:        %s\n", nowbuf);
    printf("currsorted: %d\n", utmpshm->currsorted);
    printf("uptime:     %s\n", upbuf);
    printf("number:     %d\n", utmpshm->number);
    printf("busystate:  %d\n", utmpshm->busystate);
    return 0;
}

int utmpreset(int argc, char **argv)
{
    utmpshm->busystate=0;
    utmpstate(0, NULL);
    return 0;
}

/* ulistsort */
static int cmputmpuserid(const void *i, const void *j){
    return strcasecmp((*((userinfo_t**)i))->userid,
              (*((userinfo_t**)j))->userid);
}

static int cmputmpmode(const void *i, const void *j){
    return (*((userinfo_t**)i))->mode-
    (*((userinfo_t**)j))->mode;
} 

static int cmputmpidle(const void *i, const void *j){
    return (*((userinfo_t**)i))->lastact-
    (*((userinfo_t**)j))->lastact;
} 

static int cmputmpfrom(const void *i, const void *j){
  return strcasecmp((*((userinfo_t**)i))->from,
            (*((userinfo_t**)j))->from);
} 

static int cmputmpfive(const void *i, const void *j){
    int type;
    if((type=(*((userinfo_t**)j))->five_win - (*((userinfo_t**)i))->five_win))
    return type;
    if((type=(*((userinfo_t**)i))->five_lose-(*((userinfo_t**)j))->five_lose))
    return type;
    return (*((userinfo_t**)i))->five_tie-(*((userinfo_t**)j))->five_tie;
} 

static int cmputmpsex(const void *i, const void *j){
    static int ladyfirst[]={1,0,1,0,1,0,3,3};
    return ladyfirst[(*((userinfo_t**)i))->sex]-
    ladyfirst[(*((userinfo_t**)j))->sex];
}
static int cmputmppid(const void *i, const void *j){
    return (*((userinfo_t**)i))->pid-(*((userinfo_t**)j))->pid;
}
static int cmputmpuid(const void *i, const void *j){
    return (*((userinfo_t**)i))->uid-(*((userinfo_t**)j))->uid;
}

int utmpsort(int argc, char **argv)
{
    time_t now=time(NULL);
    int count, i, ns;
    userinfo_t *uentp;
    
    if(now-utmpshm->uptime<60 && (now==utmpshm->uptime || utmpshm->busystate)){
    puts("lazy sort");
    //return; /* lazy sort */
    }
    utmpshm->busystate=1;
    utmpshm->uptime = now;
    ns=(utmpshm->currsorted?0:1);

    for(uentp = &utmpshm->uinfo[0], count=0, i=0;
    i< USHM_SIZE; i++,uentp = &utmpshm->uinfo[i])
    if(uentp->pid){
        utmpshm->sorted[ns][0][count++]= uentp;
        }
    utmpshm->number = count;
    qsort(utmpshm->sorted[ns][0],count,sizeof(userinfo_t*),cmputmpuserid);
    memcpy(utmpshm->sorted[ns][1],utmpshm->sorted[ns][0],
                         sizeof(userinfo_t *)*count);
    memcpy(utmpshm->sorted[ns][2],utmpshm->sorted[ns][0],
                         sizeof(userinfo_t *)*count);
    memcpy(utmpshm->sorted[ns][3],utmpshm->sorted[ns][0],
                         sizeof(userinfo_t *)*count);
    memcpy(utmpshm->sorted[ns][4],utmpshm->sorted[ns][0],
                         sizeof(userinfo_t *)*count);
    memcpy(utmpshm->sorted[ns][5],utmpshm->sorted[ns][0],
                         sizeof(userinfo_t *)*count);
    memcpy(utmpshm->sorted[ns][6],utmpshm->sorted[ns][0],
                         sizeof(userinfo_t *)*count);
    memcpy(utmpshm->sorted[ns][7],utmpshm->sorted[ns][0],
                         sizeof(userinfo_t *)*count);
    qsort(utmpshm->sorted[ns][1], count, sizeof(userinfo_t *), cmputmpmode );
    qsort(utmpshm->sorted[ns][2], count, sizeof(userinfo_t *), cmputmpidle );
    qsort(utmpshm->sorted[ns][3], count, sizeof(userinfo_t *), cmputmpfrom );
    qsort(utmpshm->sorted[ns][4], count, sizeof(userinfo_t *), cmputmpfive );
    //qsort(utmpshm->sorted[ns][5], count, sizeof(userinfo_t *), cmputmpsex );
    qsort(utmpshm->sorted[ns][6], count, sizeof(userinfo_t *), cmputmpuid );
    qsort(utmpshm->sorted[ns][7], count, sizeof(userinfo_t *), cmputmppid );
    utmpshm->currsorted=ns;
    utmpshm->busystate=0;

    puts("new utmpstate");
    utmpstate(0, NULL);
    return 0;
}
/* end of ulistsort */

#define TIMES   10
int utmpwatch(int argc, char **argv)
{
    int     i;
    while( 1 ){
    for( i = 0 ; i < TIMES ; ++i ){
        usleep(300);
        if( !utmpshm->busystate )
        break;
        puts("busying...");
    }
    if( i == TIMES ){
        puts("reset!");
        utmpshm->busystate = 0;
    }
    }
    return 0;
}

int utmpnum(int argc, char **argv)
{
    printf("%d.0\n", utmpshm->number);
    return 0;
}

int showglobe(int argc, char **argv)
{
    int     i;
    for( i = 0 ; i < 10 ; ++i )
    printf("GLOBE[%d] = %d\n", i, ptt->GLOBE[i]);
    return 0;
}

int setglobe(int argc, char **argv)
{
    int     where;
    if( argc != 3 )
    return 1;
    where = atoi(argv[1]);
    if( !(0 <= where && where <= 9) ){
    puts("only GLOBE[0] ~ GLOBE[9]");
    return 1;
    }
    printf("GLOBE[%d] = %d -> ", where, ptt->GLOBE[where]);
    printf("%d\n", ptt->GLOBE[where] = atoi(argv[2]));
    return 0;
}

int fixbfriend(int argc, char **argv)
{
    userinfo_t      *ptr;
    int     count, i;

    for( i = 0 ; i < MAX_BOARD ; ++i ){
    if( isdigit(brdshm->bcache[i].brdname[0]) ||
        isalpha(brdshm->bcache[i].brdname[0])    ){
        for( count = 0, ptr = brdshm->bcache[i].u ; 
         ptr != NULL && count < 256           ;
         ++count, ptr = ptr->nextbfriend        )
        ;
        printf("counting %s\n", brdshm->bcache[i].brdname);
        brdshm->bcache[i].nuser = ((count == 256) ? 0 : count);
    }
    }

    return 0;
}

struct {
    int     (*func)(int, char **);
    char    *cmd, *descript;
} cmd[] =
    { {utmpfix,    "utmpfix",    "clear dead userlist entry"},
      {utmpstate,  "utmpstate",  "list utmpstate"},
      {utmpreset,  "utmpreset",  "utmpshm->busystate=0"},
      {utmpsort,   "utmpsort",   "sort ulist"},
      {utmpwatch,  "utmpwatch",  "to see if busystate is always 1 then fix it"},
      {utmpnum,    "utmpnum",    "print utmpshm->number for snmpd"},
      {showglobe,  "showglobe",  "show GLOBE"},
      {setglobe,   "setglobe",   "set GLOBE"},
      {fixbfriend, "fixbfriend", "recount numbers of board friends"},
      {NULL, NULL, NULL} };

int main(int argc, char **argv)
{
    int     i = 0;
    
    if( argc >= 2 ){
    /* shmctl shouldn't create shm itself */
        int     shmid = shmget(UHASH_KEY, sizeof(uhash_t), 0);
    if( shmid < 0 ){
      printf("%d\n", errno);
        perror("attach utmpshm");
        return 1;
    }
    chdir(BBSHOME);
    resolve_utmp();
    resolve_boards();
    resolve_garbage();
    resolve_fcache();
    for( i = 0 ; cmd[i].func != NULL ; ++i )
        if( strcmp(cmd[i].cmd, argv[1]) == 0 ){
        return cmd[i].func(argc - 1, &argv[1]);
        }
    }
    if( argc == 1 || cmd[i].func == NULL ){
    /* usage */
    printf("usage: bbsctl [command] [options]\n");
    printf("commands:\n");
    for( i = 0 ; cmd[i].func != NULL ; ++i )
        printf("\t%-15s%s\n", cmd[i].cmd, cmd[i].descript);
    }
    return 0;
}