Main Page | Class List | File List | Class Members | File Members

acpi_listen.c File Reference

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <getopt.h>
#include <time.h>
#include <sys/poll.h>
#include <grp.h>
#include "acpid.h"
#include "ud_socket.h"

Go to the source code of this file.

Defines

#define MAX_BUFLEN   1024

Functions

static int handle_cmdline (int *argc, char ***argv)
static char * read_line (int fd)
static void time_expired (int signum)
int main (int argc, char **argv)
static void usage (FILE *fp)

Variables

static const char * progname
static const char * socketfile = ACPI_SOCKETFILE
static int max_events
static struct option opts []
static const char * opts_help []


Define Documentation

#define MAX_BUFLEN   1024
 

Definition at line 199 of file acpi_listen.c.

Referenced by read_line().


Function Documentation

static int handle_cmdline int *  argc,
char ***  argv
[static]
 

Definition at line 154 of file acpi_listen.c.

References max_events, opts, PACKAGE, socketfile, and usage().

Referenced by main().

00155 {
00156         for (;;) {
00157                 int i;
00158                 i = getopt_long(*argc, *argv, "c:s:t:vh", opts, NULL);
00159                 if (i == -1) {
00160                         break;
00161                 }
00162                 switch (i) {
00163                 case 'c':
00164                         if (!isdigit(optarg[0])) {
00165                                 usage(stderr);
00166                                 exit(EXIT_FAILURE);
00167                         }
00168                         max_events = atoi(optarg);
00169                         break;
00170                 case 's':
00171                         socketfile = optarg;
00172                         break;
00173                 case 't':
00174                         if (!isdigit(optarg[0])) {
00175                                 usage(stderr);
00176                                 exit(EXIT_FAILURE);
00177                         }
00178                         alarm(atoi(optarg));
00179                         break;
00180                 case 'v':
00181                         printf(PACKAGE "-" VERSION "\n");
00182                         exit(EXIT_SUCCESS);
00183                 case 'h':
00184                         usage(stdout);
00185                         exit(EXIT_SUCCESS);
00186                 default:
00187                         usage(stderr);
00188                         exit(EXIT_FAILURE);
00189                         break;
00190                 }
00191         }
00192 
00193         *argc -= optind;
00194         *argv += optind;
00195 
00196         return 0;
00197 }

int main int  argc,
char **  argv
 

Definition at line 55 of file acpi_listen.c.

References ACPI_MAX_ERRS, handle_cmdline(), max_events, progname, read_line(), socketfile, time_expired(), and ud_connect().

00056 {
00057         int sock_fd;
00058         int ret;
00059 
00060         /* handle an alarm */
00061         signal(SIGALRM, time_expired);
00062 
00063         /* learn who we really are */
00064         progname = (const char *)strrchr(argv[0], '/');
00065         progname = progname ? (progname + 1) : argv[0];
00066 
00067         /* handle the commandline  */
00068         handle_cmdline(&argc, &argv);
00069 
00070         /* open the socket */
00071         sock_fd = ud_connect(socketfile);
00072         if (sock_fd < 0) {
00073                 fprintf(stderr, "%s: can't open socket %s: %s\n",
00074                         progname, socketfile, strerror(errno));
00075                 exit(EXIT_FAILURE);
00076         }
00077         fcntl(sock_fd, F_SETFD, FD_CLOEXEC);
00078 
00079         /* set stdout to be line buffered */
00080         setvbuf(stdout, NULL, _IOLBF, 0);
00081 
00082         /* main loop */
00083         ret = 0;
00084         while (1) {
00085                 char *event;
00086 
00087                 /* read and handle an event */
00088                 event = read_line(sock_fd);
00089                 if (event) {
00090                         fprintf(stdout, "%s\n", event);
00091                 } else if (errno == EPIPE) {
00092                         fprintf(stderr, "connection closed\n");
00093                         break;
00094                 } else {
00095                         static int nerrs;
00096                         if (++nerrs >= ACPI_MAX_ERRS) {
00097                                 fprintf(stderr, "too many errors - aborting\n");
00098                                 ret = 1;
00099                                 break;
00100                         }
00101                 }
00102 
00103                 if (max_events > 0 && --max_events == 0) {
00104                         break;
00105                 }
00106         }
00107 
00108         return ret;
00109 }

