summaryrefslogtreecommitdiffstats
path: root/src/util
diff options
context:
space:
mode:
authorJakub Hrozek <jhrozek@redhat.com>2012-04-02 17:26:05 -0400
committerStephen Gallagher <sgallagh@redhat.com>2012-04-20 10:55:14 -0400
commit9d7d4458d94d0aac0a7edf999368eb18f89cb76a (patch)
treeb443c2ed560bc5f61cae78e94ceeb795fa1f83b4 /src/util
parent9959c512ac3ba36f7a0db7614f0357ce0bae748f (diff)
downloadsssd-9d7d4458d94d0aac0a7edf999368eb18f89cb76a.tar.gz
sssd-9d7d4458d94d0aac0a7edf999368eb18f89cb76a.tar.xz
sssd-9d7d4458d94d0aac0a7edf999368eb18f89cb76a.zip
Convert read and write operations to sss_atomic_read
https://fedorahosted.org/sssd/ticket/1209
Diffstat (limited to 'src/util')
-rw-r--r--src/util/backup_file.c44
-rw-r--r--src/util/child_common.c53
-rw-r--r--src/util/find_uid.c16
-rw-r--r--src/util/server.c96
4 files changed, 89 insertions, 120 deletions
diff --git a/src/util/backup_file.c b/src/util/backup_file.c
index ba7764049..edbf868cb 100644
--- a/src/util/backup_file.c
+++ b/src/util/backup_file.c
@@ -33,9 +33,8 @@ int backup_file(const char *src_file, int dbglvl)
int src_fd = -1;
int dst_fd = -1;
char *dst_file;
- ssize_t count;
- ssize_t num;
- ssize_t pos;
+ ssize_t numread;
+ ssize_t written;
int ret, i;
src_fd = open(src_file, O_RDONLY);
@@ -84,31 +83,30 @@ int backup_file(const char *src_file, int dbglvl)
/* copy file contents */
while (1) {
- num = read(src_fd, buf, BUFFER_SIZE);
- if (num < 0) {
- if (errno == EINTR || errno == EAGAIN) continue;
+ errno = 0;
+ numread = sss_atomic_read_s(src_fd, buf, BUFFER_SIZE);
+ if (numread < 0) {
ret = errno;
DEBUG(dbglvl, ("Error (%d [%s]) reading from source %s\n",
ret, strerror(ret), src_file));
goto done;
}
- if (num == 0) break;
-
- count = num;
-
- pos = 0;
- while (count > 0) {
- errno = 0;
- num = write(dst_fd, &buf[pos], count);
- if (num < 0) {
- if (errno == EINTR || errno == EAGAIN) continue;
- ret = errno;
- DEBUG(dbglvl, ("Error (%d [%s]) writing to destination %s\n",
- ret, strerror(ret), dst_file));
- goto done;
- }
- pos += num;
- count -= num;
+ if (numread == 0) break;
+
+ errno = 0;
+ written = sss_atomic_write_s(dst_fd, buf, numread);
+ if (written == -1) {
+ ret = errno;
+ DEBUG(dbglvl, ("Error (%d [%s]) writing to destination %s\n",
+ ret, strerror(ret), dst_file));
+ goto done;
+ }
+
+ if (written != numread) {
+ DEBUG(dbglvl, ("Wrote %d bytes expected %d bytes\n",
+ written, numread));
+ ret = EIO;
+ goto done;
}
}
diff --git a/src/util/child_common.c b/src/util/child_common.c
index 6214c7cc3..05f00b0f3 100644
--- a/src/util/child_common.c
+++ b/src/util/child_common.c
@@ -337,42 +337,36 @@ static void write_pipe_handler(struct tevent_context *ev,
struct tevent_req *req = talloc_get_type(pvt, struct tevent_req);
struct write_pipe_state *state = tevent_req_data(req,
struct write_pipe_state);
- ssize_t size;
+ errno_t ret;
if (flags & TEVENT_FD_READ) {
- DEBUG(1, ("write_pipe_done called with TEVENT_FD_READ,"
- " this should not happen.\n"));
+ DEBUG(SSSDBG_CRIT_FAILURE,
+ ("write_pipe_done called with TEVENT_FD_READ,"
+ " this should not happen.\n"));
tevent_req_error(req, EINVAL);
return;
}
- size = write(state->fd,
- state->buf + state->written,
- state->len - state->written);
- if (size == -1) {
- if (errno == EAGAIN || errno == EINTR) return;
- DEBUG(1, ("write failed [%d][%s].\n", errno, strerror(errno)));
- tevent_req_error(req, errno);
- return;
-
- } else if (size >= 0) {
- state->written += size;
- if (state->written > state->len) {
- DEBUG(1, ("write to much, this should never happen.\n"));
- tevent_req_error(req, EINVAL);
- return;
- }
- } else {
- DEBUG(1, ("unexpected return value of write [%d].\n", size));
- tevent_req_error(req, EINVAL);
+ errno = 0;
+ state->written = sss_atomic_write_s(state->fd, state->buf, state->len);
+ if (state->written == -1) {
+ ret = errno;
+ DEBUG(SSSDBG_CRIT_FAILURE,
+ ("write failed [%d][%s].\n", ret, strerror(ret)));
+ tevent_req_error(req, ret);
return;
}
- if (state->len == state->written) {
- DEBUG(6, ("All data has been sent!\n"));
- tevent_req_done(req);
+ if (state->len != state->written) {
+ DEBUG(SSSDBG_CRIT_FAILURE, ("Wrote %d bytes, expected %d\n",
+ state->written, state->len));
+ tevent_req_error(req, EIO);
return;
}
+
+ DEBUG(SSSDBG_TRACE_FUNC, ("All data has been sent!\n"));
+ tevent_req_done(req);
+ return;
}
int write_pipe_recv(struct tevent_req *req)
@@ -438,16 +432,13 @@ static void read_pipe_handler(struct tevent_context *ev,
return;
}
- size = read(state->fd,
+ size = sss_atomic_read_s(state->fd,
buf,
CHILD_MSG_CHUNK);
if (size == -1) {
err = errno;
- if (err == EAGAIN || err == EINTR) {
- return;
- }
-
- DEBUG(1, ("read failed [%d][%s].\n", err, strerror(err)));
+ DEBUG(SSSDBG_CRIT_FAILURE,
+ ("read failed [%d][%s].\n", err, strerror(err)));
tevent_req_error(req, err);
return;
diff --git a/src/util/find_uid.c b/src/util/find_uid.c
index 77b9f22a8..d34a4abd2 100644
--- a/src/util/find_uid.c
+++ b/src/util/find_uid.c
@@ -107,15 +107,13 @@ static errno_t get_uid_from_pid(const pid_t pid, uid_t *uid)
goto fail_fd;
}
- while ((ret = read(fd, buf, BUFSIZE)) != 0) {
- if (ret == -1) {
- error = errno;
- if (error == EINTR || error == EAGAIN) {
- continue;
- }
- DEBUG(1, ("read failed [%d][%s].\n", error, strerror(error)));
- goto fail_fd;
- }
+ errno = 0;
+ ret = sss_atomic_read_s(fd, buf, BUFSIZE);
+ if (ret == -1) {
+ error = errno;
+ DEBUG(SSSDBG_CRIT_FAILURE,
+ ("read failed [%d][%s].\n", error, strerror(error)));
+ goto fail_fd;
}
/* Guarantee NULL-termination in case we read the full BUFSIZE somehow */
diff --git a/src/util/server.c b/src/util/server.c
index 95b183632..39cc12776 100644
--- a/src/util/server.c
+++ b/src/util/server.c
@@ -117,51 +117,36 @@ int pidfile(const char *path, const char *name)
fd = open(file, O_RDONLY, 0644);
err = errno;
if (fd != -1) {
- len = 0;
- while ((ret = read(fd, pid_str + len, pidlen - len)) != 0) {
- if (ret == -1) {
- if (errno == EINTR || errno == EAGAIN) {
- continue;
- }
- DEBUG(1, ("read failed [%d][%s].\n", errno, strerror(errno)));
- break;
- } else if (ret > 0) {
- len += ret;
- if (len > pidlen) {
- DEBUG(1, ("read too much, this should never happen.\n"));
- close(fd);
- talloc_free(file);
- return EINVAL;
- }
- continue;
- } else {
- DEBUG(1, ("unexpected return code of read [%d].\n", ret));
- break;
- }
+ errno = 0;
+ len = sss_atomic_read_s(fd, pid_str, pidlen);
+ ret = errno;
+ if (len == -1) {
+ DEBUG(SSSDBG_CRIT_FAILURE,
+ ("read failed [%d][%s].\n", ret, strerror(ret)));
+ close(fd);
+ talloc_free(file);
+ return EINVAL;
}
/* Ensure NULL-termination */
pid_str[len] = '\0';
- if (ret == 0) {
- /* let's check the pid */
-
- pid = (pid_t)atoi(pid_str);
- if (pid != 0) {
- errno = 0;
- ret = kill(pid, 0);
- /* succeeded in signaling the process -> another sssd process */
- if (ret == 0) {
- close(fd);
- talloc_free(file);
- return EEXIST;
- }
- if (ret != 0 && errno != ESRCH) {
- err = errno;
- close(fd);
- talloc_free(file);
- return err;
- }
+ /* let's check the pid */
+ pid = (pid_t)atoi(pid_str);
+ if (pid != 0) {
+ errno = 0;
+ ret = kill(pid, 0);
+ /* succeeded in signaling the process -> another sssd process */
+ if (ret == 0) {
+ close(fd);
+ talloc_free(file);
+ return EEXIST;
+ }
+ if (ret != 0 && errno != ESRCH) {
+ err = errno;
+ close(fd);
+ talloc_free(file);
+ return err;
}
}
@@ -188,25 +173,20 @@ int pidfile(const char *path, const char *name)
snprintf(pid_str, sizeof(pid_str) -1, "%u\n", (unsigned int) getpid());
size = strlen(pid_str);
- written = 0;
- while (written < size) {
- ret = write(fd, pid_str+written, size-written);
- if (ret == -1) {
- err = errno;
- if (err == EINTR || err == EAGAIN) {
- continue;
- }
- DEBUG(1, ("write failed [%d][%s]\n", err, strerror(err)));
- break;
- }
- else {
- written += ret;
- }
+ errno = 0;
+ written = sss_atomic_write_s(fd, pid_str, size);
+ if (ret == -1) {
+ err = errno;
+ DEBUG(SSSDBG_CRIT_FAILURE,
+ ("write failed [%d][%s]\n", err, strerror(err)));
+ return err;
}
if (written != size) {
+ DEBUG(SSSDBG_CRIT_FAILURE,
+ ("Wrote %d bytes expected %d\n", written, size));
close(fd);
- return err;
+ return EIO;
}
close(fd);
@@ -317,8 +297,10 @@ static void server_stdin_handler(struct tevent_context *event_ctx,
{
const char *binary_name = (const char *)private;
uint8_t c;
- if (read(0, &c, 1) == 0) {
- DEBUG(0,("%s: EOF on stdin - terminating\n", binary_name));
+
+ errno = 0;
+ if (sss_atomic_read_s(0, &c, 1) == 0) {
+ DEBUG(SSSDBG_CRIT_FAILURE,("%s: EOF on stdin - terminating\n", binary_name));
#if HAVE_GETPGRP
if (getpgrp() == getpid()) {
kill(-getpgrp(), SIGTERM);