1999-12-09 23:15:14 +00:00
|
|
|
/*
|
2001-12-27 23:30:36 +00:00
|
|
|
* clients.c
|
|
|
|
|
* This file is part of LCDd, the lcdproc server.
|
|
|
|
|
*
|
|
|
|
|
* This file is released under the GNU General Public License. Refer to the
|
|
|
|
|
* COPYING file distributed with this package.
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 1999, William Ferrell, Scott Scriven
|
2002-04-26 00:00:31 +00:00
|
|
|
* 2002, Joris Robijn
|
2001-12-27 23:30:36 +00:00
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* Inits/shuts down client system,
|
|
|
|
|
* and searches for clients in the list.
|
2002-04-26 00:00:31 +00:00
|
|
|
* On short: manages the list of clients that are connected.
|
2001-12-27 23:30:36 +00:00
|
|
|
*
|
1999-12-09 23:15:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2002-04-26 00:00:31 +00:00
|
|
|
#include "client.h"
|
1999-12-09 23:15:14 +00:00
|
|
|
#include "clients.h"
|
2001-11-29 14:09:13 +00:00
|
|
|
#include "shared/report.h"
|
2001-11-14 13:57:25 +00:00
|
|
|
#include "render.h"
|
1999-12-09 23:15:14 +00:00
|
|
|
|
2002-08-04 20:58:51 +00:00
|
|
|
LinkedList *clientlist = NULL;
|
1999-12-09 23:15:14 +00:00
|
|
|
|
2001-12-30 00:15:25 +00:00
|
|
|
/* Initialize and kill client list...*/
|
2000-03-30 20:20:41 +00:00
|
|
|
int
|
2007-04-02 21:29:53 +00:00
|
|
|
clients_init(void)
|
1999-12-09 23:15:14 +00:00
|
|
|
{
|
2002-07-14 20:30:33 +00:00
|
|
|
debug(RPT_DEBUG, "%s()", __FUNCTION__);
|
2000-03-30 20:20:41 +00:00
|
|
|
|
2007-04-02 21:29:53 +00:00
|
|
|
clientlist = LL_new();
|
2002-04-26 00:00:31 +00:00
|
|
|
if (!clientlist) {
|
2007-04-02 21:29:53 +00:00
|
|
|
report(RPT_ERR, "%s: Unable to create client list", __FUNCTION__);
|
2000-04-03 22:13:57 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
1999-12-09 23:15:14 +00:00
|
|
|
|
2000-04-03 22:13:57 +00:00
|
|
|
return 0;
|
1999-12-09 23:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
2000-03-30 20:20:41 +00:00
|
|
|
int
|
2007-04-02 21:29:53 +00:00
|
|
|
clients_shutdown(void)
|
1999-12-09 23:15:14 +00:00
|
|
|
{
|
2002-04-26 00:00:31 +00:00
|
|
|
Client *c;
|
1999-12-09 23:15:14 +00:00
|
|
|
|
2007-04-02 21:29:53 +00:00
|
|
|
debug(RPT_DEBUG, "%s()", __FUNCTION__);
|
2000-03-30 20:20:41 +00:00
|
|
|
|
2007-04-02 21:29:53 +00:00
|
|
|
if (!clientlist) {
|
2002-08-04 20:58:51 +00:00
|
|
|
/* Program shutdown before completed startup */
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-26 00:00:31 +00:00
|
|
|
/* Free all client structures... */
|
2007-04-02 21:29:53 +00:00
|
|
|
for (c = LL_GetFirst(clientlist); c; c = LL_GetNext(clientlist)) {
|
|
|
|
|
debug(RPT_DEBUG, "%s: ...", __FUNCTION__);
|
2000-04-03 22:13:57 +00:00
|
|
|
if (c) {
|
2007-04-02 21:29:53 +00:00
|
|
|
debug(RPT_DEBUG, "%s: ... %i ...", __FUNCTION__, c->sock);
|
|
|
|
|
if (client_destroy(c) != 0) {
|
|
|
|
|
report(RPT_ERR, "%s: Error freeing client", __FUNCTION__);
|
2000-04-03 22:13:57 +00:00
|
|
|
} else {
|
2007-04-02 21:29:53 +00:00
|
|
|
debug(RPT_DEBUG, "%s: Freed client...", __FUNCTION__);
|
2000-04-03 22:13:57 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2007-04-02 21:29:53 +00:00
|
|
|
debug(RPT_DEBUG, "%s: No client!", __FUNCTION__);
|
2000-04-03 22:13:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
1999-12-09 23:15:14 +00:00
|
|
|
|
2001-12-30 00:15:25 +00:00
|
|
|
/* Then, free the list...*/
|
2007-04-02 21:29:53 +00:00
|
|
|
LL_Destroy(clientlist);
|
2000-03-30 20:20:41 +00:00
|
|
|
|
2007-04-02 21:29:53 +00:00
|
|
|
debug(RPT_DEBUG, "%s: done", __FUNCTION__);
|
1999-12-09 23:15:14 +00:00
|
|
|
|
2000-04-03 22:13:57 +00:00
|
|
|
return 0;
|
1999-12-09 23:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
2002-04-26 00:00:31 +00:00
|
|
|
int
|
2007-04-02 21:29:53 +00:00
|
|
|
clients_add_client(Client *c)
|
2002-04-26 00:00:31 +00:00
|
|
|
{
|
|
|
|
|
/* Add the client to the clients list... */
|
2007-04-02 21:29:53 +00:00
|
|
|
return LL_Push(clientlist, c);
|
2002-04-26 00:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2007-04-02 21:29:53 +00:00
|
|
|
clients_remove_client(Client *c)
|
2002-04-26 00:00:31 +00:00
|
|
|
{
|
|
|
|
|
/* Remove the client from the clients list... */
|
2007-04-02 21:29:53 +00:00
|
|
|
return(LL_Remove(clientlist, c) == NULL)?-1:0;
|
2002-04-26 00:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Client *
|
2007-04-02 21:29:53 +00:00
|
|
|
clients_getfirst(void)
|
2002-04-26 00:00:31 +00:00
|
|
|
{
|
|
|
|
|
return (Client *) LL_GetFirst(clientlist);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Client *
|
2007-04-02 21:29:53 +00:00
|
|
|
clients_getnext(void)
|
2002-04-26 00:00:31 +00:00
|
|
|
{
|
|
|
|
|
return (Client *) LL_GetNext(clientlist);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2007-04-02 21:29:53 +00:00
|
|
|
clients_client_count(void)
|
2002-04-26 00:00:31 +00:00
|
|
|
{
|
|
|
|
|
return LL_Length(clientlist);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-12-30 00:15:25 +00:00
|
|
|
/* A client is identified by the file descriptor
|
2002-04-26 00:00:31 +00:00
|
|
|
* associated with it. Find one.
|
2001-12-30 00:15:25 +00:00
|
|
|
*/
|
2000-03-30 20:20:41 +00:00
|
|
|
|
2002-04-26 00:00:31 +00:00
|
|
|
Client *
|
2007-04-02 21:29:53 +00:00
|
|
|
clients_find_client_by_sock(int sock)
|
1999-12-09 23:15:14 +00:00
|
|
|
{
|
2002-04-26 00:00:31 +00:00
|
|
|
Client *c;
|
1999-12-09 23:15:14 +00:00
|
|
|
|
2007-04-02 21:29:53 +00:00
|
|
|
debug(RPT_DEBUG, "%s(sock=%i)", __FUNCTION__, sock);
|
1999-12-09 23:15:14 +00:00
|
|
|
|
2007-04-02 21:29:53 +00:00
|
|
|
for (c = LL_GetFirst(clientlist); c; c = LL_GetNext(clientlist)) {
|
2000-04-03 22:13:57 +00:00
|
|
|
if (c->sock == sock) {
|
|
|
|
|
return c;
|
|
|
|
|
}
|
2002-04-26 00:00:31 +00:00
|
|
|
}
|
2000-03-30 20:20:41 +00:00
|
|
|
|
2007-04-02 21:29:53 +00:00
|
|
|
debug(RPT_ERR, "%s: failed", __FUNCTION__);
|
1999-12-09 23:15:14 +00:00
|
|
|
|
2000-04-03 22:13:57 +00:00
|
|
|
return NULL;
|
1999-12-09 23:15:14 +00:00
|
|
|
}
|