Project

General

Profile

Revision 13fd9e7e

ID13fd9e7efe3c7d7ae6631d672e5a391535e59c95
Parent 4f824e14
Child ddd50354

Added by Thomas Mullins over 5 years ago

Added stop command and fixed pid file deletion

View differences:

mainbox/main.c
98 98
    usleep(SLEEP_MS * (useconds_t)1000);
99 99
    i = (i+1) % N_TOOLS;
100 100
  }
101
  log_print("Recieved SIGINT or SIGTERM, shutting down");
101 102

  
102 103
  log_print("Disabling tools");
103 104
  for (i = 0; i < N_TOOLS; i++) {
......
122 123
"          defaults to roboticsclub.org\n"
123 124
"       <cmd> can be any of the following:\n"
124 125
"          run     runs tooltron if it is not already running\n"
125
"          refresh signals an already running tooltron to refresh its cache\n"
126
"          clear   signals an already running tooltron to clear its cache\n";
126
"          stop    signals a running tooltron to shut down\n"
127
"          refresh signals a running tooltron to refresh its cache\n"
128
"          clear   signals a running tooltron to clear its cache\n";
127 129

  
128 130
int main(int argc, char **argv) {
129
  int opt;
131
  int opt, status;
130 132
  const char *device = "/dev/ttyUSB0";
131 133
  const char *server = "roboticsclub.org";
132 134

  
......
156 158
  }
157 159

  
158 160
  if (strcmp(argv[optind], "refresh") == 0) {
161

  
159 162
    send_signal(SIGUSR1);
160 163
    return 0;
164

  
161 165
  } else if (strcmp(argv[optind], "clear") == 0) {
166

  
162 167
    send_signal(SIGUSR2);
163 168
    return 0;
164
  } else if (strcmp(argv[optind], "run") != 0) {
165
    /* <cmd> is not "refresh", "clear", or "run", error */
166
    printf(usage, argv[0]);
167
    return 1;
168
  }
169 169

  
170
  if (create_pid_file())
171
    /* pid file already exists, error */
172
    return 1;
170
  } else if (strcmp(argv[optind], "stop") == 0) {
171

  
172
    send_signal(SIGTERM);
173
    return 0;
174

  
175
  } else if (strcmp(argv[optind], "run") == 0) {
176

  
177
    if (create_pid_file())
178
      /* pid file already exists, error */
179
      return 1;
180

  
181
    status = tooltron_main(device, server);
182

  
183
    remove_pid_file();
184
    return status;
185

  
186
  }
173 187

  
174
  return tooltron_main(device, server);
188
  printf("Unknown command \"%s\"\n", argv[optind]);
189
  printf(usage, argv[0]);
190
  return 1;
175 191
}
mainbox/util.c
20 20
  
21 21
  fd = open(filename, O_RDONLY);
22 22
  if (fd < 0) {
23
    log_perror("open");
23
    log_perror(filename);
24 24
    return NULL;
25 25
  }
26 26

  
......
57 57
/*
58 58
 * create_pid_file
59 59
 *
60
 * Creates /var/run/tooltron.pid containing the PID of the current process.
61
 * Returns 0 if successful, or nonzero if there is an error or it already
62
 * exists.
60
 * Creates PIDFILE containing the PID of the current process.  Returns 0 if
61
 * successful, or nonzero if there is an error or it already exists.
63 62
 */
64 63
int create_pid_file() {
65 64
  int fd;
66 65
  FILE *file;
67 66
  
68
  fd = open("/var/run/tooltron.pid", O_CREAT | O_EXCL | O_WRONLY, 0644);
67
  fd = open(PIDFILE, O_CREAT | O_EXCL | O_WRONLY, 0644);
69 68
  if (fd < 0) {
70 69
    if (errno == EEXIST)
71 70
      fprintf(stderr, "ERROR: tooltron is already running, or the pidfile "
72
          "/var/run/tooltron.pid is stale");
71
          PIDFILE "is stale");
73 72
    else
74
      log_perror("open");
73
      log_perror(PIDFILE);
75 74
    return 1;
76 75
  }
77 76

  
......
90 89
/*
91 90
 * remove_pid_file
92 91
 *
93
 * Removes /var/run/tooltron.pid.
92
 * Removes PIDFILE.
94 93
 */
95 94
void remove_pid_file() {
96
  if (unlink("/var/run/tooltron.pid"))
97
    log_perror("unlink");
95
  if (unlink(PIDFILE))
96
    log_perror(PIDFILE);
98 97
}
99 98

  
100 99
/*
101 100
 * read_pid_file
102 101
 *
103
 * Returns the integer found in /var/run/tooltron.pid, or 0 if there was an
102
 * Returns the integer found in PIDFILE, or 0 if there was an
104 103
 * error.
105 104
 */
106 105
pid_t read_pid_file() {
107 106
  FILE *file;
108 107
  int pid;
109 108

  
110
  file = fopen("/var/run/tooltron.pid", "r");
109
  file = fopen(PIDFILE, "r");
111 110
  if (!file) {
112 111
    if (errno == ENOENT)
113 112
      fprintf(stderr, "ERROR: tooltron does not appear to be running\n");
114 113
    else
115
      perror("fopen");
114
      perror(PIDFILE);
116 115
    return 0;
117 116
  }
118 117

  
mainbox/util.h
3 3

  
4 4
#include <unistd.h>
5 5

  
6
#define PIDFILE "/var/run/tooltron.pid"
7

  
6 8
char *read_file(const char *filename);
7 9
int create_pid_file();
8 10
pid_t read_pid_file();

Also available in: Unified diff