summaryrefslogtreecommitdiff
path: root/helper/helper_proto.c
blob: 3abe263cc1506cc20de070bb015152662c3e8cdd (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
/* Part of mate-screensaver.
 *
 * Copyright (c) 2019-2021 Paul Wolneykien <manowar@altlinux.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

/* Provides functions for two-way communication between the screensaver
 * and the helper program. The idea of helper program is to be able to
 * run mate-screensaver-dialog without any setuid bits.
 */

#include "config.h"

#include <stdlib.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#include "helper_proto.h"

static ssize_t
read_all (int fd, void *buf, size_t count)
{
    ssize_t rd, t_rd = 0;

    if (0 == count)
        return 0;

    while (t_rd < count)
    {
        rd = read (fd, buf + t_rd, count - t_rd);
        if (0 == rd)
            break;
        if (rd < 0)
            return rd;
        t_rd += rd;
    }

    return t_rd;
}

ssize_t
read_msg (int fd, char *buf, size_t length)
{
    size_t msg_len;
    ssize_t rd;

    rd = read_all (fd, &msg_len, sizeof msg_len);
    if (rd < 0)
        return HELPER_IO_ERR;
    if (rd > 0 && rd != sizeof msg_len)
        return HELPER_LENGTH_READ_ERR;

    if (msg_len >= length)
        return HELPER_TOO_LONG_ERR;

    if (msg_len > 0)
    {
        rd = read_all (fd, buf, msg_len);
        if (rd < 0)
            return HELPER_IO_ERR;
        if (rd != msg_len)
            return HELPER_MSG_READ_ERR;
    }
    else
        rd = 0;
    buf[rd] = '\0';

    return rd;
}

int
read_prompt (int fd, char *buf, size_t *length)
{
    int msg_type, rd;

    rd = read_all (fd, &msg_type, sizeof msg_type);
    if (0 == rd)
        return 0;
    if (rd < 0)
        return HELPER_IO_ERR;
    if (rd > 0 && rd != sizeof msg_type)
        return HELPER_TYPE_READ_ERR;

    rd = read_msg (fd, buf, *length);
    if (rd < 0)
        return rd;

    *length = rd;
    return msg_type;
}

static ssize_t
write_all (int fd, const void *buf, size_t count)
{
    ssize_t wt, t_wt = 0;

    if (0 == count)
        return 0;

    while (t_wt < count)
    {
        wt = write (fd, buf + t_wt, count - t_wt);
        if (0 == wt)
            break;
        if (wt < 0)
            return wt;
        t_wt += wt;
    }

    return t_wt;
}

ssize_t
write_msg (int fd, const void *buf, size_t length)
{
    ssize_t wt;

    wt = write_all (fd, &length, sizeof length);
    if (wt < 0)
        return HELPER_IO_ERR;
    if (wt > 0 && wt != sizeof length)
        return HELPER_LENGTH_WRITE_ERR;

    if (length > 0)
    {
        wt = write_all (fd, buf, length);
        if (wt < 0)
            return HELPER_IO_ERR;
        if (wt != length)
            return HELPER_MSG_WRITE_ERR;
    }
    else
        wt = 0;

    return wt;
}

int
write_prompt (int fd, int msg_type, const void *buf, size_t length)
{
    ssize_t wt;

    wt = write_all (fd, &msg_type, sizeof msg_type);
    if (wt < 0)
        return HELPER_IO_ERR;
    if (wt > 0 && wt != sizeof msg_type)
        return HELPER_TYPE_WRITE_ERR;

    wt = write_msg (fd, buf, length);

    return wt;
}