mail(1): Bring some fixes from other BSDs.
- Use varargs properly - Use pid_t - Better handling of error conditions on forked jobs. - Some prototype and warning cleanups. Obtained from: NetBSD (some originaly from OpenBSD)
This commit is contained in:
parent
41b594a6e0
commit
6c5ab8a438
@ -439,7 +439,7 @@ folders(void)
|
|||||||
}
|
}
|
||||||
if ((cmd = value("LISTER")) == NULL)
|
if ((cmd = value("LISTER")) == NULL)
|
||||||
cmd = "ls";
|
cmd = "ls";
|
||||||
(void)run_command(cmd, 0, -1, -1, dirname, NULL, NULL);
|
(void)run_command(cmd, 0, -1, -1, dirname, NULL);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,8 +130,9 @@ next(int *msgvec)
|
|||||||
* so we can discard when the user quits.
|
* so we can discard when the user quits.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
save(char str[])
|
save(void *v)
|
||||||
{
|
{
|
||||||
|
char *str = v;
|
||||||
|
|
||||||
return (save1(str, 1, "save", saveignore));
|
return (save1(str, 1, "save", saveignore));
|
||||||
}
|
}
|
||||||
@ -140,8 +141,9 @@ save(char str[])
|
|||||||
* Copy a message to a file without affected its saved-ness
|
* Copy a message to a file without affected its saved-ness
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
copycmd(char str[])
|
copycmd(void *v)
|
||||||
{
|
{
|
||||||
|
char *str = v;
|
||||||
|
|
||||||
return (save1(str, 0, "copy", saveignore));
|
return (save1(str, 0, "copy", saveignore));
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,7 @@ dosh(char *str __unused)
|
|||||||
|
|
||||||
if ((sh = value("SHELL")) == NULL)
|
if ((sh = value("SHELL")) == NULL)
|
||||||
sh = _PATH_CSHELL;
|
sh = _PATH_CSHELL;
|
||||||
(void)run_command(sh, 0, -1, -1, NULL, NULL, NULL);
|
(void)run_command(sh, 0, -1, -1, NULL);
|
||||||
(void)signal(SIGINT, sigint);
|
(void)signal(SIGINT, sigint);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
return (0);
|
return (0);
|
||||||
@ -102,7 +102,7 @@ bangexp(char *str, size_t strsize)
|
|||||||
n = sizeof(bangbuf);
|
n = sizeof(bangbuf);
|
||||||
while (*cp != '\0') {
|
while (*cp != '\0') {
|
||||||
if (*cp == '!') {
|
if (*cp == '!') {
|
||||||
if (n < strlen(lastbang)) {
|
if (n < (int)strlen(lastbang)) {
|
||||||
overf:
|
overf:
|
||||||
printf("Command buffer overflow\n");
|
printf("Command buffer overflow\n");
|
||||||
return (-1);
|
return (-1);
|
||||||
|
@ -180,7 +180,7 @@ run_editor(FILE *fp, off_t size, int type, int readonly)
|
|||||||
nf = NULL;
|
nf = NULL;
|
||||||
if ((edit = value(type == 'e' ? "EDITOR" : "VISUAL")) == NULL)
|
if ((edit = value(type == 'e' ? "EDITOR" : "VISUAL")) == NULL)
|
||||||
edit = type == 'e' ? _PATH_EX : _PATH_VI;
|
edit = type == 'e' ? _PATH_EX : _PATH_VI;
|
||||||
if (run_command(edit, 0, -1, -1, tempname, NULL, NULL) < 0) {
|
if (run_command(edit, 0, -1, -1, tempname, NULL) < 0) {
|
||||||
(void)rm(tempname);
|
(void)rm(tempname);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ char *copyin(char *, char **);
|
|||||||
char *detract(struct name *, int);
|
char *detract(struct name *, int);
|
||||||
char *expand(char *);
|
char *expand(char *);
|
||||||
char *getdeadletter(void);
|
char *getdeadletter(void);
|
||||||
char *getname(int);
|
char *getname(uid_t);
|
||||||
char *hfield(const char *, struct message *);
|
char *hfield(const char *, struct message *);
|
||||||
FILE *infix(struct header *, FILE *);
|
FILE *infix(struct header *, FILE *);
|
||||||
char *ishfield(char [], char *, const char *);
|
char *ishfield(char [], char *, const char *);
|
||||||
@ -95,7 +95,7 @@ void collhup(int);
|
|||||||
void collint(int);
|
void collint(int);
|
||||||
void collstop(int);
|
void collstop(int);
|
||||||
void commands(void);
|
void commands(void);
|
||||||
int copycmd(char []);
|
int copycmd(void *v);
|
||||||
int core(void);
|
int core(void);
|
||||||
int count(struct name *);
|
int count(struct name *);
|
||||||
int delete(int []);
|
int delete(int []);
|
||||||
@ -130,7 +130,7 @@ int getfold(char *, int);
|
|||||||
int gethfield(FILE *, char [], int, char **);
|
int gethfield(FILE *, char [], int, char **);
|
||||||
int getmsglist(char *, int *, int);
|
int getmsglist(char *, int *, int);
|
||||||
int getrawlist(char [], char **, int);
|
int getrawlist(char [], char **, int);
|
||||||
int getuserid(char []);
|
uid_t getuserid(char []);
|
||||||
int grabh(struct header *, int);
|
int grabh(struct header *, int);
|
||||||
int group(char **);
|
int group(char **);
|
||||||
void hangup(int);
|
void hangup(int);
|
||||||
@ -198,8 +198,8 @@ int respond(int *);
|
|||||||
int retfield(char *[]);
|
int retfield(char *[]);
|
||||||
int rexit(int);
|
int rexit(int);
|
||||||
int rm(char *);
|
int rm(char *);
|
||||||
int run_command(char *, sigset_t *, int, int, char *, char *, char *);
|
int run_command(char *, sigset_t *, int, int, ...);
|
||||||
int save(char []);
|
int save(void *v);
|
||||||
int save1(char [], int, const char *, struct ignoretab *);
|
int save1(char [], int, const char *, struct ignoretab *);
|
||||||
void savedeadletter(FILE *);
|
void savedeadletter(FILE *);
|
||||||
int saveigfield(char *[]);
|
int saveigfield(char *[]);
|
||||||
@ -223,7 +223,7 @@ void sort(char **);
|
|||||||
int source(char **);
|
int source(char **);
|
||||||
void spreserve(void);
|
void spreserve(void);
|
||||||
void sreset(void);
|
void sreset(void);
|
||||||
int start_command(char *, sigset_t *, int, int, char *, char *, char *);
|
int start_command(char *, sigset_t *, int, int, ...);
|
||||||
void statusput(struct message *, FILE *, char *);
|
void statusput(struct message *, FILE *, char *);
|
||||||
void stop(int);
|
void stop(int);
|
||||||
int stouch(int []);
|
int stouch(int []);
|
||||||
|
@ -235,7 +235,7 @@ makemessage(FILE *f, int omsgCount)
|
|||||||
size -= (omsgCount + 1) * sizeof(struct message);
|
size -= (omsgCount + 1) * sizeof(struct message);
|
||||||
(void)fflush(f);
|
(void)fflush(f);
|
||||||
(void)lseek(fileno(f), (off_t)sizeof(*message), 0);
|
(void)lseek(fileno(f), (off_t)sizeof(*message), 0);
|
||||||
if (read(fileno(f), (char *)&message[omsgCount], size) != size)
|
if (read(fileno(f), (void *)&message[omsgCount], size) != size)
|
||||||
errx(1, "Message temporary file corrupted");
|
errx(1, "Message temporary file corrupted");
|
||||||
message[msgCount].m_size = 0;
|
message[msgCount].m_size = 0;
|
||||||
message[msgCount].m_lines = 0;
|
message[msgCount].m_lines = 0;
|
||||||
|
@ -45,7 +45,7 @@ __FBSDID("$FreeBSD$");
|
|||||||
* Search the passwd file for a uid. Return name on success, NULL on failure.
|
* Search the passwd file for a uid. Return name on success, NULL on failure.
|
||||||
*/
|
*/
|
||||||
char *
|
char *
|
||||||
getname(int uid)
|
getname(uid_t uid)
|
||||||
{
|
{
|
||||||
struct passwd *pw;
|
struct passwd *pw;
|
||||||
|
|
||||||
@ -58,7 +58,7 @@ getname(int uid)
|
|||||||
* Convert the passed name to a user id and return it. Return -1
|
* Convert the passed name to a user id and return it. Return -1
|
||||||
* on error.
|
* on error.
|
||||||
*/
|
*/
|
||||||
int
|
uid_t
|
||||||
getuserid(char name[])
|
getuserid(char name[])
|
||||||
{
|
{
|
||||||
struct passwd *pw;
|
struct passwd *pw;
|
||||||
|
@ -38,6 +38,8 @@ __FBSDID("$FreeBSD$");
|
|||||||
#include "rcv.h"
|
#include "rcv.h"
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdarg.h>
|
||||||
#include "extern.h"
|
#include "extern.h"
|
||||||
|
|
||||||
#define READ 0
|
#define READ 0
|
||||||
@ -46,22 +48,23 @@ __FBSDID("$FreeBSD$");
|
|||||||
struct fp {
|
struct fp {
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
int pipe;
|
int pipe;
|
||||||
int pid;
|
pid_t pid;
|
||||||
struct fp *link;
|
struct fp *link;
|
||||||
};
|
};
|
||||||
static struct fp *fp_head;
|
static struct fp *fp_head;
|
||||||
|
|
||||||
struct child {
|
struct child {
|
||||||
int pid;
|
pid_t pid;
|
||||||
char done;
|
char done;
|
||||||
char free;
|
char free;
|
||||||
int status;
|
int status;
|
||||||
struct child *link;
|
struct child *link;
|
||||||
};
|
};
|
||||||
static struct child *child;
|
static struct child *child, *child_freelist = NULL;
|
||||||
static struct child *findchild(int);
|
|
||||||
static void delchild(struct child *);
|
static void delchild(struct child *);
|
||||||
static int file_pid(FILE *);
|
static pid_t file_pid(FILE *);
|
||||||
|
static pid_t start_commandv(char *, sigset_t *, int, int, va_list);
|
||||||
|
|
||||||
FILE *
|
FILE *
|
||||||
Fopen(const char *path, const char *mode)
|
Fopen(const char *path, const char *mode)
|
||||||
@ -90,6 +93,7 @@ Fdopen(int fd, const char *mode)
|
|||||||
int
|
int
|
||||||
Fclose(FILE *fp)
|
Fclose(FILE *fp)
|
||||||
{
|
{
|
||||||
|
|
||||||
unregister_file(fp);
|
unregister_file(fp);
|
||||||
return (fclose(fp));
|
return (fclose(fp));
|
||||||
}
|
}
|
||||||
@ -99,7 +103,7 @@ Popen(char *cmd, const char *mode)
|
|||||||
{
|
{
|
||||||
int p[2];
|
int p[2];
|
||||||
int myside, hisside, fd0, fd1;
|
int myside, hisside, fd0, fd1;
|
||||||
int pid;
|
pid_t pid;
|
||||||
sigset_t nset;
|
sigset_t nset;
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
|
|
||||||
@ -109,15 +113,15 @@ Popen(char *cmd, const char *mode)
|
|||||||
(void)fcntl(p[WRITE], F_SETFD, 1);
|
(void)fcntl(p[WRITE], F_SETFD, 1);
|
||||||
if (*mode == 'r') {
|
if (*mode == 'r') {
|
||||||
myside = p[READ];
|
myside = p[READ];
|
||||||
fd0 = -1;
|
hisside = fd0 = fd1 = p[WRITE];
|
||||||
hisside = fd1 = p[WRITE];
|
|
||||||
} else {
|
} else {
|
||||||
myside = p[WRITE];
|
myside = p[WRITE];
|
||||||
hisside = fd0 = p[READ];
|
hisside = fd0 = p[READ];
|
||||||
fd1 = -1;
|
fd1 = -1;
|
||||||
}
|
}
|
||||||
(void)sigemptyset(&nset);
|
(void)sigemptyset(&nset);
|
||||||
if ((pid = start_command(cmd, &nset, fd0, fd1, NULL, NULL, NULL)) < 0) {
|
pid = start_command(value("SHELL"), &nset, fd0, fd1, "-c", cmd, NULL);
|
||||||
|
if (pid < 0) {
|
||||||
(void)close(p[READ]);
|
(void)close(p[READ]);
|
||||||
(void)close(p[WRITE]);
|
(void)close(p[WRITE]);
|
||||||
return (NULL);
|
return (NULL);
|
||||||
@ -158,7 +162,7 @@ close_all_files(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
register_file(FILE *fp, int pipe, int pid)
|
register_file(FILE *fp, int pipe, pid_t pid)
|
||||||
{
|
{
|
||||||
struct fp *fpp;
|
struct fp *fpp;
|
||||||
|
|
||||||
@ -186,7 +190,7 @@ unregister_file(FILE *fp)
|
|||||||
/*NOTREACHED*/
|
/*NOTREACHED*/
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
pid_t
|
||||||
file_pid(FILE *fp)
|
file_pid(FILE *fp)
|
||||||
{
|
{
|
||||||
struct fp *p;
|
struct fp *p;
|
||||||
@ -200,29 +204,16 @@ file_pid(FILE *fp)
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* Run a command without a shell, with optional arguments and splicing
|
* Run a command without a shell, with optional arguments and splicing
|
||||||
* of stdin and stdout. The command name can be a sequence of words.
|
* of stdin (-1 means none) and stdout. The command name can be a sequence
|
||||||
|
* of words.
|
||||||
* Signals must be handled by the caller.
|
* Signals must be handled by the caller.
|
||||||
* "Mask" contains the signals to ignore in the new process.
|
* "nset" contains the signals to ignore in the new process.
|
||||||
* SIGINT is enabled unless it's in the mask.
|
* SIGINT is enabled unless it's in "nset".
|
||||||
*/
|
*/
|
||||||
/*VARARGS4*/
|
static pid_t
|
||||||
int
|
start_commandv(char *cmd, sigset_t *nset, int infd, int outfd, va_list args)
|
||||||
run_command(char *cmd, sigset_t *mask, int infd, int outfd, char *a0,
|
|
||||||
char *a1, char *a2)
|
|
||||||
{
|
{
|
||||||
int pid;
|
pid_t pid;
|
||||||
|
|
||||||
if ((pid = start_command(cmd, mask, infd, outfd, a0, a1, a2)) < 0)
|
|
||||||
return (-1);
|
|
||||||
return (wait_command(pid));
|
|
||||||
}
|
|
||||||
|
|
||||||
/*VARARGS4*/
|
|
||||||
int
|
|
||||||
start_command(char *cmd, sigset_t *mask, int infd, int outfd, char *a0,
|
|
||||||
char *a1, char *a2)
|
|
||||||
{
|
|
||||||
int pid;
|
|
||||||
|
|
||||||
if ((pid = fork()) < 0) {
|
if ((pid = fork()) < 0) {
|
||||||
warn("fork");
|
warn("fork");
|
||||||
@ -232,11 +223,10 @@ start_command(char *cmd, sigset_t *mask, int infd, int outfd, char *a0,
|
|||||||
char *argv[100];
|
char *argv[100];
|
||||||
int i = getrawlist(cmd, argv, sizeof(argv) / sizeof(*argv));
|
int i = getrawlist(cmd, argv, sizeof(argv) / sizeof(*argv));
|
||||||
|
|
||||||
if ((argv[i++] = a0) != NULL &&
|
while ((argv[i++] = va_arg(args, char *)))
|
||||||
(argv[i++] = a1) != NULL &&
|
;
|
||||||
(argv[i++] = a2) != NULL)
|
argv[i] = NULL;
|
||||||
argv[i] = NULL;
|
prepare_child(nset, infd, outfd);
|
||||||
prepare_child(mask, infd, outfd);
|
|
||||||
execvp(argv[0], argv);
|
execvp(argv[0], argv);
|
||||||
warn("%s", argv[0]);
|
warn("%s", argv[0]);
|
||||||
_exit(1);
|
_exit(1);
|
||||||
@ -244,6 +234,32 @@ start_command(char *cmd, sigset_t *mask, int infd, int outfd, char *a0,
|
|||||||
return (pid);
|
return (pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
run_command(char *cmd, sigset_t *nset, int infd, int outfd, ...)
|
||||||
|
{
|
||||||
|
pid_t pid;
|
||||||
|
va_list args;
|
||||||
|
|
||||||
|
va_start(args, outfd);
|
||||||
|
pid = start_commandv(cmd, nset, infd, outfd, args);
|
||||||
|
va_end(args);
|
||||||
|
if (pid < 0)
|
||||||
|
return -1;
|
||||||
|
return wait_command(pid);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
start_command(char *cmd, sigset_t *nset, int infd, int outfd, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
int r;
|
||||||
|
|
||||||
|
va_start(args, outfd);
|
||||||
|
r = start_commandv(cmd, nset, infd, outfd, args);
|
||||||
|
va_end(args);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
prepare_child(sigset_t *nset, int infd, int outfd)
|
prepare_child(sigset_t *nset, int infd, int outfd)
|
||||||
{
|
{
|
||||||
@ -268,7 +284,7 @@ prepare_child(sigset_t *nset, int infd, int outfd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
wait_command(int pid)
|
wait_command(pid_t pid)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (wait_child(pid) < 0) {
|
if (wait_child(pid) < 0) {
|
||||||
@ -279,7 +295,7 @@ wait_command(int pid)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static struct child *
|
static struct child *
|
||||||
findchild(int pid)
|
findchild(pid_t pid, int dont_alloc)
|
||||||
{
|
{
|
||||||
struct child **cpp;
|
struct child **cpp;
|
||||||
|
|
||||||
@ -287,9 +303,16 @@ findchild(int pid)
|
|||||||
cpp = &(*cpp)->link)
|
cpp = &(*cpp)->link)
|
||||||
;
|
;
|
||||||
if (*cpp == NULL) {
|
if (*cpp == NULL) {
|
||||||
*cpp = malloc(sizeof(struct child));
|
if (dont_alloc)
|
||||||
if (*cpp == NULL)
|
return(NULL);
|
||||||
err(1, "Out of memory");
|
if (child_freelist) {
|
||||||
|
*cpp = child_freelist;
|
||||||
|
child_freelist = (*cpp)->link;
|
||||||
|
} else {
|
||||||
|
*cpp = malloc(sizeof(struct child));
|
||||||
|
if (*cpp == NULL)
|
||||||
|
err(1, "malloc");
|
||||||
|
}
|
||||||
(*cpp)->pid = pid;
|
(*cpp)->pid = pid;
|
||||||
(*cpp)->done = (*cpp)->free = 0;
|
(*cpp)->done = (*cpp)->free = 0;
|
||||||
(*cpp)->link = NULL;
|
(*cpp)->link = NULL;
|
||||||
@ -305,19 +328,22 @@ delchild(struct child *cp)
|
|||||||
for (cpp = &child; *cpp != cp; cpp = &(*cpp)->link)
|
for (cpp = &child; *cpp != cp; cpp = &(*cpp)->link)
|
||||||
;
|
;
|
||||||
*cpp = cp->link;
|
*cpp = cp->link;
|
||||||
(void)free(cp);
|
cp->link = child_freelist;
|
||||||
|
child_freelist = cp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*ARGSUSED*/
|
/*ARGSUSED*/
|
||||||
void
|
void
|
||||||
sigchild(int signo __unused)
|
sigchild(int signo __unused)
|
||||||
{
|
{
|
||||||
int pid;
|
pid_t pid;
|
||||||
int status;
|
int status;
|
||||||
struct child *cp;
|
struct child *cp;
|
||||||
|
int save_errno;
|
||||||
|
|
||||||
|
save_errno = errno;
|
||||||
while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
|
while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
|
||||||
cp = findchild(pid);
|
cp = findchild(pid, 1);
|
||||||
if (cp->free)
|
if (cp->free)
|
||||||
delchild(cp);
|
delchild(cp);
|
||||||
else {
|
else {
|
||||||
@ -325,6 +351,7 @@ sigchild(int signo __unused)
|
|||||||
cp->status = status;
|
cp->status = status;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
errno = save_errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
int wait_status;
|
int wait_status;
|
||||||
@ -333,7 +360,7 @@ int wait_status;
|
|||||||
* Wait for a specific child to die.
|
* Wait for a specific child to die.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
wait_child(int pid)
|
wait_child(pid_t pid)
|
||||||
{
|
{
|
||||||
sigset_t nset, oset;
|
sigset_t nset, oset;
|
||||||
struct child *cp;
|
struct child *cp;
|
||||||
@ -342,7 +369,7 @@ wait_child(int pid)
|
|||||||
(void)sigaddset(&nset, SIGCHLD);
|
(void)sigaddset(&nset, SIGCHLD);
|
||||||
(void)sigprocmask(SIG_BLOCK, &nset, &oset);
|
(void)sigprocmask(SIG_BLOCK, &nset, &oset);
|
||||||
|
|
||||||
cp = findchild(pid);
|
cp = findchild(pid, 1);
|
||||||
|
|
||||||
while (!cp->done)
|
while (!cp->done)
|
||||||
(void)sigsuspend(&oset);
|
(void)sigsuspend(&oset);
|
||||||
@ -356,18 +383,19 @@ wait_child(int pid)
|
|||||||
* Mark a child as don't care.
|
* Mark a child as don't care.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
free_child(int pid)
|
free_child(pid_t pid)
|
||||||
{
|
{
|
||||||
|
struct child *cp;
|
||||||
sigset_t nset, oset;
|
sigset_t nset, oset;
|
||||||
struct child *cp = findchild(pid);
|
|
||||||
|
|
||||||
(void)sigemptyset(&nset);
|
(void)sigemptyset(&nset);
|
||||||
(void)sigaddset(&nset, SIGCHLD);
|
(void)sigaddset(&nset, SIGCHLD);
|
||||||
(void)sigprocmask(SIG_BLOCK, &nset, &oset);
|
(void)sigprocmask(SIG_BLOCK, &nset, &oset);
|
||||||
|
if ((cp = findchild(pid, 0)) != NULL) {
|
||||||
if (cp->done)
|
if (cp->done)
|
||||||
delchild(cp);
|
delchild(cp);
|
||||||
else
|
else
|
||||||
cp->free = 1;
|
cp->free = 1;
|
||||||
|
}
|
||||||
(void)sigprocmask(SIG_SETMASK, &oset, NULL);
|
(void)sigprocmask(SIG_SETMASK, &oset, NULL);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user