~ubuntu-branches/ubuntu/saucy/acpid/saucy-proposed

« back to all changes in this revision

Viewing changes to sock.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Meskes
  • Date: 2011-04-17 16:33:17 UTC
  • mfrom: (2.1.28 sid)
  • Revision ID: james.westby@ubuntu.com-20110417163317-j1io7oftjay4r9i2
Tags: 1:2.0.9-1
ImportedĀ UpstreamĀ versionĀ 2.0.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
/* the number of non-root clients that are connected */
43
43
int non_root_clients;
44
44
 
 
45
/* accept a new client connection */
45
46
static void
46
47
process_sock(int fd)
47
48
{
53
54
        /* accept and add to our lists */
54
55
        cli_fd = ud_accept(fd, &creds);
55
56
        if (cli_fd < 0) {
56
 
                acpid_log(LOG_ERR, "can't accept client: %s\n",
 
57
                acpid_log(LOG_ERR, "can't accept client: %s",
57
58
                          strerror(errno));
58
59
                accept_errors++;
59
60
                if (accept_errors >= 5) {
60
 
                        acpid_log(LOG_ERR, "giving up\n");
 
61
                        acpid_log(LOG_ERR, "giving up");
61
62
                        clean_exit_with_status(EXIT_FAILURE);
62
63
                }
63
64
                return;
64
65
        }
65
66
        accept_errors = 0;
66
 
        /* This check against clientmax is from the non-netlink 1.0.10.  */
 
67
 
 
68
        /* don't allow too many non-root clients  */
67
69
        if (creds.uid != 0 && non_root_clients >= clientmax) {
68
70
                close(cli_fd);
69
 
                acpid_log(LOG_ERR,
70
 
                    "too many non-root clients\n");
 
71
                acpid_log(LOG_ERR, "too many non-root clients");
71
72
                return;
72
73
        }
73
74
        if (creds.uid != 0) {
74
75
                non_root_clients++;
75
76
        }
76
 
        fcntl(cli_fd, F_SETFD, FD_CLOEXEC);
77
 
        snprintf(buf, sizeof(buf)-1, "%d[%d:%d]",
 
77
 
 
78
    /* don't leak fds when execing */
 
79
        if (fcntl(cli_fd, F_SETFD, FD_CLOEXEC) < 0) {
 
80
                close(cli_fd);
 
81
                acpid_log(LOG_ERR, "fcntl() on client for FD_CLOEXEC: %s", 
 
82
            strerror(errno));
 
83
                return;
 
84
    }
 
85
 
 
86
    /* don't allow clients to block this */
 
87
    if (fcntl(cli_fd, F_SETFL, O_NONBLOCK) < 0) {
 
88
                close(cli_fd);
 
89
                acpid_log(LOG_ERR, "fcntl() on client for O_NONBLOCK: %s", 
 
90
            strerror(errno));
 
91
                return;
 
92
    }
 
93
 
 
94
    snprintf(buf, sizeof(buf)-1, "%d[%d:%d]",
78
95
                 creds.pid, creds.uid, creds.gid);
79
96
        acpid_add_client(cli_fd, buf);
80
97
}
81
98
 
 
99
/* set up the socket for client connections */
82
100
void
83
101
open_sock()
84
102
{
87
105
 
88
106
        fd = ud_create_socket(socketfile);
89
107
        if (fd < 0) {
90
 
                acpid_log(LOG_ERR, "can't open socket %s: %s\n",
 
108
                acpid_log(LOG_ERR, "can't open socket %s: %s",
91
109
                        socketfile, strerror(errno));
92
110
                exit(EXIT_FAILURE);
93
111
        }
94
 
        fcntl(fd, F_SETFD, FD_CLOEXEC);
95
 
        chmod(socketfile, socketmode);
 
112
 
 
113
    /* don't leak fds when execing */
 
114
    if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
 
115
                close(fd);
 
116
                acpid_log(LOG_ERR, "fcntl() on socket %s for FD_CLOEXEC: %s", 
 
117
            socketfile, strerror(errno));
 
118
                return;
 
119
    }
 
120
 
 
121
    /* avoid a potential hang */
 
122
    if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
 
123
                close(fd);
 
124
                acpid_log(LOG_ERR, "fcntl() on socket %s for O_NONBLOCK: %s", 
 
125
            socketfile, strerror(errno));
 
126
                return;
 
127
    }
 
128
 
 
129
        if (chmod(socketfile, socketmode) < 0) {
 
130
                close(fd);
 
131
                acpid_log(LOG_ERR, "chmod() on socket %s: %s", 
 
132
            socketfile, strerror(errno));
 
133
                return;
 
134
    }
 
135
 
 
136
    /* if we need to change the socket's group, do so */
96
137
        if (socketgroup) {
97
138
                struct group *gr;
98
139
                struct stat buf;
99
 
                gr = getgrnam(socketgroup);
 
140
 
 
141
        gr = getgrnam(socketgroup);
100
142
                if (!gr) {
101
 
                        acpid_log(LOG_ERR, "group %s does not exist\n", socketgroup);
 
143
                        acpid_log(LOG_ERR, "group %s does not exist", socketgroup);
102
144
                        exit(EXIT_FAILURE);
103
145
                }
104
146
                if (stat(socketfile, &buf) < 0) {
105
 
                        acpid_log(LOG_ERR, "can't stat %s\n", socketfile);
 
147
                        acpid_log(LOG_ERR, "can't stat %s: %s", 
 
148
                socketfile, strerror(errno));
106
149
                        exit(EXIT_FAILURE);
107
150
                }
108
151
                if (chown(socketfile, buf.st_uid, gr->gr_gid) < 0) {
109
 
                        acpid_log(LOG_ERR, "can't chown: %s\n", strerror(errno));
 
152
                        acpid_log(LOG_ERR, "can't chown %s: %s", 
 
153
                socketfile, strerror(errno));
110
154
                        exit(EXIT_FAILURE);
111
155
                }
112
156
        }
116
160
        c.process = process_sock;
117
161
        add_connection(&c);
118
162
}
119