~ubuntu-branches/ubuntu/saucy/nut/saucy

« back to all changes in this revision

Viewing changes to drivers/serial.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2005-07-20 19:48:50 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050720194850-oo61wjr33rrx2mre
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* serial.c - common serial port functions for Network UPS Tools drivers
 
2
 
 
3
   Copyright (C) 2003  Russell Kroll <rkroll@exploits.org>
 
4
 
 
5
   This program is free software; you can redistribute it and/or modify
 
6
   it under the terms of the GNU General Public License as published by
 
7
   the Free Software Foundation; either version 2 of the License, or
 
8
   (at your option) any later version.
 
9
 
 
10
   This program is distributed in the hope that it will be useful,
 
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
   GNU General Public License for more details.
 
14
 
 
15
   You should have received a copy of the GNU General Public License
 
16
   along with this program; if not, write to the Free Software
 
17
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
*/
 
19
 
 
20
#include "common.h"
 
21
#include "timehead.h"
 
22
 
 
23
#include <grp.h>
 
24
#include <pwd.h>
 
25
#include <ctype.h>
 
26
#include <sys/file.h>
 
27
#include <sys/types.h>
 
28
#include <unistd.h>
 
29
#include <sys/termios.h>
 
30
 
 
31
#ifdef HAVE_UU_LOCK
 
32
#include <libutil.h>
 
33
#endif
 
34
 
 
35
#include "serial.h"
 
36
 
 
37
        extern  int     do_lock_port, exit_flag;
 
38
 
 
39
        static  unsigned int    comm_failures = 0;
 
40
 
 
41
static void ser_open_error(const char *port)
 
42
{
 
43
        struct  stat    fs;
 
44
        struct  passwd  *user;
 
45
        struct  group   *group;
 
46
 
 
47
        printf("\n");
 
48
 
 
49
        printf("Unable to open %s: %s\n\n", port, strerror(errno));
 
50
 
 
51
        /* check for bad port definitions first */
 
52
        if (stat(port, &fs)) {
 
53
                printf("Things to try:\n\n");
 
54
                printf(" - Check 'port=' in ups.conf\n\n");
 
55
                printf(" - Check owner/permissions of all parts of path\n\n");
 
56
                fatalx("Fatal error: unusable configuration");
 
57
        }
 
58
 
 
59
        user = getpwuid(getuid());
 
60
 
 
61
        if (user)
 
62
                printf("  Current user id: %s (%d)\n", 
 
63
                        user->pw_name, (int) user->pw_uid);
 
64
 
 
65
        user = getpwuid(fs.st_uid);
 
66
 
 
67
        if (user)
 
68
                printf("Serial port owner: %s (%d)\n",
 
69
                        user->pw_name, (int) fs.st_uid);
 
70
 
 
71
        group = getgrgid(fs.st_gid);
 
72
 
 
73
        if (group)
 
74
                printf("Serial port group: %s (%d)\n",
 
75
                        group->gr_name, (int) fs.st_gid);
 
76
 
 
77
        printf("     Mode of port: %04o\n\n", (int) fs.st_mode & 07777);
 
78
 
 
79
        printf("Things to try:\n\n");
 
80
        printf(" - Use another port (with the right permissions)\n\n");
 
81
        printf(" - Fix the port owner/group or permissions on this port\n\n");
 
82
        printf(" - Run this driver as another user (upsdrvctl -u or 'user=...' in ups.conf).\n");
 
83
        printf("   See upsdrvctl(8) and ups.conf(5).\n\n");
 
84
 
 
85
        fatalx("Fatal error: unusable configuration");
 
86
}
 
87
 
 
88
static void lock_set(int fd, const char *port)
 
89
{
 
90
        int     ret;
 
91
 
 
92
        if (fd < 0)
 
93
                fatal("lock_set: programming error: fd = %d", fd);
 
94
 
 
95
        if (do_lock_port == 0)
 
96
                return;
 
97
 
 
98
#ifdef HAVE_UU_LOCK
 
99
        ret = uu_lock(xbasename(port));
 
100
 
 
101
        if (ret != 0)
 
102
                fatalx("Can't uu_lock %s: %s", xbasename(port), 
 
103
                        uu_lockerr(ret));
 
104
 
 
105
        return;
 
106
 
 
107
#elif defined(HAVE_FLOCK)
 
108
 
 
109
        ret = flock(fd, LOCK_EX | LOCK_NB);
 
110
 
 
111
        if (ret != 0)
 
112
                fatalx("%s is locked by another process", port);
 
113
 
 
114
        return;
 
115
 
 
116
#elif defined(HAVE_LOCKF)
 
117
 
 
118
        lseek(fd, 0L, SEEK_SET);
 
119
 
 
120
        ret = lockf(fd, F_TLOCK, 0L);
 
121
 
 
122
        if (ret != 0)
 
123
                fatalx("%s is locked by another process", port);
 
124
 
 
125
        return;
 
126
 
 
127
#endif
 
128
 
 
129
        upslog(LOG_WARNING, "Warning: no locking method is available");
 
130
}
 
