summaryrefslogtreecommitdiffstats
path: root/util/shmctl.c
blob: 92a220e4608e8201518acf2ea9f2445e0ac2a2f6 (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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/* $Id$ */
#include "bbs.h"
#include <sys/wait.h>

extern SHM_t   *SHM;

/* utmpfix ----------------------------------------------------------------- */
int logout_friend_online(userinfo_t *utmp)
{
    int i, j, k;
    int offset=(int) (utmp - &SHM->uinfo[0]);
    userinfo_t *ui;
    while(utmp->friendtotal){
    if( !(0 <= utmp->friendtotal && utmp->friendtotal < MAX_FRIEND) )
        return 1;
    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 = &SHM->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));
}

typedef struct {
    int     index;
    int     idle;
} IDLE_t;

int sfIDLE(const void *a, const void *b)
{
    return ((IDLE_t *)b)->idle - ((IDLE_t *)a)->idle;
}

int utmpfix(int argc, char **argv)
{
    int     i, fast = 0, lowerbound = 100, nownum = SHM->UTMPnumber;
    int     which, nactive = 0;
    time_t  now, timeout = -1;
    char    *clean, buf[1024], ch;
    IDLE_t  idle[USHM_SIZE];
    char    changeflag = 0;

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

    for( i = 0 ; i < 5 ; ++i )
    if( !SHM->UTMPbusystate )
        break;
    else{
        puts("utmpshm is busy....");
        sleep(1);
    }

    SHM->UTMPbusystate = 1;
    printf("starting scaning... %s \n", (fast ? "(fast mode)" : ""));
#ifdef OUTTA_TIMER
    now = SHM->GV2.e.now;
#else
    time(&now);
#endif
    for( i = 0, nactive = 0 ; i < USHM_SIZE ; ++i )
    if( SHM->uinfo[i].pid ){
        idle[nactive].index = i;
        idle[nactive].idle = now - SHM->uinfo[i].lastact;
        ++nactive;
    }
    if( !fast )
    qsort(idle, nactive, sizeof(IDLE_t), sfIDLE);

    for( i = 0 ; i < nactive ; ++i ){
    which = idle[i].index;
    clean = NULL;
    if( !isalpha(SHM->uinfo[which].userid[0]) )
        clean = "userid error";
    else if( kill(SHM->uinfo[which].pid, 0) < 0 ){
        clean = "process error";
        purge_utmp(&SHM->uinfo[which]);
    }
    else if( searchuser(SHM->uinfo[which].userid) == 0 ){
        clean = "user not exist";
    }
#ifdef DOTIMEOUT
    else if( !fast ){
        if( nownum > lowerbound &&
        idle[i].idle > 
        (timeout == -1 ? IDLE_TIMEOUT : timeout) ){
        sprintf(buf, "timeout(%s",
            ctime(&SHM->uinfo[which].lastact));
        buf[strlen(buf) - 1] = 0;
        strcat(buf, ")");
        clean = buf;
        kill(SHM->uinfo[which].pid, SIGHUP);
        printf("%s\n", buf);
        --nownum;
        continue;
        }
    }
#endif
    
    if( clean ){
        printf("clean %06d(%s), userid: %s\n",
           i, clean, SHM->uinfo[which].userid);
        memset(&SHM->uinfo[which], 0, sizeof(userinfo_t));
        --nownum;
        changeflag = 1;
    }
    }
    SHM->UTMPbusystate = 0;
    if( changeflag )
    SHM->UTMPneedsort = 1;
    return 0;
}
/* end of utmpfix ---------------------------------------------------------- */

/* utmpsortd --------------------------------------------------------------- */
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
cmputmpchc(const void *i, const void *j)
{
    int             type;
    if ((type = (*((userinfo_t **) j))->chc_win - (*((userinfo_t **) i))->chc_win))
    return type;
    if ((type = (*((userinfo_t **) i))->chc_lose - (*((userinfo_t **) j))->chc_lose))
    return type;
    return (*((userinfo_t **) i))->chc_tie - (*((userinfo_t **) j))->chc_tie;
}

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;
}

