File: 13_predictable_client.diff

package info (click to toggle)
ttysnoop 0.12d-6.1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 392 kB
  • sloc: ansic: 5,419; makefile: 165
file content (130 lines) | stat: -rw-r--r-- 3,190 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
Description: Predictable behaviour of client program.
 Better error recovery and exeption handling.
 .
 Ignore the SIGPIPE trigger.
 .
 Give priority to reading the device being snooped at,
 only then reading from standard input.
Author: Mats Erik Andersson <debian@gisladisker.se>
Forwarded: no
Last-Update: 2010-06-21
--- ttysnoop-0.12d.debian/ttysnoop.c
+++ ttysnoop-0.12d/ttysnoop.c
@@ -25,6 +25,7 @@
 #include <grp.h>
 #include <utmp.h>
 #include <errno.h>
+#include <signal.h>
 
 #include "config.h"
 #include "common.h"
@@ -33,6 +34,11 @@
 
 char buff[BUFF_SIZE];
 
+void message (void)
+{
+	printf ("\r\nBack at local tty.\r\n");
+}
+
 int main (int argc, char *argv[])
 {
 	fd_set readset;
@@ -40,24 +46,30 @@ int main (int argc, char *argv[])
 	int sockfd, fdmax, quit = 0, n;
 	char sockname[128];
 	
+	if (geteuid())
+		errorf("Insufficient privileges.\n");
+
 	if (argc < 2)
 		errorf ("Usage: ttysnoop <pty>\n");
 	
 	/* create the client socket */
 	
 	if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
-		errorf ("can't create client socket\n");
+		errorf ("Cannot create client socket.\n");
 	
-	sprintf (sockname, "%s/%s", SPOOLDIR, argv[1]);
+	snprintf (sockname, sizeof(sockname), "%s/%s", SPOOLDIR, argv[1]);
+	memset (&sock_addr, '\0', sizeof(sock_addr));
 	sock_addr.sun_family = AF_UNIX;
 	strncopy (sock_addr.sun_path, sockname);
+
 	if (connect(sockfd, (struct sockaddr *) &sock_addr, sizeof(sock_addr)) < 0)
-		errorf ("can't connect to server\n");
+		errorf ("Cannot connect to server. Is it really a valid socket?\n");
 	
 	/* put stdin into raw mode */
 	
 	stty_initstore ();
 	atexit (stty_orig);
+	atexit (message);
 	if (isatty(STDIN_FILENO))
 		stty_raw (STDIN_FILENO);
 
@@ -67,32 +79,52 @@ int main (int argc, char *argv[])
 	
 	/* do our thing */
 	
+	signal(SIGPIPE, SIG_IGN);
+	FD_ZERO (&readset);
+
 	while (!quit)
 	{
-		FD_ZERO (&readset);
+		int ret;
+
 		FD_SET (STDIN_FILENO, &readset);
 		FD_SET (sockfd, &readset);
 		
-		select (fdmax + 1, &readset, NULL, NULL, NULL);
-		
-		if (FD_ISSET(STDIN_FILENO, &readset))
-		{
-			n = read(STDIN_FILENO, buff, BUFF_SIZE);
+		if ( (ret = select (fdmax + 1, &readset, NULL, NULL, NULL)) < 0 ) {
+			if ( errno == EINTR )
+				continue;
 
-			if (write(sockfd, buff, n) < 0)
-				quit = 1;
+			break;	/* Cannot recover. */
 		}
 		
+		/* Give precedence to the end point where we snoop. */
 		if (FD_ISSET(sockfd, &readset))
 		{
-			if ((n = read(sockfd, buff, BUFF_SIZE)) < 1)
-				quit = 1;
+			if ((n = read(sockfd, buff, BUFF_SIZE)) <= 0) {
+				if ( (n < 0) && (errno == EINTR) )
+					continue;
+				if ( n == 0 )
+					break;	/* EOF on socket. */
 
+				quit = 1;	/* Will soon exit! */
+			}
 			if (n > 0)
 				write (STDOUT_FILENO, buff, n);
 		}
+		
+		if (FD_ISSET(STDIN_FILENO, &readset))
+		{
+			n = read(STDIN_FILENO, buff, BUFF_SIZE);
+			if ( n == 0 ) {
+				/* The observer has left. Close down! */
+				shutdown(sockfd, SHUT_WR);
+				FD_CLR(STDIN_FILENO, &readset);
+				quit = 1;
+			}
+
+			if ( ( n > 0 ) && ( write(sockfd, buff, n) < 0 ) )
+				quit = 1;	/* The terminal service has already closed down. */
+		}
 	}
 	
-	printf ("\r\nBack at local tty.\r\n");
 	return 0;
 }