131
 
 
132
int ser_open(const char *port)
 
133
{
 
134
        int     fd;
 
135
 
 
136
        fd = open(port, O_RDWR | O_NOCTTY | O_EXCL | O_NONBLOCK);
 
137
 
 
138
        if (fd < 0)
 
139
                ser_open_error(port);
 
140
 
 
141
        lock_set(fd, port);
 
142
 
 
143
        return fd;
 
144
}
 
145
 
 
146
int ser_set_speed(int fd, const char *port, speed_t speed)
 
147
{
 
148
        struct  termios tio;
 
149
 
 
150
        if (tcgetattr(fd, &tio) != 0)
 
151
                fatal("tcgetattr(%s)", port);
 
152
 
 
153
        tio.c_cflag = CS8 | CLOCAL | CREAD;
 
154
        tio.c_iflag = IGNPAR;
 
155
        tio.c_oflag = 0;
 
156
        tio.c_lflag = 0;
 
157
        tio.c_cc[VMIN] = 1;
 
158
        tio.c_cc[VTIME] = 0;
 
159
 
 
160
#ifdef HAVE_CFSETISPEED
 
161
        cfsetispeed(&tio, speed);
 
162
        cfsetospeed(&tio, speed);
 
163
#else
 
164
#error This system lacks cfsetispeed() and has no other means to set the speed
 
165
#endif
 
166
 
 
167
        tcflush(fd, TCIFLUSH);
 
168
        tcsetattr(fd, TCSANOW, &tio);
 
169
 
 
170
        return 0;
 
171
}
 
172
 
 
173
int ser_close(int fd, const char *port)
 
174
{
 
175
        if (fd < 0)
 
176
                fatal("ser_close: programming error: fd=%d port=%s", fd, port);
 
177
 
 
178
        if (close(fd) != 0)
 
179
                return -1;
 
180
 
 
181
#ifdef HAVE_UU_LOCK
 
182
        if (do_lock_port)
 
183
                uu_unlock(xbasename(port));
 
184
#endif
 
185
 
 
186
        return 0;
 
187
}
 
188
 
 
189
int ser_send_char(int fd, char ch)
 
190
{
 
191
        return write(fd, &ch, 1);
 
192
}
 
193
 
 
194
/* send the results of the format string with d_usec delay after each char */
 
195
unsigned int ser_send_pace(int fd, unsigned long d_usec, const char *fmt, ...)
 
196
{
 
197
        int     ret;
 
198
        char    buf[1024], *p;
 
199
        va_list ap;
 
200
        unsigned int    sent = 0;
 
201
 
 
202
        va_start(ap, fmt);
 
203
 
 
204
        ret = vsnprintf(buf, sizeof(buf), fmt, ap);
 
205
 
 
206
        va_end(ap);
 
207
 
 
208
        if ((ret < 1) || (ret >= (int) sizeof(buf)))
 
209
                upslogx(LOG_WARNING, "ser_send_pace: vsnprintf needed more "
 
210
                        "than %d bytes", sizeof(buf));
 
211
 
 
212
        for (p = buf; *p; p++) {
 
213
                if (write(fd, p, 1) != 1)
 
214
                        return -1;
 
215
 
 
216
                if (d_usec)
 
217
                        usleep(d_usec);
 
218
 
 
219
                sent++;
 
220
        }
 
221
 
 
222
        return sent;
 
223
}
 
224
 
 
225
/* send the results of the format string with no delay */
 
226
unsigned int ser_send(int fd, const char *fmt, ...)
 
