aboutsummaryrefslogtreecommitdiffstats
path: root/src/serial.c
blob: c8e2295190d42fa74955c9873bf50d413e298b08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * serial.c:
 *
 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
 * All rights reserved.
 *
 */

static char rcsid[] = "$Id$";

/*
 * $Log$
 * Revision 1.2  2008/02/14 16:21:17  james
 * *** empty log message ***
 *
 * Revision 1.1  2008/02/14 12:51:14  james
 * *** empty log message ***
 *
 * Revision 1.4  2008/02/14 10:39:14  james
 * *** empty log message ***
 *
 * Revision 1.3  2008/02/13 09:12:21  james
 * *** empty log message ***
 *
 * Revision 1.2  2008/02/12 22:36:46  james
 * *** empty log message ***
 *
 * Revision 1.1  2008/02/09 15:47:28  james
 * *** empty log message ***
 *
 * Revision 1.2  2008/02/07 11:11:14  staffcvs
 * *** empty log message ***
 *
 * Revision 1.1  2008/02/07 01:02:52  james
 * *** empty log message ***
 *
 * Revision 1.3  2008/02/06 17:53:28  james
 * *** empty log message ***
 *
 * Revision 1.2  2008/02/04 02:05:06  james
 * *** empty log message ***
 *
 * Revision 1.1  2008/02/04 01:32:39  james
 * *** empty log message ***
 *
 */

#include "project.h"
#include <pwd.h>
#include <dirent.h>
#include <sys/stat.h>

#define LOCK_ASCII
#undef LOCK_BINARY

#define NLOCKFILES	10

typedef struct {
char *lockfiles[NLOCKFILES];
char *potential_lockfiles[NLOCKFILES];
struct timeval last_content_check;
} Serial_lock;

typedef struct
{
  TTY_SIGNATURE;
  Serial_lock lock;
  int fd;
} Serial;



static int
chown_uucp(fd)
int fd;
{
    static int uuid = -1, ugid;
    struct passwd *pw;

    if (uuid <0) {
        if (pw = getpwnam("uucp")) {
            uuid = pw->pw_uid;
            ugid = pw->pw_gid;
        } else {
		return -1;
	}
    }
    return fchown(fd, uuid, ugid);
}

int make_lockfile(char *name)
{
char buf[1024],tmpfn[1024];
char *ptr;
int fd;
int i;

strcpy(tmpfn,name);

ptr=rindex(tmpfn,'/');
if (!ptr) return -1;

ptr++;

ptr+=sprintf(ptr,"LTMP.%d",getpid());
*ptr=0;

i=sprintf(buf,"%10d\n",getpid());

fd=open(tmpfn,O_WRONLY|O_CREAT|O_TRUNC,0444);
if (fd<0) {
	unlink(tmpfn);
	return -1;
}

write(fd,buf,i);
fchmod(fd,044);
if( chown_uucp(fd)) {
	close(fd);
	unlink(tmpfn);
	return -1;
}

close(fd);

if (link(tmpfn,name)<0) {
	unlink(tmpfn);
	return -1;
}

unlink(tmpfn);
return 0;
}



void construct_lock_file_name_by_name(char *ptr)
{

printf("lock by file %s\n",ptr);

}
void construct_lock_file_name_by_device(dev_t dev)
{

printf("lock by dev %x\n",dev);
}


#define DEV "/dev/"
int construct_possible_lock_files(char *device)
{
int nl;
struct dirent *de;
DIR *d;
struct stat dev_stat,ent_stat;
char buf[1024];


if (stat(device,&dev_stat)) return -1;
if (!S_ISCHR(dev_stat.st_mode)) return -1;

construct_lock_file_name_by_device(dev_stat.st_rdev);

construct_lock_file_name_by_name(device);

for (d=opendir(DEV);(de=readdir(d));)
{
strcpy(buf,DEV);
strcat(buf,de->d_name);

if (stat(buf,&ent_stat)) continue;
if (!S_ISCHR(ent_stat.st_mode)) continue;
if (ent_stat.st_rdev!=dev_stat.st_rdev) continue;

construct_lock_file_name_by_name(buf);

}
closedir(d);

}


static void 
serial_check_lock(Serial *t)
{
}


static void
serial_close (TTY * _t)
{
  Serial *t = (Serial *) _t;

  if (!t)
    return;

  close (t->fd);
  free (t);
}



static int
serial_read (TTY * _t, void *buf, int len)
{
  Serial *t = (Serial *) _t;
  int red, done = 0;

  serial_check_lock(t);
  if (t->blocked) return 0;

  do
    {

      red = wrap_read (t->fd, buf, len);
      if (red < 0)
        return -1;
      if (!red)
        return done;

      buf += red;
      len -= red;
      done += red;
    }
  while (len);


  return done;
}


static int
ptty_write (TTY * _t, void *buf, int len)
{
  int writ, done = 0;
  Serial *t = (Serial *) _t;

  serial_check_lock(t);
  if (t->blocked) return 0;

  do
    {

      writ = wrap_write (t->fd, buf, len);
      if (writ < 0)
        return -1;
      if (!writ)
        sleep (1);

      buf += writ;
      len -= writ;
      done += writ;
    }
  while (len);


  return done;
}

TTY *
serial_open (char *path)
{
  Serial *t;
  pid_t child;
  char name[1024];
  struct winsize winsize = { 0 };
  struct termios termios;
  int fd;


  default_termios (&termios);

  fd=open(path,O_RDWR);

  set_nonblocking (fd);

  t = (Serial *) malloc (sizeof (Serial));

  strncpy (t->name, path, sizeof (t->name));
  t->name[sizeof (t->name) - 1] = 0;

  t->recv = serial_read;
  //t->xmit = serial_write;
  t->close = serial_close;
  t->fd = fd;
  t->rfd = t->fd;
  t->wfd = t->fd;
  t->size.x = VT102_COLS;
  t->size.y = VT102_ROWS;
  t->blocked = 0;

  return (TTY *) t;
}