]> rtime.felk.cvut.cz Git - can-utils.git/blob - slcand.c
cangw: introduce uid command line option
[can-utils.git] / slcand.c
1 /*
2  * slcand.c - userspace daemon for serial line CAN interface driver SLCAN
3  *
4  * Copyright (c) 2009 Robert Haddon <robert.haddon@verari.com>
5  * Copyright (c) 2009 Verari Systems Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  * Send feedback to <linux-can@vger.kernel.org>
22  *
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/socket.h>
32 #include <fcntl.h>
33 #include <syslog.h>
34 #include <errno.h>
35 #include <pwd.h>
36 #include <signal.h>
37 #include <sys/ioctl.h>
38 #include <net/if.h>
39 #include <termios.h>
40 #include <linux/tty.h>
41
42 /* Change this to whatever your daemon is called */
43 #define DAEMON_NAME "slcand"
44
45 /* Change this to the user under which to run */
46 #define RUN_AS_USER "root"
47
48 /* The length of ttypath buffer */
49 #define TTYPATH_LENGTH  64
50
51 /* UART flow control types */
52 #define FLOW_NONE 0
53 #define FLOW_HW 1
54 #define FLOW_SW 2
55
56 void print_usage(char *prg)
57 {
58         fprintf(stderr, "\nUsage: %s [options] <tty> [canif-name]\n\n", prg);
59         fprintf(stderr, "Options: -o         (send open command 'O\\r')\n");
60         fprintf(stderr, "         -c         (send close command 'C\\r')\n");
61         fprintf(stderr, "         -f         (read status flags with 'F\\r' to reset error states)\n");
62         fprintf(stderr, "         -s <speed> (set CAN speed 0..8)\n");
63         fprintf(stderr, "         -S <speed> (set UART speed in baud)\n");
64         fprintf(stderr, "         -t <type>  (set UART flow control type 'hw' or 'sw')\n");
65         fprintf(stderr, "         -b <btr>   (set bit time register value)\n");
66         fprintf(stderr, "         -F         (stay in foreground; no daemonize)\n");
67         fprintf(stderr, "         -h         (show this help page)\n");
68         fprintf(stderr, "\nExamples:\n");
69         fprintf(stderr, "slcand -o -c -f -s6 ttyUSB0\n");
70         fprintf(stderr, "slcand -o -c -f -s6 ttyUSB0 can0\n");
71         fprintf(stderr, "slcand -o -c -f -s6 /dev/ttyUSB0\n");
72         fprintf(stderr, "\n");
73         exit(EXIT_FAILURE);
74 }
75
76 static int slcand_running;
77 static int exit_code;
78 static char ttypath[TTYPATH_LENGTH];
79
80 static void child_handler(int signum)
81 {
82         switch (signum) {
83
84         case SIGUSR1:
85                 /* exit parent */
86                 exit(EXIT_SUCCESS);
87                 break;
88         case SIGALRM:
89         case SIGCHLD:
90                 syslog(LOG_NOTICE, "received signal %i on %s", signum, ttypath);
91                 exit_code = EXIT_FAILURE;
92                 slcand_running = 0;
93                 break;
94         case SIGINT:
95         case SIGTERM:
96                 syslog(LOG_NOTICE, "received signal %i on %s", signum, ttypath);
97                 exit_code = EXIT_SUCCESS;
98                 slcand_running = 0;
99                 break;
100         }
101 }
102
103 static int look_up_uart_speed(long int s)
104 {
105         switch (s) {
106
107         case 9600:
108                 return B9600;
109         case 19200:
110                 return B19200;
111         case 38400:
112                 return B38400;
113         case 57600:
114                 return B57600;
115         case 115200:
116                 return B115200;
117         case 230400:
118                 return B230400;
119         case 460800:
120                 return B460800;
121         case 500000:
122                 return B500000;
123         case 576000:
124                 return B576000;
125         case 921600:
126                 return B921600;
127         case 1000000:
128                 return B1000000;
129         case 1152000:
130                 return B1152000;
131         case 1500000:
132                 return B1500000;
133         case 2000000:
134                 return B2000000;
135 #ifdef B2500000
136         case 2500000:
137                 return B2500000;
138 #endif
139 #ifdef B3000000
140         case 3000000:
141                 return B3000000;
142 #endif
143 #ifdef B3500000
144         case 3500000:
145                 return B3500000;
146 #endif
147 #ifdef B3710000
148         case 3710000
149                 return B3710000;
150 #endif
151 #ifdef B4000000
152         case 4000000:
153                 return B4000000;
154 #endif
155         default:
156                 return -1;
157         }
158 }
159
160 int main(int argc, char *argv[])
161 {
162         char *tty = NULL;
163         char const *devprefix = "/dev/";
164         char *name = NULL;
165         char buf[IFNAMSIZ+1];
166         struct termios tios;
167         speed_t old_ispeed;
168         speed_t old_ospeed;
169
170         int opt;
171         int send_open = 0;
172         int send_close = 0;
173         int send_read_status_flags = 0;
174         char *speed = NULL;
175         char *uart_speed_str = NULL;
176         long int uart_speed = 0;
177         int flow_type = FLOW_NONE;
178         char *btr = NULL;
179         int run_as_daemon = 1;
180         char *pch;
181         int ldisc = N_SLCAN;
182         int fd;
183
184         ttypath[0] = '\0';
185
186         while ((opt = getopt(argc, argv, "ocfs:S:t:b:?hF")) != -1) {
187                 switch (opt) {
188                 case 'o':
189                         send_open = 1;
190                         break;
191                 case 'c':
192                         send_close = 1;
193                         break;
194                 case 'f':
195                         send_read_status_flags = 1;
196                         break;
197                 case 's':
198                         speed = optarg;
199                         if (strlen(speed) > 1)
200                                 print_usage(argv[0]);
201                         break;
202                 case 'S':
203                         uart_speed_str = optarg;
204                         errno = 0;
205                         uart_speed = strtol(uart_speed_str, NULL, 10);
206                         if (errno)
207                                 print_usage(argv[0]);
208                         if (look_up_uart_speed(uart_speed) == -1) {
209                                 fprintf(stderr, "Unsupported UART speed (%lu)\n", uart_speed);
210                                 exit(EXIT_FAILURE);
211                         }
212                         break;
213                 case 't':
214                         if (!strcmp(optarg, "hw")) {
215                                 flow_type = FLOW_HW;
216                         } else if (!strcmp(optarg, "sw")) {
217                                 flow_type = FLOW_SW;
218                         } else {
219                                 fprintf(stderr, "Unsupported flow type (%s)\n", optarg);
220                                 exit(EXIT_FAILURE);
221                         }
222                         break;
223                 case 'b':
224                         btr = optarg;
225                         if (strlen(btr) > 6)
226                                 print_usage(argv[0]);
227                         break;
228                 case 'F':
229                         run_as_daemon = 0;
230                         break;
231                 case 'h':
232                 case '?':
233                 default:
234                         print_usage(argv[0]);
235                         break;
236                 }
237         }
238
239         /* Initialize the logging interface */
240         openlog(DAEMON_NAME, LOG_PID, LOG_LOCAL5);
241
242         /* Parse serial device name and optional can interface name */
243         tty = argv[optind];
244         if (NULL == tty)
245                 print_usage(argv[0]);
246
247         name = argv[optind + 1];
248
249         /* Prepare the tty device name string */
250         pch = strstr(tty, devprefix);
251         if (pch != tty)
252                 snprintf(ttypath, TTYPATH_LENGTH, "%s%s", devprefix, tty);
253         else
254                 snprintf(ttypath, TTYPATH_LENGTH, "%s", tty);
255
256         syslog(LOG_INFO, "starting on TTY device %s", ttypath);
257
258         /* Daemonize */
259         if (run_as_daemon) {
260                 if (daemon(0, 0)) {
261                         syslog(LOG_ERR, "failed to daemonize");
262                         exit(EXIT_FAILURE);
263                 }
264         }
265         else {
266                 /* Trap signals that we expect to receive */
267                 signal(SIGINT, child_handler);
268                 signal(SIGTERM, child_handler);
269         }
270
271         /* */
272         slcand_running = 1;
273
274         /* Now we are a daemon -- do the work for which we were paid */
275         fd = open(ttypath, O_RDWR | O_NONBLOCK | O_NOCTTY);
276         if (fd < 0) {
277                 syslog(LOG_NOTICE, "failed to open TTY device %s\n", ttypath);
278                 perror(ttypath);
279                 exit(EXIT_FAILURE);
280         }
281
282         /* Configure baud rate */
283         memset(&tios, 0, sizeof(struct termios));
284         if (tcgetattr(fd, &tios) < 0) {
285                 syslog(LOG_NOTICE, "failed to get attributes for TTY device %s: %s\n", ttypath, strerror(errno));
286                 exit(EXIT_FAILURE);
287         }
288
289         /* Get old values for later restore */
290         old_ispeed = cfgetispeed(&tios);
291         old_ospeed = cfgetospeed(&tios);
292
293         /* Reset UART settings */
294         cfmakeraw(&tios);
295         tios.c_iflag &= ~IXOFF;
296         tios.c_cflag &= ~CRTSCTS;
297
298         /* Baud Rate */
299         cfsetispeed(&tios, look_up_uart_speed(uart_speed));
300         cfsetospeed(&tios, look_up_uart_speed(uart_speed));
301
302         /* Flow control */
303         if (flow_type == FLOW_HW)
304                 tios.c_cflag |= CRTSCTS;
305         else if (flow_type == FLOW_SW)
306                 tios.c_iflag |= (IXON | IXOFF);
307
308         /* apply changes */
309         if (tcsetattr(fd, TCSADRAIN, &tios) < 0)
310                 syslog(LOG_NOTICE, "Cannot set attributes for device \"%s\": %s!\n", ttypath, strerror(errno));
311
312         if (speed) {
313                 sprintf(buf, "C\rS%s\r", speed);
314                 write(fd, buf, strlen(buf));
315         }
316
317         if (btr) {
318                 sprintf(buf, "C\rs%s\r", btr);
319                 write(fd, buf, strlen(buf));
320         }
321
322         if (send_read_status_flags) {
323                 sprintf(buf, "F\r");
324                 write(fd, buf, strlen(buf));
325         }
326
327         if (send_open) {
328                 sprintf(buf, "O\r");
329                 write(fd, buf, strlen(buf));
330         }
331
332         /* set slcan like discipline on given tty */
333         if (ioctl(fd, TIOCSETD, &ldisc) < 0) {
334                 perror("ioctl TIOCSETD");
335                 exit(EXIT_FAILURE);
336         }
337         
338         /* retrieve the name of the created CAN netdevice */
339         if (ioctl(fd, SIOCGIFNAME, buf) < 0) {
340                 perror("ioctl SIOCGIFNAME");
341                 exit(EXIT_FAILURE);
342         }
343
344         syslog(LOG_NOTICE, "attached TTY %s to netdevice %s\n", ttypath, buf);
345         
346         /* try to rename the created netdevice */
347         if (name) {
348                 struct ifreq ifr;
349                 int s = socket(PF_INET, SOCK_DGRAM, 0);
350
351                 if (s < 0)
352                         perror("socket for interface rename");
353                 else {
354                         strncpy(ifr.ifr_name, buf, IFNAMSIZ);
355                         strncpy(ifr.ifr_newname, name, IFNAMSIZ);
356
357                         if (ioctl(s, SIOCSIFNAME, &ifr) < 0) {
358                                 syslog(LOG_NOTICE, "netdevice %s rename to %s failed\n", buf, name);
359                                 perror("ioctl SIOCSIFNAME rename");
360                                 exit(EXIT_FAILURE);
361                         } else
362                                 syslog(LOG_NOTICE, "netdevice %s renamed to %s\n", buf, name);
363
364                         close(s);
365                 }       
366         }
367
368         /* The Big Loop */
369         while (slcand_running)
370                 sleep(1); /* wait 1 second */
371
372         /* Reset line discipline */
373         syslog(LOG_INFO, "stopping on TTY device %s", ttypath);
374         ldisc = N_TTY;
375         if (ioctl(fd, TIOCSETD, &ldisc) < 0) {
376                 perror("ioctl TIOCSETD");
377                 exit(EXIT_FAILURE);
378         }
379
380         if (send_close) {
381                 sprintf(buf, "C\r");
382                 write(fd, buf, strlen(buf));
383         }
384
385         /* Reset old rates */
386         cfsetispeed(&tios, old_ispeed);
387         cfsetospeed(&tios, old_ospeed);
388
389         /* apply changes */
390         if (tcsetattr(fd, TCSADRAIN, &tios) < 0)
391                 syslog(LOG_NOTICE, "Cannot set attributes for device \"%s\": %s!\n", ttypath, strerror(errno));
392
393         /* Finish up */
394         syslog(LOG_NOTICE, "terminated on %s", ttypath);
395         closelog();
396         return exit_code;
397 }