227
{
 
228
        int     ret;
 
229
        char    buf[1024];
 
230
        va_list ap;
 
231
 
 
232
        va_start(ap, fmt);
 
233
 
 
234
        ret = vsnprintf(buf, sizeof(buf), fmt, ap);
 
235
 
 
236
        va_end(ap);
 
237
 
 
238
        if ((ret < 1) || (ret >= (int) sizeof(buf)))
 
239
                upslogx(LOG_WARNING, "ser_send: vsnprintf needed more "
 
240
                        "than %d bytes", sizeof(buf));
 
241
 
 
242
        ret = write(fd, buf, strlen(buf));
 
243
 
 
244
        return ret;
 
245
}
 
246
 
 
247
/* send buflen bytes from buf with no delay */
 
248
int ser_send_buf(int fd, const char *buf, size_t buflen)
 
249
{
 
250
        return (write(fd, buf, buflen));
 
251
}
 
252
 
 
253
/* send buflen bytes from buf with d_usec delay after each char */
 
254
int ser_send_buf_pace(int fd, unsigned long d_usec, const char *buf, 
 
255
        size_t buflen)
 
256
{
 
257
        int     ret;
 
258
        unsigned int    i;
 
259
 
 
260
        for (i = 0; i < buflen; i++) {
 
261
                ret = ser_send_char(fd, buf[i]);
 
262
 
 
263
                if (ret != 1)
 
264
                        return ret;
 
265
 
 
266
                usleep(d_usec);
 
267
        }
 
268
 
 
269
        return buflen;
 
270
}
 
271
 
 
272
static int get_buf(int fd, char *buf, size_t buflen, long d_sec, long d_usec)
 
273
{
 
274
        int     ret;
 
275
        fd_set  rfds;
 
276
        struct  timeval tv; 
 
277
 
 
278
        FD_ZERO(&rfds);
 
279
        FD_SET(fd, &rfds);
 
280
 
 
281
        tv.tv_sec = d_sec;
 
282
        tv.tv_usec = d_usec;
 
283
 
 
284
        ret = select(fd + 1, &rfds, NULL, NULL, &tv);
 
285
 
 
286
        if (ret == -1) {
 
287
                if (errno != EINTR)
 
288
                        upslog(LOG_ERR, "select fd %d", fd);
 
289
 
 
290
                return -1;
 
291
        }
 
292
 
 
293
        if (ret == 0)
 
294
                return -1;      /* timeout */
 
295
 
 
296
        ret = read(fd, buf, buflen);
 
297
 
 
298
        if (ret < 1)
 
299
                return -1;      /* read error */
 
300
 
 
301
        return ret;
 
302
}
 
303
 
 
304
int ser_get_char(int fd, char *ch, long d_sec, long d_usec)
 
305
{
 
306
        return get_buf(fd, ch, 1, d_sec, d_usec);
 
307
}
 
308
 
 
309
/* keep reading until buflen bytes are received or a timeout occurs */
 
310
int ser_get_buf_len(int fd, char *buf, size_t buflen, long d_sec, long d_usec)
 
311
{
 
312
        int     i, ret;
 
313
        char    tmp[64];
 
314
        size_t  count = 0, tmplen;
 
315
 
 
316
        memset(buf, '\0', buflen);
 
317
 
 
318
        /* don't use all of tmp if the caller has a small buffer */
 
319
        tmplen = sizeof(tmp);
 
320
 
 
321
        if (buflen < tmplen)
 
322
                tmplen = buflen;
 
323
 
 
324
        while (count < buflen) {
 
325
                ret = get_buf(fd, tmp, tmplen, d_sec, d_usec);
 
326
 
 
327
                if (ret < 1)
 
328
                        return -1;
 
329
 
 
330
                for (i = 0; i < ret; i++) {
 
331
                        if (count == buflen) 
 
332
                                return count;
 
333
 
 
334
                        buf[count++] = tmp[i];
 
335
                }
 
336
 
 
337
                /* make sure we don't read too much next time */
 
338
                tmplen = sizeof(tmp);
 
339
 
 
340
                if ((buflen - count) < tmplen)
 
341
                        tmplen = buflen - count;
 
342
                
 
343
        }
 
344
 
 
345
        return count;
 
346
}
 
347
 
 
348
/* reads a line up to <endchar>, discarding anything else that may follow,
 
349
   with callouts to the handler if anything matches the alertset */
 
350
int ser_get_line_alert(int fd, char *buf, size_t buflen, char endchar,
 
351
        const char *ignset, const char *alertset, void handler(char ch), 
 
352
        long d_sec, long d_usec)
 
