Commit 4de712d1 authored by Guido van Rossum's avatar Guido van Rossum

Print ps (process status) for us when starting a new thread.

Even less shuffling of stdout (only at start of new interpreter).

Interact properly with new interpreter initialization conventions
(must use Py_Initialize/Py_Finalize *and*
Py_NewInterpreter/Py_EndInterpreter).

Probably more minor changes.
parent 0fa13314
...@@ -48,9 +48,12 @@ static void create_thread(int, struct sockaddr_in *); ...@@ -48,9 +48,12 @@ static void create_thread(int, struct sockaddr_in *);
static void *service_thread(struct workorder *); static void *service_thread(struct workorder *);
static void run_interpreter(FILE *, FILE *); static void run_interpreter(FILE *, FILE *);
static int run_command(char *, PyObject *); static int run_command(char *, PyObject *);
static void ps(void);
static char *progname = "pysvr"; static char *progname = "pysvr";
static PyThreadState *gtstate;
main(int argc, char **argv) main(int argc, char **argv)
{ {
int port = PORT; int port = PORT;
...@@ -98,7 +101,7 @@ usage() ...@@ -98,7 +101,7 @@ usage()
static void static void
main_thread(int port) main_thread(int port)
{ {
int sock, conn, size; int sock, conn, size, i;
struct sockaddr_in addr, clientaddr; struct sockaddr_in addr, clientaddr;
sock = socket(PF_INET, SOCK_STREAM, 0); sock = socket(PF_INET, SOCK_STREAM, 0);
...@@ -108,7 +111,7 @@ main_thread(int port) ...@@ -108,7 +111,7 @@ main_thread(int port)
exit(1); exit(1);
} }
memset(&addr, '\0', sizeof addr); memset((char *)&addr, '\0', sizeof addr);
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_port = htons(port); addr.sin_port = htons(port);
addr.sin_addr.s_addr = 0L; addr.sin_addr.s_addr = 0L;
...@@ -126,8 +129,9 @@ main_thread(int port) ...@@ -126,8 +129,9 @@ main_thread(int port)
fprintf(stderr, "Listening on port %d...\n", port); fprintf(stderr, "Listening on port %d...\n", port);
for (;;) { for (i = 0; ; i++) {
size = sizeof clientaddr; size = sizeof clientaddr;
memset((char *) &clientaddr, '\0', size);
conn = accept(sock, (struct sockaddr *) &clientaddr, &size); conn = accept(sock, (struct sockaddr *) &clientaddr, &size);
if (conn < 0) { if (conn < 0) {
oprogname(); oprogname();
...@@ -136,6 +140,7 @@ main_thread(int port) ...@@ -136,6 +140,7 @@ main_thread(int port)
} }
size = sizeof addr; size = sizeof addr;
memset((char *) &addr, '\0', size);
if (getsockname(conn, (struct sockaddr *)&addr, &size) < 0) { if (getsockname(conn, (struct sockaddr *)&addr, &size) < 0) {
oprogname(); oprogname();
perror("can't get socket name of connection"); perror("can't get socket name of connection");
...@@ -150,8 +155,21 @@ main_thread(int port) ...@@ -150,8 +155,21 @@ main_thread(int port)
close(conn); close(conn);
continue; continue;
} }
if (i == 4) {
close(conn);
break;
}
create_thread(conn, &clientaddr); create_thread(conn, &clientaddr);
} }
close(sock);
if (gtstate) {
PyEval_AcquireThread(gtstate);
gtstate = NULL;
Py_Finalize();
}
exit(0);
} }
static void static void
...@@ -192,8 +210,11 @@ static PyObject *the_builtins; ...@@ -192,8 +210,11 @@ static PyObject *the_builtins;
static void static void
init_python() init_python()
{ {
PyEval_InitThreads(); /* Create and acquire the interpreter lock */ if (gtstate)
PyEval_ReleaseLock(); /* Release the lock */ return;
Py_Initialize(); /* Initialize the interpreter */
PyEval_InitThreads(); /* Create (and acquire) the interpreter lock */
gtstate = PyEval_SaveThread(); /* Release the thread state */
} }
static void * static void *
...@@ -203,6 +224,8 @@ service_thread(struct workorder *work) ...@@ -203,6 +224,8 @@ service_thread(struct workorder *work)
fprintf(stderr, "Start thread for connection %d.\n", work->conn); fprintf(stderr, "Start thread for connection %d.\n", work->conn);
ps();
input = fdopen(work->conn, "r"); input = fdopen(work->conn, "r");
if (input == NULL) { if (input == NULL) {
oprogname(); oprogname();
...@@ -264,6 +287,10 @@ run_interpreter(FILE *input, FILE *output) ...@@ -264,6 +287,10 @@ run_interpreter(FILE *input, FILE *output)
new_stdin = PyFile_FromFile(input, "<socket-in>", "r", NULL); new_stdin = PyFile_FromFile(input, "<socket-in>", "r", NULL);
new_stdout = PyFile_FromFile(output, "<socket-out>", "w", NULL); new_stdout = PyFile_FromFile(output, "<socket-out>", "w", NULL);
PySys_SetObject("stdin", new_stdin);
PySys_SetObject("stdout", new_stdout);
PySys_SetObject("stderr", new_stdout);
for (n = 1; !PyErr_Occurred(); n++) { for (n = 1; !PyErr_Occurred(); n++) {
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
fprintf(output, "%d> ", n); fprintf(output, "%d> ", n);
...@@ -286,10 +313,6 @@ run_interpreter(FILE *input, FILE *output) ...@@ -286,10 +313,6 @@ run_interpreter(FILE *input, FILE *output)
if (p[0] == '#' || p[0] == '\0') if (p[0] == '#' || p[0] == '\0')
continue; continue;
PySys_SetObject("stdin", new_stdin);
PySys_SetObject("stdout", new_stdout);
PySys_SetObject("stderr", new_stdout);
end = run_command(buffer, globals); end = run_command(buffer, globals);
if (end < 0) if (end < 0)
PyErr_Print(); PyErr_Print();
...@@ -327,3 +350,11 @@ run_command(char *buffer, PyObject *globals) ...@@ -327,3 +350,11 @@ run_command(char *buffer, PyObject *globals)
Py_DECREF(v); Py_DECREF(v);
return 0; return 0;
} }
static void
ps()
{
char buffer[100];
sprintf(buffer, "ps -l -p %d </dev/null | tail +2l\n", getpid());
system(buffer);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment