summaryrefslogtreecommitdiffstats
path: root/util/broadcast.c
blob: 13dab0867ca4cdc28ad39a12632779d067e5a067 (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
/* $Id$ */
#include "bbs.h"
#include <getopt.h>

extern SHM_t   *SHM;

int main(int argc, char *argv[])
{
    int sleep_time = 5;
    int num_per_loop = 500;

    int i, j;
    userinfo_t *uentp;
    msgque_t msg;
    time_t now;
    int *sorted, UTMPnumber; // SHM snapshot

    while ((i = getopt(argc, argv, "t:n:")) != -1)
    switch (i) {
        case 't':
        sleep_time = atoi(optarg);
        break;
        case 'n':
        num_per_loop = atoi(optarg);
        break;
    }

    if (optind == argc || strlen(argv[optind]) == 0) {
    fprintf(stderr, "no message to broadcast\n\n");
    return 1;
    }

    printf("broadcast \"%s\" ? [y/N]\n", argv[optind]);
    if (tolower(getchar()) != 'y')
    return 0;

    attach_SHM();
    sorted = (int *)malloc(sizeof(int) * USHM_SIZE);
    memcpy(sorted, SHM->sorted[SHM->currsorted][0], sizeof(int) * USHM_SIZE);
    UTMPnumber = SHM->UTMPnumber;

    msg.pid = currpid;
    strlcpy(msg.userid, "系統廣播", sizeof(msg.userid));
    snprintf(msg.last_call_in, sizeof(msg.last_call_in), "[廣播]%s", argv[optind]);

    now = time(NULL);

    for (i = 0, j = 1; i < UTMPnumber; ++i, ++j) {
    if (j == num_per_loop) {
        fprintf(stderr, "%5d/%5d\n", i + 1, UTMPnumber);
        j = 1;
        now = time(NULL);
        sleep(sleep_time);
    }

    // XXX why use sorted list?
    //     can we just scan uinfo with proper checking?
    uentp = &SHM->uinfo[sorted[i]];
    if (uentp->pid && kill(uentp->pid, 0) != -1){
        int write_pos = uentp->msgcount;
        if (write_pos < (MAX_MSGS - 1)){
        uentp->msgcount = write_pos + 1;
        memcpy(&uentp->msgs[write_pos], &msg, sizeof(msg));
#ifdef NOKILLWATERBALL
        uentp->wbtime = (time4_t)now;
#else
        kill(uentp->pid, SIGUSR2);
#endif
        }
    }
    }
    return 0;
}