This commit was generated by cvs2svn to compensate for changes in r158,
[gnokii.git] / utils / mgnokiidev.c
1 /*
2
3   $Id$
4   
5   G N O K I I
6
7   A Linux/Unix toolset and driver for Nokia mobile phones.
8
9   Copyright (C) 1999, 2000 Hugh Blemings & Pavel Janík ml.
10
11   Released under the terms of the GNU GPL, see file COPYING for more details.
12
13   Mgnokiidev gets passed a slave pty name by gnokiid and uses this
14   information to create a symlink from the pty to /dev/gnokii.
15
16   $Log$
17   Revision 1.1.1.3  2002/04/03 00:08:23  short
18   Found in "gnokii-working" directory, some November-patches version
19
20   Revision 1.7  2001/09/14 12:38:00  pkot
21   More cleanups
22
23   Revision 1.6  2000/12/27 10:54:15  pkot
24   Added Unix98 PTYs support (Michael Mráka).
25
26   
27 */
28
29 #include <stdio.h>
30 #include <errno.h>
31 #include <signal.h>
32 #include <termios.h>
33 #include <grp.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/time.h>
39 #include <unistd.h>
40 #include <ctype.h>
41
42 #define DEVLEN  30
43 #define MAXLEN  12
44
45 int main(int argc, char *argv[])
46 {
47   int count, err, aux;
48   char dev_name[DEVLEN];
49
50   /* Check we have one and only one command line argument. */
51   if (argc != 2) {
52     fprintf(stderr, "mgnokiidev takes one and only one argument!\n");
53     exit(-2);
54   }
55
56   /* Check if argument has a reasonable length (less than MAXLEN characters) */
57   if (strlen(argv[1]) >= MAXLEN) {
58     fprintf(stderr, "Argument must be less than %d characters.\n", MAXLEN);
59     exit (-2);
60   }
61
62   strncpy(dev_name, argv[1], DEVLEN);
63
64   /* Check for suspicious characters. */
65   aux = strlen(dev_name);
66   for (count = 0; count < aux; count ++)
67     if (!(isalnum(dev_name[count]) || dev_name[count]=='/')) {
68       fprintf(stderr, "Suspicious character at index %d in argument.\n", count);
69       exit (-2);
70     }
71
72   /* Now become root */
73   setuid(0);
74
75   /* Change group of slave pty to group of mgnokiidev */
76   err = chown(dev_name, -1, getgid());
77
78   if (err < 0) {
79     perror("mgnokiidev - chown: ");
80     exit (-2);
81   }
82
83   /* Change permissions to rw by group */
84   err = chmod(dev_name, S_IRGRP | S_IWGRP | S_IRUSR | S_IWUSR);
85         
86   if (err < 0) {
87     perror("mgnokiidev - chmod: ");
88     exit (-2);
89   }
90
91   /* FIXME: Possible bug - should check that /dev/gnokii doesn't already exist
92      in case multiple users are trying to run gnokii. Well, but will be
93      mgnokiidev called then? I do not think so - you will probably got the
94      message serialport in use or similar. Don't you. I haven't tested it
95      though. */
96
97   /* Remove symlink in case it already exists. Don't care if it fails.  */
98   unlink ("/dev/gnokii");
99
100   /* Create symlink */
101   err = symlink(dev_name, "/dev/gnokii");
102
103   if (err < 0) {
104     perror("mgnokiidev - symlink: ");
105     exit (-2);
106   }
107
108   /* Done */
109   exit (0);
110 }