]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - slcand.c
candump: new option to print extra message information
[sojka/can-utils.git] / slcand.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * slcand.c - userspace daemon for serial line CAN interface driver SLCAN
7  *
8  * Copyright (c) 2009 Robert Haddon <robert.haddon@verari.com>
9  * Copyright (c) 2009 Verari Systems Inc.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  *
25  * This code is derived from an example daemon code from
26  *
27  * http://en.wikipedia.org/wiki/Daemon_(computer_software)#Sample_Program_in_C_on_Linux
28  * (accessed 2009-05-05)
29  *
30  * So it is additionally licensed under the GNU Free Documentation License:
31  *
32  * Permission is granted to copy, distribute and/or modify this document
33  * under the terms of the GNU Free Documentation License, Version 1.2
34  * or any later version published by the Free Software Foundation;
35  * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
36  * A copy of the license is included in the section entitled "GNU
37  * Free Documentation License".
38  *
39  * Send feedback to <linux-can@vger.kernel.org>
40  *
41  */
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <syslog.h>
49 #include <errno.h>
50 #include <pwd.h>
51 #include <signal.h>
52 #include <sys/socket.h>
53 #include <sys/ioctl.h>
54 #include <sys/types.h>
55 #include <sys/stat.h>
56 #include <net/if.h>
57
58 /* default slcan line discipline since Kernel 2.6.25 */
59 #define LDISC_N_SLCAN 17
60
61 /* Change this to whatever your daemon is called */
62 #define DAEMON_NAME "slcand"
63
64 /* Change this to the user under which to run */
65 #define RUN_AS_USER "root"
66
67 /* The length of ttypath buffer */
68 #define TTYPATH_LENGTH  64
69
70 /* The length of pidfile name buffer */
71 #define PIDFILE_LENGTH  64
72
73 #define EXIT_SUCCESS 0
74 #define EXIT_FAILURE 1
75
76 void print_usage(char *prg)
77 {
78         fprintf(stderr, "\nUsage: %s tty [name]\n\n", prg);
79         fprintf(stderr, "Example:\n");
80         fprintf(stderr, "%s ttyUSB1\n", prg);
81         fprintf(stderr, "%s ttyS0 can0\n", prg);
82         fprintf(stderr, "\n");
83         exit(EXIT_FAILURE);
84 }
85
86 static void child_handler (int signum)
87 {
88         switch (signum)
89         {
90         case SIGALRM:
91                 exit (EXIT_FAILURE);
92                 break;
93         case SIGUSR1:
94                 exit (EXIT_SUCCESS);
95                 break;
96         case SIGCHLD:
97                 exit (EXIT_FAILURE);
98                 break;
99         }
100 }
101
102 static void daemonize (const char *lockfile, char *tty, char *name)
103 {
104         pid_t pid, sid, parent;
105         int lfp = -1;
106         FILE * pFile;
107         char const *pidprefix = "/var/run/";
108         char const *pidsuffix = ".pid";
109         char pidfile[PIDFILE_LENGTH];
110
111         snprintf(pidfile, PIDFILE_LENGTH, "%s%s-%s%s", pidprefix, DAEMON_NAME, tty, pidsuffix);
112
113         /* already a daemon */
114         if (getppid () == 1)
115                 return;
116
117         /* Create the lock file as the current user */
118         if (lockfile && lockfile[0])
119         {
120                 lfp = open (lockfile, O_RDWR | O_CREAT, 0640);
121                 if (lfp < 0)
122                 {
123                         syslog (LOG_ERR, "unable to create lock file %s, code=%d (%s)",
124                                 lockfile, errno, strerror (errno));
125                         exit (EXIT_FAILURE);
126                 }
127         }
128
129         /* Drop user if there is one, and we were run as root */
130         if (getuid () == 0 || geteuid () == 0)
131         {
132                 struct passwd *pw = getpwnam (RUN_AS_USER);
133                 if (pw)
134                 {
135                         //syslog (LOG_NOTICE, "setting user to " RUN_AS_USER);
136                         setuid (pw->pw_uid);
137                 }
138         }
139
140         /* Trap signals that we expect to receive */
141         signal (SIGCHLD, child_handler);
142         signal (SIGUSR1, child_handler);
143         signal (SIGALRM, child_handler);
144
145         /* Fork off the parent process */
146         pid = fork ();
147         if (pid < 0)
148         {
149                 syslog (LOG_ERR, "unable to fork daemon, code=%d (%s)",
150                         errno, strerror (errno));
151                 exit (EXIT_FAILURE);
152         }
153         /* If we got a good PID, then we can exit the parent process. */
154         if (pid > 0)
155         {
156
157                 /* Wait for confirmation from the child via SIGTERM or SIGCHLD, or
158                    for two seconds to elapse (SIGALRM).  pause() should not return. */
159                 alarm (2);
160                 pause ();
161
162                 exit (EXIT_FAILURE);
163         }
164
165         /* At this point we are executing as the child process */
166         parent = getppid ();
167
168         /* Cancel certain signals */
169         signal (SIGCHLD, SIG_DFL);      /* A child process dies */
170         signal (SIGTSTP, SIG_IGN);      /* Various TTY signals */
171         signal (SIGTTOU, SIG_IGN);
172         signal (SIGTTIN, SIG_IGN);
173         signal (SIGHUP, SIG_IGN);       /* Ignore hangup signal */
174         signal (SIGTERM, SIG_DFL);      /* Die on SIGTERM */
175
176         /* Change the file mode mask */
177         umask (0);
178
179         /* Create a new SID for the child process */
180         sid = setsid ();
181         if (sid < 0)
182         {
183                 syslog (LOG_ERR, "unable to create a new session, code %d (%s)",
184                         errno, strerror (errno));
185                 exit (EXIT_FAILURE);
186         }
187
188         pFile = fopen (pidfile,"w");
189         if (pFile < 0)
190         {
191                 syslog (LOG_ERR, "unable to create pid file %s, code=%d (%s)",
192                         pidfile, errno, strerror (errno));
193                 exit (EXIT_FAILURE);
194         }
195         fprintf (pFile, "%d\n", sid);
196         fclose (pFile);
197
198         /* Change the current working directory.  This prevents the current
199            directory from being locked; hence not being able to remove it. */
200         if ((chdir ("/")) < 0)
201         {
202                 syslog (LOG_ERR, "unable to change directory to %s, code %d (%s)",
203                         "/", errno, strerror (errno));
204                 exit (EXIT_FAILURE);
205         }
206
207         /* Redirect standard files to /dev/null */
208         freopen ("/dev/null", "r", stdin);
209         freopen ("/dev/null", "w", stdout);
210         freopen ("/dev/null", "w", stderr);
211
212         /* Tell the parent process that we are A-okay */
213         kill (parent, SIGUSR1);
214 }
215
216 int main (int argc, char *argv[])
217 {
218         char *tty = NULL;
219         char ttypath[TTYPATH_LENGTH];
220         char const *devprefix = "/dev/";
221         char *name = NULL;
222         char buf[IFNAMSIZ+1];
223
224         /* Initialize the logging interface */
225         openlog (DAEMON_NAME, LOG_PID, LOG_LOCAL5);
226
227         /* See how we're called */
228         if (argc == 2) {
229                 tty = argv[1];
230         } else if (argc == 3) {
231                 tty = argv[1];
232                 name = argv[2];
233                 if (strlen(name) > IFNAMSIZ-1)
234                         print_usage(argv[0]);
235         } else {
236                 print_usage(argv[0]);
237         }
238
239         /* Prepare the tty device name string */
240         char * pch;
241         pch = strstr (tty, devprefix);
242         if (pch == tty) {
243                 print_usage(argv[0]);
244         }
245
246         snprintf (ttypath, TTYPATH_LENGTH, "%s%s", devprefix, tty);
247         syslog (LOG_INFO, "starting on TTY device %s", ttypath);
248
249         /* Daemonize */
250         daemonize ("/var/lock/" DAEMON_NAME, tty, name);
251
252         /* Now we are a daemon -- do the work for which we were paid */
253         int fd;
254         int ldisc = LDISC_N_SLCAN;
255
256         if ((fd = open (ttypath, O_WRONLY | O_NOCTTY )) < 0) {
257             syslog (LOG_NOTICE, "failed to open TTY device %s\n", ttypath);
258                 perror(ttypath);
259                 exit(1);
260         }
261
262         /* set slcan like discipline on given tty */
263         if (ioctl (fd, TIOCSETD, &ldisc) < 0) {
264                 perror("ioctl TIOCSETD");
265                 exit(1);
266         }
267         
268         /* retrieve the name of the created CAN netdevice */
269         if (ioctl (fd, SIOCGIFNAME, buf) < 0) {
270                 perror("ioctl SIOCGIFNAME");
271                 exit(1);
272         }
273
274         syslog (LOG_NOTICE, "attached TTY %s to netdevice %s\n", ttypath, buf);
275         
276         /* try to rename the created netdevice */
277         if (name) {
278                 struct ifreq ifr;
279                 int s = socket(PF_INET, SOCK_DGRAM, 0);
280                 if (s < 0)
281                         perror("socket for interface rename");
282                 else {
283                         strncpy (ifr.ifr_name, buf, IFNAMSIZ);
284                         strncpy (ifr.ifr_newname, name, IFNAMSIZ);
285
286                         if (ioctl(s, SIOCSIFNAME, &ifr) < 0) {
287                                 syslog (LOG_NOTICE, "netdevice %s rename to %s failed\n", buf, name);
288                                 perror("ioctl SIOCSIFNAME rename");
289                 exit(1);
290                         } else
291                                 syslog (LOG_NOTICE, "netdevice %s renamed to %s\n", buf, name);
292
293                         close(s);
294                 }       
295         }
296
297         /* The Big Loop */
298         while (1) {
299                 sleep(60); /* wait 60 seconds */
300         }
301
302         /* Finish up */
303         syslog (LOG_NOTICE, "terminated on %s", ttypath);
304         closelog ();
305         return 0;
306 }