a3c4cf267f5c9a03d041c3a253d37a8da5ab8b33
[umurmur.git] / src / sharedmemory.c
1 #include "sharedmemory.h"
2
3 int shm_fd;
4 shm_t *shmptr = NULL;
5 char shm_file_name[128];
6
7 void Sharedmemory_init( int bindport, int bindport6 )
8 {
9
10         int server_max_clients = getIntConf(MAX_CLIENTS);
11         int shmtotal_size =  sizeof( shm_t  ) + (sizeof( shmclient_t ) * server_max_clients);
12
13         if( !bindport )
14         {
15                 bindport = getIntConf(BINDPORT);
16         }
17
18         sprintf( shm_file_name, "/umurmurd:%i", bindport );
19         Log_info("SHM_API: shm_fd=\"%s\"", shm_file_name  );
20
21         shm_fd = shm_open( shm_file_name, O_CREAT | O_RDWR, 0660 );
22         if(shm_fd == -1)
23         {
24                 Log_fatal( "SHM_API: Open failed:%s\n", strerror(errno));
25                 exit(EXIT_FAILURE);
26         }
27
28         if( ftruncate( shm_fd, shmtotal_size ) == -1 )
29         {
30                 Sharedmemory_deinit();
31                 Log_fatal( "SHM_API: ftruncate : %s\n", strerror(errno));
32                 exit(EXIT_FAILURE);
33         }
34
35         shmptr = mmap( 0, shmtotal_size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0 );
36         if (shmptr == MAP_FAILED)
37         {
38                 Log_fatal( "SHM_API: mmap failed : %s\n", strerror(errno));
39                 exit(EXIT_FAILURE);
40         }
41
42         memset( shmptr, 0, shmtotal_size );
43
44         shmptr->umurmurd_pid = getpid();
45         shmptr->server_max_clients = server_max_clients;
46         shmptr->shmtotal_size = shmtotal_size;
47         shmptr->shmclient_size = sizeof( shmclient_t ) * shmptr->server_max_clients;
48
49 }
50
51 void Sharedmemory_update(void)
52 {
53
54         uint64_t now;
55         unsigned int cc = 0;
56         client_t *client_itr = NULL;
57
58         memset( &shmptr->client[0], 0, shmptr->shmclient_size );
59         shmptr->clientcount = Client_count();
60
61         if( shmptr->clientcount )
62         {
63                 Timer_init( &now );
64                 while( Client_iterate(&client_itr) != NULL )
65                 {
66                         if( client_itr->authenticated )
67                         {
68                                 channel_t *channel = client_itr->channel;
69
70                                 char* clientAddressString = Util_clientAddressToString( client_itr );
71
72                                 strncpy( shmptr->client[cc].username, client_itr->username, 120 );
73                                 strncpy( shmptr->client[cc].ipaddress, clientAddressString, INET6_ADDRSTRLEN - 1 );
74                                 strncpy( shmptr->client[cc].channel, channel->name, 120 );
75
76                                 strncpy( shmptr->client[cc].os, client_itr->os, 120 );
77                                 strncpy( shmptr->client[cc].release, client_itr->release, 120 );
78                                 strncpy( shmptr->client[cc].os_version, client_itr->os_version, 120 );
79
80                                 shmptr->client[cc].tcp_port = Util_clientAddressToPortTCP( client_itr );
81                                 shmptr->client[cc].udp_port = Util_clientAddressToPortUDP( client_itr );
82
83                                 shmptr->client[cc].online_secs = ( now - client_itr->connectTime ) / 1000000LL;
84                                 shmptr->client[cc].idle_secs = ( now - client_itr->idleTime ) / 1000000LL;
85
86                                 shmptr->client[cc].bUDP  = client_itr->bUDP;
87                                 shmptr->client[cc].deaf  = client_itr->deaf;
88                                 shmptr->client[cc].mute  = client_itr->mute;
89                                 shmptr->client[cc].bOpus  = client_itr->bOpus;
90                                 shmptr->client[cc].self_deaf  = client_itr->self_deaf;
91                                 shmptr->client[cc].self_mute  = client_itr->self_mute;
92                                 shmptr->client[cc].recording  = client_itr->recording;
93                                 shmptr->client[cc].authenticated  = client_itr->authenticated;
94
95                                 shmptr->client[cc].availableBandwidth  = client_itr->availableBandwidth;
96
97                                 shmptr->client[cc].UDPPingAvg = client_itr->UDPPingAvg;
98                                 shmptr->client[cc].UDPPingVar = client_itr->UDPPingVar;
99                                 shmptr->client[cc].TCPPingAvg = client_itr->TCPPingAvg;
100                                 shmptr->client[cc].TCPPingVar = client_itr->TCPPingVar;
101
102                                 shmptr->client[cc].isAdmin = client_itr->isAdmin;
103                                 shmptr->client[cc].isSuppressed = client_itr->isSuppressed;
104
105                                 shmptr->client[cc].UDPPackets = client_itr->UDPPackets;
106                                 shmptr->client[cc].TCPPackets = client_itr->TCPPackets;
107
108                                 free(clientAddressString);
109                         }
110                         cc++;
111                 }
112         }
113 }
114 void Sharedmemory_alivetick(void)
115 {
116         shmptr->alive++;
117 }
118
119 void Sharedmemory_deinit(void)
120 {
121         close( shm_fd );
122         shm_unlink( shm_file_name );
123         shmptr->umurmurd_pid = 0;
124 }