1 #include "sharedmemory.h"
5 char shm_file_name[128];
7 void Sharedmemory_init( int bindport, int bindport6 )
10 int server_max_clients = getIntConf(MAX_CLIENTS);
11 int shmtotal_size = sizeof( shm_t ) + (sizeof( shmclient_t ) * server_max_clients);
15 bindport = getIntConf(BINDPORT);
18 sprintf( shm_file_name, "/umurmurd:%i", bindport );
19 Log_info("SHM_API: shm_fd=\"%s\"", shm_file_name );
21 shm_fd = shm_open( shm_file_name, O_CREAT | O_RDWR, 0660 );
24 Log_fatal( "SHM_API: Open failed:%s\n", strerror(errno));
28 if( ftruncate( shm_fd, shmtotal_size ) == -1 )
30 Sharedmemory_deinit();
31 Log_fatal( "SHM_API: ftruncate : %s\n", strerror(errno));
35 shmptr = mmap( 0, shmtotal_size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0 );
36 if (shmptr == MAP_FAILED)
38 Log_fatal( "SHM_API: mmap failed : %s\n", strerror(errno));
42 memset( shmptr, 0, shmtotal_size );
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;
51 void Sharedmemory_update(void)
56 client_t *client_itr = NULL;
58 memset( &shmptr->client[0], 0, shmptr->shmclient_size );
59 shmptr->clientcount = Client_count();
61 if( shmptr->clientcount )
64 while( Client_iterate(&client_itr) != NULL )
66 if( client_itr->authenticated && !client_itr->shutdown_wait )
68 channel_t *channel = client_itr->channel;
70 char* clientAddressString = Util_clientAddressToString( client_itr );
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 );
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 );
80 strncpy( shmptr->client[cc].hash, client_itr->hash, 20 );
82 shmptr->client[cc].tcp_port = Util_clientAddressToPortTCP( client_itr );
83 shmptr->client[cc].udp_port = Util_clientAddressToPortUDP( client_itr );
85 shmptr->client[cc].online_secs = ( now - client_itr->connectTime ) / 1000000LL;
86 shmptr->client[cc].idle_secs = ( now - client_itr->idleTime ) / 1000000LL;
88 shmptr->client[cc].bUDP = client_itr->bUDP;
89 shmptr->client[cc].deaf = client_itr->deaf;
90 shmptr->client[cc].mute = client_itr->mute;
91 shmptr->client[cc].bOpus = client_itr->bOpus;
92 shmptr->client[cc].self_deaf = client_itr->self_deaf;
93 shmptr->client[cc].self_mute = client_itr->self_mute;
94 shmptr->client[cc].recording = client_itr->recording;
95 shmptr->client[cc].authenticated = client_itr->authenticated;
97 shmptr->client[cc].availableBandwidth = client_itr->availableBandwidth;
99 shmptr->client[cc].UDPPingAvg = client_itr->UDPPingAvg;
100 shmptr->client[cc].UDPPingVar = client_itr->UDPPingVar;
101 shmptr->client[cc].TCPPingAvg = client_itr->TCPPingAvg;
102 shmptr->client[cc].TCPPingVar = client_itr->TCPPingVar;
106 shmptr->client[cc].isAdmin = client_itr->isAdmin;
107 shmptr->client[cc].isSuppressed = client_itr->isSuppressed;
109 shmptr->client[cc].UDPPackets = client_itr->UDPPackets;
110 shmptr->client[cc].TCPPackets = client_itr->TCPPackets;
112 free(clientAddressString);
118 void Sharedmemory_alivetick(void)
123 void Sharedmemory_deinit(void)
126 shm_unlink( shm_file_name );
127 shmptr->umurmurd_pid = 0;