提交 4948a24d 编写于 作者: R Rich Felker

make passwd/group functions safe against cancellation in stdio

these changes are a prerequisite to making stdio cancellable.
上级 147f355c
#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;
}
......
#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;
}
#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;
}
......
#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;
return pw;
break;
}
pthread_setcancelstate(cs, 0);
return pw;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册