Re: [PATCH v5 26/44] tty: Add read-recursive, writer-prioritized rwsemaphore

From: Peter Hurley
Date: Tue Mar 26 2013 - 19:48:44 EST


On Mon, 2013-03-18 at 18:59 -0700, Greg Kroah-Hartman wrote:
> > If you'd like, I can send you 6 or so short user test programs that
> > hang, crash, or deadlock inside 60 seconds on mainline and next, but not
> > with this patchset.
>
> That would be interesting to have, please send them.
>
> And I hope that they only lock up when run as root, but I'm afraid to
> ask that question...

Sorry Greg, I meant to get to this sooner.

This one is pretty straightforward. Does not require suid.
I would still recommend only running this in a disposable vm -- that's
what I do. The vm should have 6 cores to do real business.

It helps to hook up netconsole or whatever so you can see what kind of
progress it's making but don't use a slow framebuffer console because
that invariably locks up :). If it pauses for 5 secs., that's actually
correct behavior.

--- >% ---

Signed-off-by: Peter Hurley <peter@xxxxxxxxxxxxxxxxxx>
---
pts_test3.c | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 151 insertions(+)
create mode 100644 pts_test3.c

diff --git a/pts_test3.c b/pts_test3.c
new file mode 100644
index 0000000..b673f17
--- /dev/null
+++ b/pts_test3.c
@@ -0,0 +1,151 @@
+/*
+ * pts_test3.c
+ *
+ * Created on: Dec, 2012
+ * Copyright (C) 2012 Ilya Zykov
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Modified-by: Peter Hurley <peter@xxxxxxxxxxxxxxxxxx>
+ */
+
+#include <stdio.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <termios.h>
+#include <stdlib.h>
+#include <pthread.h>
+#include <signal.h>
+
+#define BUF_SIZE 2
+#define ERROR_EXIT_CODE 1
+#define parent child_id
+
+static int
+mfd=-1, sfd=-1, parent=1;
+
+static pthread_t
+pth_id;
+
+static char
+pty_name[24], buf[]={ '1', '\n' };
+
+
+static void
+pty_exit(int ret, char * exit_message){
+ if (sfd >= 0) close(sfd);
+ if (mfd >= 0) close(mfd);
+ printf("%s %s %s exit. \n",exit_message?exit_message:"",
+ ret?"Error":"Normal", parent?"parent":"child");
+ exit(ret);
+}
+
+static void
+pty_init(void){
+ int ptn;
+ if( (mfd=open("/dev/ptmx", O_RDWR )) < 0 )
+ pty_exit(ERROR_EXIT_CODE,"Couldn't open /dev/ptmx. \n");
+ if (ioctl(mfd, TIOCGPTN, &ptn) < 0 )
+ pty_exit(ERROR_EXIT_CODE,"Couldn't get pty number. \n");
+ snprintf(pty_name, sizeof(pty_name), "/dev/pts/%d", ptn);
+ //printf("Slave pty name = %s.\n",pty_name);
+ ptn=0;
+ if (ioctl(mfd, TIOCSPTLCK, &ptn) < 0 )
+ pty_exit(ERROR_EXIT_CODE,"Couldn't unlock pty slave. \n");
+ if ( (sfd=open(pty_name, O_RDWR )) < 0 )
+ pty_exit(ERROR_EXIT_CODE, "Couldn't open pty slave. \n");
+}
+
+static void *
+pty_thread_open(void * arg) {
+ static const char ret[]="Thread open has been created.\n";
+ printf(ret);
+ do {
+ close(open(pty_name, O_RDWR ));
+ } while(1);
+ return ret;
+}
+
+static void *
+pty_thread_read(void * arg) {
+ static const char ret[]="Thread read has been created.\n";
+ printf(ret);
+ do {
+ read(sfd, buf, BUF_SIZE);
+ } while(1);
+ return ret;
+}
+
+static void *
+pty_thread_write(void * arg) {
+ static char ret[]="Thread write has been created.\n";
+ printf(ret);
+ do {
+ write(mfd, buf, BUF_SIZE);
+ } while(1);
+ return ret;
+}
+
+#define N_PPS 18
+
+static void *
+pty_thread_msetd(void * arg) {
+ static const char ret[]="Thread msetd has been created.\n";
+ static int ldisc;
+ printf(ret);
+ do {
+ ldisc = N_PPS;
+ ioctl(mfd, TIOCSETD, &ldisc);
+ ldisc = N_TTY;
+ ioctl(mfd, TIOCSETD, &ldisc);
+ } while(1);
+ return ret;
+}
+
+static void *
+pty_thread_ssetd(void * arg) {
+ static char ret[]="Thread ssetd has been created.\n";
+ static int ldisc;
+ printf(ret);
+ do {
+ ldisc = N_PPS;
+ ioctl(sfd, TIOCSETD, &ldisc);
+ ldisc = N_TTY;
+ ioctl(sfd, TIOCSETD, &ldisc);
+ } while(1);
+ return ret;
+}
+
+int main(int argc,char *argv[]) {
+ pty_init();
+ child_id=fork();
+ if(parent) {
+ sleep(100);
+ kill(child_id, SIGINT);
+ pty_exit(0,"Parent normal exit\n");
+ }
+ pthread_create(&pth_id, NULL, &pty_thread_open, 0);
+ /* For WARNINGS. */
+ pthread_create(&pth_id, NULL, &pty_thread_write, 0);
+ pthread_create(&pth_id, NULL, &pty_thread_read, 0);
+
+ pthread_create(&pth_id, NULL, &pty_thread_msetd, 0);
+ pthread_create(&pth_id, NULL, &pty_thread_ssetd, 0);
+ do {
+ close(sfd);
+ close(mfd);
+ pty_init();
+ } while(1);
+ return 0;
+}
--
1.8.1.2




--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/