Project

General

Profile

Revision cc7646f9

IDcc7646f9d14d13acbf68e7ae50ec2e645885c4d7
Parent 5e03b78d
Child 4f824e14

Added by Thomas Mullins almost 11 years ago

Major changes to mainbox code.

-Added local cache for RFID -> tool permissions
-Queries new page /crm/roboauth/%08x, which gives bitmask of all tool
permissions instead of just one
-Added pid file at /var/run/tooltron.pid
-Now must be run as "tooltron run" and also has "tooltron refresh" and
"tooltron clear" which send signals to the pid in the pid file
-Other things I'm forgetting

View differences:

mainbox/util.c
2 2
#include "log.h"
3 3
#include <stdlib.h>
4 4
#include <stdio.h>
5
#include <errno.h>
5 6
#include <unistd.h>
6 7
#include <sys/types.h>
7 8
#include <sys/stat.h>
8 9
#include <fcntl.h>
9 10

  
11
/*
12
 * read_file
13
 *
14
 * Reads in an entire file. Returns NULL on error, or a malloc'd pointer to a
15
 * string which should later be freed.
16
 */
10 17
char *read_file(const char *filename) {
11 18
  int fd, len, size, nread;
12 19
  char *str;
......
46 53
    }
47 54
  }
48 55
}
56

  
57
/*
58
 * create_pid_file
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.
63
 */
64
int create_pid_file() {
65
  int fd;
66
  FILE *file;
67
  
68
  fd = open("/var/run/tooltron.pid", O_CREAT | O_EXCL | O_WRONLY, 0644);
69
  if (fd < 0) {
70
    if (errno == EEXIST)
71
      fprintf(stderr, "ERROR: tooltron is already running, or the pidfile "
72
          "/var/run/tooltron.pid is stale");
73
    else
74
      log_perror("open");
75
    return 1;
76
  }
77

  
78
  file = fdopen(fd, "w");
79
  if (!file) {
80
    log_perror("fdopen");
81
    return 1;
82
  }
83

  
84
  fprintf(file, "%d", getpid());
85

  
86
  fclose(file);
87
  return 0;
88
}
89

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

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

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

  
119
  if (fscanf(file, "%d", &pid) != 1) {
120
    perror("fscanf");
121
    fclose(file);
122
    return 0;
123
  }
124

  
125
  fclose(file);
126
  return pid;
127
}

Also available in: Unified diff