Convert popen()'s `pidlist' to a SLIST, for consistency.
I guess the original author of the popen() code didn't want to use our <sys/queue.h> macro's, because the single linked list macro's didn't offer O(1) deletion. Because I introduced SLIST_REMOVE_NEXT() some time ago, we can now use the macro's here. By converting the code to an SLIST, it is more consistent with other parts of the C library and the operating system. Reviewed by: csjp Approved by: philip (mentor, implicit)
This commit is contained in:
parent
b423c28f74
commit
7e7f7ca3c6
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=180963
@ -38,6 +38,7 @@ __FBSDID("$FreeBSD$");
|
|||||||
|
|
||||||
#include "namespace.h"
|
#include "namespace.h"
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
|
#include <sys/queue.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
|
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
@ -53,11 +54,12 @@ __FBSDID("$FreeBSD$");
|
|||||||
|
|
||||||
extern char **environ;
|
extern char **environ;
|
||||||
|
|
||||||
static struct pid {
|
struct pid {
|
||||||
struct pid *next;
|
SLIST_ENTRY(pid) next;
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
} *pidlist;
|
};
|
||||||
|
static SLIST_HEAD(, pid) pidlist = SLIST_HEAD_INITIALIZER(pidlist);
|
||||||
static pthread_mutex_t pidlist_mutex = PTHREAD_MUTEX_INITIALIZER;
|
static pthread_mutex_t pidlist_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
|
||||||
#define THREAD_LOCK() if (__isthreaded) _pthread_mutex_lock(&pidlist_mutex)
|
#define THREAD_LOCK() if (__isthreaded) _pthread_mutex_lock(&pidlist_mutex)
|
||||||
@ -133,9 +135,8 @@ popen(command, type)
|
|||||||
}
|
}
|
||||||
(void)_close(pdes[1]);
|
(void)_close(pdes[1]);
|
||||||
}
|
}
|
||||||
for (p = pidlist; p; p = p->next) {
|
SLIST_FOREACH(p, &pidlist, next)
|
||||||
(void)_close(fileno(p->fp));
|
(void)_close(fileno(p->fp));
|
||||||
}
|
|
||||||
_execve(_PATH_BSHELL, argv, environ);
|
_execve(_PATH_BSHELL, argv, environ);
|
||||||
_exit(127);
|
_exit(127);
|
||||||
/* NOTREACHED */
|
/* NOTREACHED */
|
||||||
@ -155,8 +156,7 @@ popen(command, type)
|
|||||||
cur->fp = iop;
|
cur->fp = iop;
|
||||||
cur->pid = pid;
|
cur->pid = pid;
|
||||||
THREAD_LOCK();
|
THREAD_LOCK();
|
||||||
cur->next = pidlist;
|
SLIST_INSERT_HEAD(&pidlist, cur, next);
|
||||||
pidlist = cur;
|
|
||||||
THREAD_UNLOCK();
|
THREAD_UNLOCK();
|
||||||
|
|
||||||
return (iop);
|
return (iop);
|
||||||
@ -171,7 +171,7 @@ int
|
|||||||
pclose(iop)
|
pclose(iop)
|
||||||
FILE *iop;
|
FILE *iop;
|
||||||
{
|
{
|
||||||
struct pid *cur, *last;
|
struct pid *cur, *last = NULL;
|
||||||
int pstat;
|
int pstat;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
|
|
||||||
@ -179,17 +179,19 @@ pclose(iop)
|
|||||||
* Find the appropriate file pointer and remove it from the list.
|
* Find the appropriate file pointer and remove it from the list.
|
||||||
*/
|
*/
|
||||||
THREAD_LOCK();
|
THREAD_LOCK();
|
||||||
for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
|
SLIST_FOREACH(cur, &pidlist, next) {
|
||||||
if (cur->fp == iop)
|
if (cur->fp == iop)
|
||||||
break;
|
break;
|
||||||
|
last = cur;
|
||||||
|
}
|
||||||
if (cur == NULL) {
|
if (cur == NULL) {
|
||||||
THREAD_UNLOCK();
|
THREAD_UNLOCK();
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
if (last == NULL)
|
if (last == NULL)
|
||||||
pidlist = cur->next;
|
SLIST_REMOVE_HEAD(&pidlist, next);
|
||||||
else
|
else
|
||||||
last->next = cur->next;
|
SLIST_REMOVE_NEXT(&pidlist, last, next);
|
||||||
THREAD_UNLOCK();
|
THREAD_UNLOCK();
|
||||||
|
|
||||||
(void)fclose(iop);
|
(void)fclose(iop);
|
||||||
|
Loading…
Reference in New Issue
Block a user