inline void utmpsort(int sortall)
{
    userinfo_t     *uentp;
    int             count, i, ns;
    short           nusers[MAX_BOARD];

    SHM->UTMPbusystate = 1;
#ifdef OUTTA_TIMER
    SHM->UTMPuptime = SHM->GV2.e.now;
#else
    SHM->UTMPuptime = time(NULL);
#endif
    ns = (SHM->currsorted ? 0 : 1);

    for (uentp = &SHM->uinfo[0], count = i = 0;
     i < USHM_SIZE;
     ++i, uentp = &SHM->uinfo[i]) {
    if (uentp->pid) {
        if (uentp->sex < 0 || uentp->sex > 7)
        purge_utmp(uentp);
        else
        SHM->sorted[ns][0][count++] = uentp;
    }
    }
    SHM->UTMPnumber = count;
    qsort(SHM->sorted[ns][0], count, sizeof(userinfo_t *), cmputmpuserid);
    for (i = 0; i < count; ++i)
    ((userinfo_t *) SHM->sorted[ns][0][i])->idoffset = i;
    memcpy(SHM->sorted[ns][6],
       SHM->sorted[ns][0], sizeof(userinfo_t *) * count);
    memcpy(SHM->sorted[ns][7],
       SHM->sorted[ns][0], sizeof(userinfo_t *) * count);
    qsort(SHM->sorted[ns][6], count, sizeof(userinfo_t *), cmputmpuid);
    qsort(SHM->sorted[ns][7], count, sizeof(userinfo_t *), cmputmppid);
    if( sortall ){
    memcpy(SHM->sorted[ns][1],
           SHM->sorted[ns][0], sizeof(userinfo_t *) * count);
    memcpy(SHM->sorted[ns][2],
           SHM->sorted[ns][0], sizeof(userinfo_t *) * count);
    memcpy(SHM->sorted[ns][3],
           SHM->sorted[ns][0], sizeof(userinfo_t *) * count);
    memcpy(SHM->sorted[ns][4],
           SHM->sorted[ns][0], sizeof(userinfo_t *) * count);
    memcpy(SHM->sorted[ns][5],
           SHM->sorted[ns][0], sizeof(userinfo_t *) * count);
    qsort(SHM->sorted[ns][1], count, sizeof(userinfo_t *), cmputmpmode);
    qsort(SHM->sorted[ns][2], count, sizeof(userinfo_t *), cmputmpidle);
    qsort(SHM->sorted[ns][3], count, sizeof(userinfo_t *), cmputmpfrom);
    qsort(SHM->sorted[ns][4], count, sizeof(userinfo_t *), cmputmpfive);
    qsort(SHM->sorted[ns][5], count, sizeof(userinfo_t *), cmputmpchc);
    memset(nusers, 0, sizeof(nusers));
    for (i = 0; i < count; ++i) {
        uentp = SHM->sorted[ns][0][i];
        if (uentp && uentp->pid &&
        0 < uentp->brc_id && uentp->brc_id < MAX_BOARD)
        ++nusers[uentp->brc_id - 1];
    }
    for (i = 0; i < SHM->Bnumber; ++i)
        if (SHM->bcache[i].brdname[0] != 0)
        SHM->bcache[i].nuser = nusers[i];
    }

    SHM->currsorted = ns;
    SHM->UTMPbusystate = 0;
}

int utmpsortd(int argc, char **argv)
{
    pid_t   pid;
    int     interval; // sleep interval in microsecond(1/10**6)
    int     sortall, counter = 0;

    if( fork() > 0 ){
    puts("sortutmpd daemonized...");
    return 0;
    }

    setproctitle("shmctl utmpsortd");
    if( argc < 2 || (interval = atoi(argv[1])) < 500000 )
    interval = 1000000; // default to 1 sec
    sortall = ((argc < 3) ? 1 : atoi(argv[2]));

    while( 1 ){
    if( (pid = fork()) != 0 ){
        int     s;
        waitpid(pid, &s, 0);
    }
    else{
        while( 1 ){
        int     i;
        for( i = 0 ; SHM->UTMPbusystate && i < 5 ; ++i )
            usleep(300000);

        if( SHM->UTMPneedsort ){
            if( ++counter == sortall ){
            utmpsort(1);
            counter = 0;
            }
            else
            utmpsort(0);
        }

        usleep(interval);
        }
    }
    }
}
/* end of utmpsortd -------------------------------------------------------- */

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