static char * read_line int  fd  )  [static]
 

Definition at line 201 of file acpi_listen.c.

References MAX_BUFLEN.

Referenced by main().

00202 {
00203         static char *buf;
00204         int buflen = 64;
00205         int i = 0;
00206         int r;
00207         int searching = 1;
00208 
00209         while (searching) {
00210                 buf = realloc(buf, buflen);
00211                 if (!buf) {
00212                         fprintf(stderr, "ERR: malloc(%d): %s\n",
00213                                 buflen, strerror(errno));
00214                         return NULL;
00215                 }
00216                 memset(buf+i, 0, buflen-i);
00217 
00218                 while (i < buflen) {
00219                         r = read(fd, buf+i, 1);
00220                         if (r < 0 && errno != EINTR) {
00221                                 /* we should do something with the data */
00222                                 fprintf(stderr, "ERR: read(): %s\n",
00223                                         strerror(errno));
00224                                 return NULL;
00225                         } else if (r == 0) {
00226                                 /* signal this in an almost standard way */
00227                                 errno = EPIPE;
00228                                 return NULL;
00229                         } else if (r == 1) {
00230                                 /* scan for a newline */
00231                                 if (buf[i] == '\n') {
00232                                         searching = 0;
00233                                         buf[i] = '\0';
00234                                         break;
00235                                 }
00236                                 i++;
00237                         }
00238                 }
00239                 if (buflen >= MAX_BUFLEN) {
00240                         break;
00241                 }
00242                 buflen *= 2;
00243         }
00244 
00245         return buf;
00246 }

static void time_expired int  signum  )  [static]
 

Definition at line 49 of file acpi_listen.c.

Referenced by main().

00050 {
00051         exit(EXIT_SUCCESS);
00052 }

static void usage FILE *  fp  )  [static]
 

Definition at line 128 of file acpi_listen.c.

References opts, opts_help, and progname.

Referenced by handle_cmdline().

00129 {
00130         struct option *opt;
00131         const char **hlp;
00132         int max, size;
00133 
00134         fprintf(fp, "Usage: %s [OPTIONS]\n", progname);
00135         max = 0;
00136         for (opt = opts; opt->name; opt++) {
00137                 size = strlen(opt->name);
00138                 if (size > max)
00139                         max = size;
00140         }
00141         for (opt = opts, hlp = opts_help; opt->name; opt++, hlp++) {
00142                 fprintf(fp, "  -%c, --%s", opt->val, opt->name);
00143                 size = strlen(opt->name);
00144                 for (; size < max; size++)
00145                         fprintf(fp, " ");
00146                 fprintf(fp, "  %s\n", *hlp);
00147         }
00148 }


Variable Documentation

int max_events [static]
 

Definition at line 46 of file acpi_listen.c.

Referenced by handle_cmdline(), and main().

struct option opts[] [static]
 

Initial value:

 {
        {"count", 0, 0, 'c'},
        {"socketfile", 1, 0, 's'},
        {"time", 0, 0, 't'},
        {"version", 0, 0, 'v'},
        {"help", 0, 0, 'h'},
        {NULL, 0, 0, 0},
}

Definition at line 111 of file acpi_listen.c.

Referenced by handle_cmdline(), and usage().

const char* opts_help[] [static]
 

Initial value:

 {
        "Set the maximum number of events.",    
        "Use the specified socket file.",       
        "Listen for the specified time (in seconds).",
        "Print version information.",           
        "Print this message.",                  
}

Definition at line 119 of file acpi_listen.c.

Referenced by handle_cmdline(), and usage().

const char* progname [static]
 

Definition at line 44 of file acpi_listen.c.

Referenced by daemonize(), handle_cmdline(), main(), open_logs(), and usage().

const char* socketfile = ACPI_SOCKETFILE [static]
 

Definition at line 45 of file acpi_listen.c.

Referenced by handle_cmdline(), and main().


© sourcejam.com 2005-2008