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

extern SHM_t   *SHM;

void print_help(int argc, char *argv[])
{
    fprintf(stderr, "Usage: %s [-t sleep_time] [-n users_per_round] [-o broadcast_name] broadcast content\n\n", argv[0]);
}

int main(int argc, char *argv[])
{
    int sleep_time = 5;
    int num_per_loop = 500;
    char * owner = "系統廣播";

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

    while ((i = getopt(argc, argv, "t:n:o:h")) != -1)
    switch (i) {
        case 'h':
        print_help(argc, argv);
        return 0;
        break;
        case 't':
        sleep_time = atoi(optarg);
        break;
        case 'n':
        num_per_loop = atoi(optarg);
        break;
        case 'o':
        owner = 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 = getpid();
    strlcpy(msg.userid, owner, sizeof(msg.userid));
    snprintf(msg.last_call_in, sizeof(msg.last_call_in), "[廣播]%s", argv[optind]);

    now = time(NULL);

    for (i = 0, j = 0; i < UTMPnumber; ++i, ++j) {
    if (j == num_per_loop) {
        fprintf(stderr, "%5d/%5d\n", i + 1, UTMPnumber);
        j = 0;
        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;
}