int utmpstatus(int argc, char **argv)
{
    time_t  now;
    char    upbuf[64], nowbuf[64];
#ifdef OUTTA_TIMER
    now = SHM->GV2.e.now;
#else
    now = time(NULL);
#endif
    CTIMEx(upbuf,  SHM->UTMPuptime);
    CTIMEx(nowbuf, now);
    printf("now:        %s\n", nowbuf);
    printf("currsorted: %d\n", SHM->currsorted);
    printf("uptime:     %s\n", upbuf);
    printf("number:     %d\n", SHM->UTMPnumber);
    printf("busystate:  %d\n", SHM->UTMPbusystate);
    return 0;
}

int utmpreset(int argc, char **argv)
{
    SHM->UTMPbusystate=0;
    utmpstatus(0, NULL);
    return 0;
}

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

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

char    *GV2str[] = {"dymaxactive", "toomanyusers",
             "noonlineuser", NULL};
int showglobal(int argc, char **argv)
{
    int     i;
    for( i = 0 ; i < 10 ; ++i )
    printf("GLOBALVAR[%d] = %d\n", i, SHM->GLOBALVAR[i]);
    for( i = 0 ; GV2str[i] != NULL ; ++i )
    printf("GV2.%s = %d\n", GV2str[i], SHM->GV2.v[i]);
    return 0;
}

int setglobal(int argc, char **argv)
{
    int     where, value;
    if( argc != 3 ){
    puts("usage: shmctl setglobal ([0-9]|GV2) newvalue");
    return 1;
    }
    where = argv[1][0] - '0';
    value = atoi(argv[2]);

    if( 0 <= where && where <= 9 ){
    printf("GLOBALVAR[%d] = %d -> ", where, SHM->GLOBALVAR[where]);
    printf("%d\n", SHM->GLOBALVAR[where] = value);
    return 0;
    }
    else{
    for( where = 0 ; GV2str[where] != NULL ; ++where )
        if( strcmp(GV2str[where], argv[1]) == 0 ){
        printf("GV2.%s = %d -> ", GV2str[where], SHM->GV2.v[where]);
        printf("%d\n", SHM->GV2.v[where] = value);
        return 0;
        }
    }
    printf("GLOBALVAR %s not found\n", argv[1]);

    return 1;
}

int listpid(int argc, char **argv)
{
    int     i;
    for( i = 0 ; i < USHM_SIZE ; ++i )
    if( SHM->uinfo[i].pid > 0 )
        printf("%d\n", SHM->uinfo[i].pid);
    return 0;
}

int listbrd(int argc, char **argv)
{
    int     i, ch;
    int     noHidden = 0;

    while( (ch = getopt(argc, argv, "hn")) != -1 )
    switch( ch ){
    case 'n':
        noHidden = 1;
        break;
    case 'h':
    default:
        printf("usage:\tshmctl\tlistbrd [-n]\n"
           "\t-n no hidden board\n");
        return 0;
    }

    for( i = 0 ; i < MAX_BOARD ; ++i )
    if( bcache[i].brdname[0] && !(bcache[i].brdattr & BRD_GROUPBOARD) &&
        (!noHidden ||
         !((bcache[i].brdattr & BRD_HIDE) ||
           (bcache[i].level && !(bcache[i].brdattr & BRD_POSTMASK) &&
        (bcache[i].level & 
         ~(PERM_BASIC|PERM_CHAT|PERM_PAGE|PERM_POST|PERM_LOGINOK))))) )
        printf("%s\n", bcache[i].brdname);
    return 0;
}

