summaryrefslogtreecommitdiff
path: root/modules/pammodutil/modutil_ioloop.c
blob: a852a7b8b8d6c9bb57b0698dbed7a8ee86a44af4 (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
/*
 * $Id$
 *
 * These functions provides common methods for ensure a complete read or
 * write occurs. It handles EINTR and partial read/write returns.
 */

#include <unistd.h>
#include <errno.h>

#include <security/pam_modules.h>
#include "include/security/_pam_modutil.h"

int _pammodutil_read(int fd, char *buffer, int count)
{
       int block, offset = 0;

       while (count > 0) {
               block = read(fd, &buffer[offset], count);

               if (block < 0) {
                       if (errno == EINTR) continue;
                       return block;
               }
               if (block == 0) return offset;

               offset += block;
               count -= block;
       }

       return offset;
}

int _pammodutil_write(int fd, const char *buffer, int count)
{
       int block, offset = 0;

       while (count > 0) {
               block = write(fd, &buffer[offset], count);

               if (block < 0) {
                       if (errno == EINTR) continue;
                       return block;
               }
               if (block == 0) return offset;

               offset += block;
               count -= block;
       }

       return offset;
}