~ubuntu-branches/ubuntu/natty/ntp/natty

« back to all changes in this revision

Viewing changes to lib/isc/unix/keyboard.c

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2010-11-30 11:14:31 UTC
  • mfrom: (4.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20101130111431-7r00o0ck9qzwmm9f
Tags: 1:4.2.6.p2+dfsg-1ubuntu1
* Merge from debian unstable, remaining changes are:
  + debian/ntp.conf, debian/ntpdate.default: Change default server to
    ntp.ubuntu.com.
  + debian/ntpdate.ifup: Stop ntp before running ntpdate when an interface
    comes up, then start again afterwards.
  + debian/ntp.init, debian/rules: Only stop when entering single user mode.
  + Add enforcing AppArmor profile (LP: #382905):
    - debian/control: add Conflicts/Replaces on apparmor-profiles <
      2.3.1+1403-0ubuntu10 (since we are now shipping usr.sbin.ntpd) and
      apparmor < 2.3.1+1403-0ubuntu10 (since we are now shipping tunables/ntpd)
    - debian/control: add Suggests on apparmor
    - debian/ntp.dirs: add apparmor directories
    - debian/ntp.preinst: force complain on certain upgrades
    - debian/ntp.postinst: reload apparmor profile
    - debian/ntp.postrm: remove the force-complain file
    - add debian/apparmor-profile*
    - debian/rules: install apparmor-profile and apparmor-profile.tunable
    - debian/README.Debian: add note on AppArmor
  + debian/{control,rules}: add and enable hardened build for PIE
    (Debian bug 542721).
  + debian/apparmor-profile: adjust location of drift files (LP: #456308)
  + debian/rules, debian/ntp.dirs, debian/source_ntp.py: Add apport hook.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2004, 2007  Internet Systems Consortium, Inc. ("ISC")
 
3
 * Copyright (C) 2000, 2001  Internet Software Consortium.
 
4
 *
 
5
 * Permission to use, copy, modify, and/or distribute this software for any
 
6
 * purpose with or without fee is hereby granted, provided that the above
 
7
 * copyright notice and this permission notice appear in all copies.
 
8
 *
 
9
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 
10
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 
11
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 
12
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 
13
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 
14
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 
15
 * PERFORMANCE OF THIS SOFTWARE.
 
16
 */
 
17
 
 
18
/* $Id: keyboard.c,v 1.13 2007/06/19 23:47:18 tbox Exp $ */
 
19
 
 
20
#include <config.h>
 
21
 
 
22
#include <sys/param.h>
 
23
#include <sys/types.h>
 
24
#include <sys/time.h>
 
25
#include <sys/uio.h>
 
26
 
 
27
#include <errno.h>
 
28
#include <stdlib.h>
 
29
#include <string.h>
 
30
#include <termios.h>
 
31
#include <unistd.h>
 
32
#include <fcntl.h>
 
33
 
 
34
#include <isc/keyboard.h>
 
35
#include <isc/util.h>
 
36
 
 
37
isc_result_t
 
38
isc_keyboard_open(isc_keyboard_t *keyboard) {
 
39
        int fd;
 
40
        isc_result_t ret;
 
41
        struct termios current_mode;
 
42
 
 
43
        REQUIRE(keyboard != NULL);
 
44
 
 
45
        fd = open("/dev/tty", O_RDONLY, 0);
 
46
        if (fd < 0)
 
47
                return (ISC_R_IOERROR);
 
48
 
 
49
        keyboard->fd = fd;
 
50
 
 
51
        if (tcgetattr(fd, &keyboard->saved_mode) < 0) {
 
52
                ret = ISC_R_IOERROR;
 
53
                goto errout;
 
54
        }
 
55
 
 
56
        current_mode = keyboard->saved_mode;
 
57
 
 
58
        current_mode.c_iflag &=
 
59
                        ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
 
60
        current_mode.c_oflag &= ~OPOST;
 
61
        current_mode.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
 
62
        current_mode.c_cflag &= ~(CSIZE|PARENB);
 
63
        current_mode.c_cflag |= CS8;
 
64
 
 
65
        current_mode.c_cc[VMIN] = 1;
 
66
        current_mode.c_cc[VTIME] = 0;
 
67
        if (tcsetattr(fd, TCSAFLUSH, &current_mode) < 0) {
 
68
                ret = ISC_R_IOERROR;
 
69
                goto errout;
 
70
        }
 
71
 
 
72
        keyboard->result = ISC_R_SUCCESS;
 
73
 
 
74
        return (ISC_R_SUCCESS);
 
75
 
 
76
 errout:
 
77
        close (fd);
 
78
 
 
79
        return (ret);
 
80
}
 
81
 
 
82
isc_result_t
 
83
isc_keyboard_close(isc_keyboard_t *keyboard, unsigned int sleeptime) {
 
84
        REQUIRE(keyboard != NULL);
 
85
 
 
86
        if (sleeptime > 0 && keyboard->result != ISC_R_CANCELED)
 
87
                (void)sleep(sleeptime);
 
88
 
 
89
        (void)tcsetattr(keyboard->fd, TCSAFLUSH, &keyboard->saved_mode);
 
90
        (void)close(keyboard->fd);
 
91
 
 
92
        keyboard->fd = -1;
 
93
 
 
94
        return (ISC_R_SUCCESS);
 
95
}
 
96
 
 
97
isc_result_t
 
98
isc_keyboard_getchar(isc_keyboard_t *keyboard, unsigned char *cp) {
 
99
        ssize_t cc;
 
100
        unsigned char c;
 
101
        cc_t *controlchars;
 
102
 
 
103
        REQUIRE(keyboard != NULL);
 
104
        REQUIRE(cp != NULL);
 
105
 
 
106
        cc = read(keyboard->fd, &c, 1);
 
107
        if (cc < 0) {
 
108
                keyboard->result = ISC_R_IOERROR;
 
109
                return (keyboard->result);
 
110
        }
 
111
 
 
112
        controlchars = keyboard->saved_mode.c_cc;
 
113
        if (c == controlchars[VINTR] || c == controlchars[VQUIT]) {
 
114
                keyboard->result = ISC_R_CANCELED;
 
115
                return (keyboard->result);
 
116
        }
 
117
 
 
118
        *cp = c;
 
119
 
 
120
        return (ISC_R_SUCCESS);
 
121
}
 
122
 
 
123
isc_boolean_t
 
124
isc_keyboard_canceled(isc_keyboard_t *keyboard) {
 
125
        return (ISC_TF(keyboard->result == ISC_R_CANCELED));
 
126
}