make passwd/group functions safe against cancellation in stdio

these changes are a prerequisite to making stdio cancellable.
This commit is contained in:
Rich Felker 2012-02-01 23:51:19 -05:00
parent 147f355cb6
commit 4948a24df2
4 changed files with 35 additions and 6 deletions

View File

@ -1,4 +1,5 @@
#include "pwf.h"
#include <pthread.h>
#define FIX(x) (gr->gr_##x = gr->gr_##x-line+buf)
@ -11,9 +12,15 @@ static int getgr_r(const char *name, gid_t gid, struct group *gr, char *buf, siz
size_t nmem = 0;
int rv = 0;
size_t i;
int cs;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
f = fopen("/etc/group", "rb");
if (!f) return errno;
if (!f) {
rv = errno;
goto done;
}
*res = 0;
while (__getgrent_a(f, gr, &line, &len, &mem, &nmem)) {
@ -39,6 +46,8 @@ static int getgr_r(const char *name, gid_t gid, struct group *gr, char *buf, siz
free(mem);
free(line);
fclose(f);
done:
pthread_setcancelstate(cs, 0);
return rv;
}

View File

@ -1,16 +1,19 @@
#include "pwf.h"
#include <pthread.h>
struct group *__getgrent_a(FILE *f, struct group *gr, char **line, size_t *size, char ***mem, size_t *nmem)
{
ssize_t l;
char *s, *mems;
size_t i;
int cs;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
for (;;) {
if ((l=getline(line, size, f)) < 0) {
free(*line);
*line = 0;
return 0;
gr = 0;
goto end;
}
line[0][l-1] = 0;
@ -46,5 +49,7 @@ struct group *__getgrent_a(FILE *f, struct group *gr, char **line, size_t *size,
mem[0][0] = 0;
}
gr->gr_mem = *mem;
end:
pthread_setcancelstate(cs, 0);
return gr;
}

View File

@ -1,4 +1,5 @@
#include "pwf.h"
#include <pthread.h>
#define FIX(x) (pw->pw_##x = pw->pw_##x-line+buf)
@ -8,9 +9,15 @@ static int getpw_r(const char *name, uid_t uid, struct passwd *pw, char *buf, si
char *line = 0;
size_t len = 0;
int rv = 0;
int cs;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
f = fopen("/etc/passwd", "rb");
if (!f) return errno;
if (!f) {
rv = errno;
goto done;
}
*res = 0;
while (__getpwent_a(f, pw, &line, &len)) {
@ -32,6 +39,8 @@ static int getpw_r(const char *name, uid_t uid, struct passwd *pw, char *buf, si
}
free(line);
fclose(f);
done:
pthread_setcancelstate(cs, 0);
return rv;
}

View File

@ -1,14 +1,18 @@
#include "pwf.h"
#include <pthread.h>
struct passwd *__getpwent_a(FILE *f, struct passwd *pw, char **line, size_t *size)
{
ssize_t l;
char *s;
int cs;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
for (;;) {
if ((l=getline(line, size, f)) < 0) {
free(*line);
*line = 0;
return 0;
pw = 0;
break;
}
line[0][l-1] = 0;
@ -32,6 +36,8 @@ struct passwd *__getpwent_a(FILE *f, struct passwd *pw, char **line, size_t *siz
if (!(s = strchr(s, ':'))) continue;
*s++ = 0; pw->pw_shell = s;
break;
}
pthread_setcancelstate(cs, 0);
return pw;
}
}