#ifdef OUTTA_TIMER
int timed(int argc, char **argv)
{
    pid_t   pid;
    if( (pid = fork()) < 0 )
    perror("fork()");
    if( pid != 0 )
    return 0;
    setproctitle("shmctl timed");
    while( 1 ){
    SHM->GV2.e.now = time(NULL);
    sleep(1);
    }
}
#endif

#if 0
void buildclass(int bid, int level)
{
    boardheader_t  *bptr;
    if( level == 20 ){ /* for safty */
    printf("is there something wrong? class level: %d\n", level);
    return;
    }
    bptr = &bcache[bid];
    if (bptr->firstchild[0] == NULL || bptr->childcount <= 0)
        load_uidofgid(bid + 1, 1); /* 因為這邊 bid從 0開始, 所以再 +1 回來 */
    if (bptr->firstchild[1] == NULL || bptr->childcount <= 0)
        load_uidofgid(bid + 1, 1); /* 因為這邊 bid從 0開始, 所以再 +1 回來 */

    for (bptr = bptr->firstchild[0]; bptr != NULL ; bptr = bptr->next[0]) {
    if( bptr->brdattr & BRD_GROUPBOARD )
            buildclass(bptr - bcache, level + 1);
    }
}
#endif

int bBMC(int argc, char **argv)
{
    int     i;
    for( i = 0 ; i < MAX_BOARD ; ++i )
    if( bcache[i].brdname[0] )
        buildBMcache(i + 1); /* XXXbid */
    return 0;
}

int SHMinit(int argc, char **argv)
{
    int     ch;
    int     no_uhash_loader = 0;
    while( (ch = getopt(argc, argv, "n")) != -1 )
    switch( ch ){
    case 'n':
        no_uhash_loader = 1;
        break;
    default:
        printf("usage:\tshmctl\tSHMinit\n");
        return 0;
    }

    puts("loading uhash...");
    system("bin/uhash_loader");

    attach_SHM();

    puts("loading bcache...");
    reload_bcache();

    puts("building BMcache...");
    bBMC(1, NULL);

#if 0
    puts("building class...");
    buildclass(0, 0);
#endif

    if( !no_uhash_loader ){
    puts("utmpsortd...");
    utmpsortd(1, NULL);
    }

#ifdef OUTTA_TIMER
    puts("timed...");
    timed(1, NULL);
#endif
    return 0;
}

struct {
    int     (*func)(int, char **);
    char    *cmd, *descript;
} cmd[] =
    { {utmpfix,    "utmpfix",    "clear dead userlist entry"},
      {utmpsortd,  "utmpsortd",  "utmp sorting daemon"},
      {utmpstatus, "utmpstatus", "list utmpstatus"},
      {utmpreset,  "utmpreset",  "SHM->busystate=0"},
      {utmpwatch,  "utmpwatch",  "to see if busystate is always 1 then fix it"},
      {utmpnum,    "utmpnum",    "print SHM->number for snmpd"},
      {showglobal, "showglobal", "show GLOBALVAR[]"},
      {setglobal,  "setglobal",  "set GLOBALVAR[]"},
      {listpid,    "listpid",    "list all pids of mbbsd"},
      {listbrd,    "listbrd",    "list board info in SHM"},
#ifdef OUTTA_TIMER
      {timed,      "timed",      "time daemon for OUTTA_TIMER"},
#endif
      {bBMC,       "bBMC",       "build BM cache"},
      {SHMinit,    "SHMinit",    "initialize SHM (including uhash_loader)"},
      {NULL, NULL, NULL} };

extern char ** environ;

int main(int argc, char **argv)
{
    int     i = 0;
    
    chdir(BBSHOME);
    initsetproctitle(argc, argv, environ);
    if( argc >= 2 ){
    if( strcmp(argv[1], "init") == 0 ){
        /* in this case, do NOT run attach_SHM here.
           because uhash_loader is not run yet.      */
        return SHMinit(argc - 1, &argv[1]);
    }

    attach_SHM();
    /* shmctl doesn't need resolve_boards() first */
    //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;
}