353
{
 
354
        int     i, ret;
 
355
        char    tmp[64];
 
356
        size_t  count = 0, maxcount;
 
357
 
 
358
        memset(buf, '\0', buflen);
 
359
 
 
360
        maxcount = buflen - 1;          /* for trailing \0 */
 
361
 
 
362
        while (count < maxcount) {
 
363
                ret = get_buf(fd, tmp, sizeof(tmp), d_sec, d_usec);
 
364
 
 
365
                if (ret < 1)
 
366
                        return -1;
 
367
 
 
368
                for (i = 0; i < ret; i++) {
 
369
 
 
370
                        if ((count == maxcount) || (tmp[i] == endchar)) {
 
371
                                buf[count] = '\0';
 
372
                                return count;
 
373
                        }
 
374
 
 
375
                        if (strchr(ignset, tmp[i]))
 
376
                                continue;
 
377
 
 
378
                        if (strchr(alertset, tmp[i])) {
 
379
                                if (handler)
 
380
                                        handler(tmp[i]);
 
381
 
 
382
                                continue;
 
383
                        }
 
384
 
 
385
                        buf[count++] = tmp[i];
 
386
                }
 
387
        }
 
388
 
 
389
        buf[count] = '\0';
 
390
 
 
391
        return count;
 
392
}
 
393
 
 
394
/* as above, only with no alertset handling (just a wrapper) */
 
395
int ser_get_line(int fd, char *buf, size_t buflen, char endchar,
 
396
        const char *ignset, long d_sec, long d_usec)
 
397
{
 
398
        return ser_get_line_alert(fd, buf, buflen, endchar, ignset, "", NULL,
 
399
                d_sec, d_usec);
 
400
}
 
401
 
 
402
int ser_flush_in(int fd, const char *ignset, int verbose)
 
403
{
 
404
        int     ret, extra = 0;
 
405
        char    ch;
 
406
 
 
407
        while ((ret = ser_get_char(fd, &ch, 0, 0)) > 0) {
 
408
 
 
409
                if (strchr(ignset, ch))
 
410
                        continue;
 
411
 
 
412
                extra++;
 
413
 
 
414
                if (verbose == 0)
 
415
                        continue;
 
416
 
 
417
                if (isprint(ch & 0xFF))
 
418
                        upslogx(LOG_INFO, "ser_flush_in: read char %c", ch);
 
419
                else
 
420
                        upslogx(LOG_INFO, "ser_flush_in: read char 0x%02x", ch);
 
421
        }
 
422
 
 
423
        return extra;
 
424
}
 
425
 
 
426
void ser_comm_fail(const char *fmt, ...)
 
427
{
 
428
        int     ret;
 
429
        char    why[SMALLBUF];
 
430
        va_list ap;
 
431
 
 
432
        /* this means we're probably here because select was interrupted */
 
433
        if (exit_flag != 0)
 
434
                return;         /* ignored, since we're about to exit anyway */
 
435
 
 
436
        comm_failures++;
 
437
 
 
438
        if ((comm_failures == SER_ERR_LIMIT) ||
 
439
                ((comm_failures % SER_ERR_RATE) == 0)) {
 
440
                upslogx(LOG_WARNING, "Warning: excessive comm failures, "
 
441
                        "limiting error reporting");
 
442
        }
 
443
 
 
444
        /* once it's past the limit, only log once every SER_ERR_LIMIT calls */
 
445
        if ((comm_failures > SER_ERR_LIMIT) &&
 
446
                ((comm_failures % SER_ERR_LIMIT) != 0))
 
447
                return;
 
448
 
 
449
        /* generic message if the caller hasn't elaborated */
 
450
        if (!fmt) {
 
451
                upslogx(LOG_WARNING, "Communications with UPS lost"
 
452
                        " - check cabling");
 
453
                return;
 
454
        }
 
455
 
 
456
        va_start(ap, fmt);
 
457
        ret = vsnprintf(why, sizeof(why), fmt, ap);
 
458
        va_end(ap);
 
459
 
 
460
        if ((ret < 1) || (ret >= (int) sizeof(why)))
 
461
                upslogx(LOG_WARNING, "ser_comm_fail: vsnprintf needed "
 
462
                        "more than %d bytes", sizeof(why));
 
463
 
 
464
        upslogx(LOG_WARNING, "Communications with UPS lost: %s", why);
 
465
}
 
466
 
 
467
void ser_comm_good(void)
 
468
{
 
469
        if (comm_failures == 0)
 
470
                return;
 
471
 
 
472
        upslogx(LOG_NOTICE, "Communications with UPS re-established");
 
473
        comm_failures = 0;
 
474
}