GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
lib/driver/main.c
Go to the documentation of this file.
1 
2 /****************************************************************************
3  *
4  * MODULE: driver
5  * AUTHOR(S): Glynn Clements <glynn gclements.plus.com>(original contributor)
6  * Jachym Cepicky <jachym les-ejk.cz>
7  * PURPOSE: graphics monitor driver
8  * COPYRIGHT: (C) 2006-2007 by the GRASS Development Team
9  *
10  * This program is free software under the GNU General Public
11  * License (>=v2). Read the file COPYING that comes with GRASS
12  * for details.
13  *
14  *****************************************************************************/
15 #include <stdio.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <signal.h>
19 #include <setjmp.h>
20 #include <unistd.h>
21 
22 #include <grass/gis.h>
23 #include <grass/glocale.h>
24 #include "driverlib.h"
25 #include "driver.h"
26 #include "pad.h"
27 
28 static jmp_buf save;
29 
30 static RETSIGTYPE handle_sigpipe(int sig)
31 {
32  longjmp(save, 1);
33 }
34 
35 static RETSIGTYPE handle_sigterm(int sig)
36 {
38 }
39 
40 int LIB_main(int argc, char **argv)
41 {
42  char *me;
43  int _wfd;
44  int _rfd;
45  int eof;
46 
47  char c;
48  pid_t pid;
49  int foreground;
50  int listenfd;
51 
52 #ifndef __MINGW32__
53  struct sigaction sigact;
54 #endif
55 
56  /* The calling syntax is as follows:
57  monitor_name [-] ["input_fifo output_fifo"]
58 
59  The "-", if present, causes the monitor to run in foreground.
60  Otherwise, once it has been determined that the monitor is not
61  already running, we will fork and the parent will exit, so that
62  the monitor is left running in background.
63  */
64 
65  if (argc < 2) {
66  G_warning("Usage: %s <name> [-]", argv[0]);
67  return 1;
68  }
69 
70  /* whoami */
71  me = argv[1];
72 
73  foreground = (argc >= 3 && argv[2][0] == '-');
74 
75 #ifndef __MINGW32__
76 #ifdef SIGPIPE
77  sigact.sa_handler = handle_sigpipe;
78  sigemptyset(&sigact.sa_mask);
79  sigact.sa_flags = 0;
80  sigaction(SIGPIPE, &sigact, NULL);
81 #endif
82  sigact.sa_handler = handle_sigterm;
83  sigemptyset(&sigact.sa_mask);
84  sigact.sa_flags = 0;
85  sigaction(SIGTERM, &sigact, NULL);
86 #endif
87 
88  listenfd = prepare_connection_sock(me);
89 
90  G_message(_("Graphics driver [%s] started"), me);
91 
92 #ifndef __MINGW32__
93  if (!foreground) {
94  pid = fork();
95  if (pid) {
96  if (pid > 0) { /* parent exits */
97  exit(0);
98  }
99  else { /* weren't able to fork */
100 
101  G_fatal_error("Error - Could not fork to start [%s]", me);
102  exit(EXIT_FAILURE);
103  }
104  }
105  else {
106  /* change process groups to be shielded from keyboard signals */
107 #ifdef SETPGRP_VOID
108  setpgrp();
109 #else
110  setpgrp(0, getpid());
111 #endif
112  }
113  } /* monitor runs */
114 #endif
115 
116  while (1) { /* re-open upon EOF */
117  for (;;) {
118  if (get_connection_sock(listenfd, &_rfd, &_wfd, COM_Work_stream())
119  >= 0)
120  break;
121 
122  COM_Do_work(0);
123  }
124 
125  command_init(_rfd, _wfd);
126 
127  COM_Client_Open();
128 
129  eof = 0;
130 
131  while (eof <= 0) {
132  COM_Do_work(1);
133 
134  if (setjmp(save)) {
135  G_warning("Monitor <%s>: Caught SIGPIPE", me);
136  break;
137  }
138 
139  if (get_command(&c) != 0)
140  break;
141 
142  if (process_command(c)) {
143  G_warning("Monitor <%s>: Premature EOF", me);
144  break;
145  }
146  }
147 
148  /* read encountered EOF. close socket now */
149  close(_wfd);
150  close(_rfd);
151  _rfd = _wfd = -1; /* Set to invalid file descriptor number */
152 
154  }
155 
156  return 0;
157 }
void command_init(int rfd, int wfd)
Definition: command.c:82
int COM_Work_stream(void)
Definition: Work.c:9
void COM_Client_Open(void)
Definition: driver/Client.c:4
void COM_Do_work(int)
Definition: Work.c:15
int process_command(int c)
Definition: command.c:111
void COM_Graph_close(void)
Definition: driver/Graph.c:11
void COM_Client_Close(void)
Definition: driver/Client.c:10
int get_connection_sock(int, int *, int *, int)
int prepare_connection_sock(const char *)
int get_command(char *c)
Definition: command.c:530
void G_message(const char *msg,...)
Print a message to stderr.
Definition: lib/gis/error.c:74
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int LIB_main(int argc, char **argv)
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.