2 * Conky, a system monitor, based on torsmo
4 * This program is licensed under BSD license, read COPYING
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
22 static pthread_t daemon_thread;
23 static int daemon_status = 0;
26 /* okay, heres how it will basically work.
27 * when something connects, it will first send the conkyrc on the local (daemonized) server
28 * after this, it will simply continue to send all the buffered text to the remote client
29 * http://analyser.oli.tudelft.nl/beej/mirror/net/html/
30 * http://www.kegel.com/c10k.html
33 #define MYPORT 3490 // the port users will be connecting to
35 #define BACKLOG 10 // how many pending connections queue will hold
37 void sigchld_handler(/*int s*/)
39 while(wait(NULL) > 0);
48 int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
49 struct sockaddr_in my_addr; // my address information
50 struct sockaddr_in their_addr; // connector's address information
55 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
60 if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
65 my_addr.sin_family = AF_INET; // host byte order
66 my_addr.sin_port = htons(MYPORT); // short, network byte order
67 my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
68 memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
70 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))
76 if (listen(sockfd, BACKLOG) == -1) {
81 sa.sa_handler = sigchld_handler; // reap all dead processes
82 sigemptyset(&sa.sa_mask);
83 sa.sa_flags = SA_RESTART;
84 if (sigaction(SIGCHLD, &sa, NULL) == -1) {
89 while(1) { // main accept() loop
90 sin_size = sizeof(struct sockaddr_in);
91 if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr,
96 printf("server: got connection from %s\n",
97 inet_ntoa(their_addr.sin_addr));
98 if (!fork()) { // this is the child process
99 close(sockfd); // child doesn't need the listener
100 if (send(new_fd, data, 14, 0) == -1)
105 close(new_fd); // parent doesn't need this
111 void daemon_run(const char *s)
113 /* create thread, keep an eye on it */
116 if (!daemon_status) {
118 iret = pthread_create(&daemon_thread, NULL, daemon_loop, NULL);
119 } else if (daemon_status == 1) {
120 /* thread is still running, we'll just wait for it to finish for now */
121 pthread_join(daemon_thread, NULL);