nexmon – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /* source: xioclose.c */
2 /* Copyright Gerhard Rieger 2001-2008 */
3 /* Published under the GNU General Public License V.2, see file COPYING */
4  
5 /* this is the source of the extended close function */
6  
7  
8 #include "xiosysincludes.h"
9 #include "xioopen.h"
10 #include "xiolockfile.h"
11  
12 #include "xio-termios.h"
13  
14  
15 /* close the xio fd; must be valid and "simple" (not dual) */
16 int xioclose1(struct single *pipe) {
17  
18 if (pipe->tag == XIO_TAG_INVALID) {
19 Notice("xioclose1(): invalid file descriptor");
20 errno = EINVAL;
21 return -1;
22 }
23  
24 switch (pipe->howtoclose) {
25  
26 #if WITH_READLINE
27 case XIOCLOSE_READLINE:
28 Write_history(pipe->para.readline.history_file);
29 /*xiotermios_setflag(pipe->fd, 3, ECHO|ICANON);*/ /* error when pty closed */
30 break;
31 #endif /* WITH_READLINE */
32  
33 #if WITH_OPENSSL
34 case XIOCLOSE_OPENSSL:
35 if (pipe->para.openssl.ssl) {
36 /* e.g. on TCP connection refused, we do not yet have this set */
37 sycSSL_shutdown(pipe->para.openssl.ssl);
38 sycSSL_free(pipe->para.openssl.ssl);
39 pipe->para.openssl.ssl = NULL;
40 }
41 Close(pipe->rfd); pipe->rfd = -1;
42 Close(pipe->wfd); pipe->wfd = -1;
43 if (pipe->para.openssl.ctx) {
44 sycSSL_CTX_free(pipe->para.openssl.ctx);
45 pipe->para.openssl.ctx = NULL;
46 }
47 break;
48 #endif /* WITH_OPENSSL */
49  
50 case XIOCLOSE_SIGTERM:
51 if (pipe->child.pid > 0) {
52 if (Kill(pipe->child.pid, SIGTERM) < 0) {
53 Msg2(errno==ESRCH?E_INFO:E_WARN, "kill(%d, SIGTERM): %s",
54 pipe->child.pid, strerror(errno));
55 }
56 }
57 break;
58 case XIOCLOSE_CLOSE_SIGTERM:
59 if (pipe->child.pid > 0) {
60 if (Kill(pipe->child.pid, SIGTERM) < 0) {
61 Msg2(errno==ESRCH?E_INFO:E_WARN, "kill(%d, SIGTERM): %s",
62 pipe->child.pid, strerror(errno));
63 }
64 }
65 /*PASSTHROUGH*/
66 case XIOCLOSE_CLOSE:
67 if (XIOWITHRD(pipe->flags) && pipe->rfd >= 0) {
68 if (Close(pipe->rfd) < 0) {
69 Info2("close(%d): %s", pipe->rfd, strerror(errno));
70 }
71 }
72 if (XIOWITHWR(pipe->flags) && pipe->wfd >= 0) {
73 if (Close(pipe->wfd) < 0) {
74 Info2("close(%d): %s", pipe->wfd, strerror(errno));
75 }
76 }
77 break;
78  
79 case XIOCLOSE_SLEEP_SIGTERM:
80 Usleep(250000);
81 if (pipe->child.pid > 0) {
82 if (Kill(pipe->child.pid, SIGTERM) < 0) {
83 Msg2(errno==ESRCH?E_INFO:E_WARN, "kill(%d, SIGTERM): %s",
84 pipe->child.pid, strerror(errno));
85 }
86 }
87 break;
88  
89 case XIOCLOSE_NONE:
90 break;
91  
92 case XIOCLOSE_UNSPEC:
93 Warn1("xioclose(): no close action specified on 0x%x", pipe);
94 break;
95  
96 default:
97 Error2("xioclose(): bad close action 0x%x on 0x%x", pipe->howtoclose, pipe);
98 break;
99 }
100  
101 #if WITH_TERMIOS
102 if (pipe->ttyvalid) {
103 if (Tcsetattr(pipe->rfd, 0, &pipe->savetty) < 0) {
104 Warn2("cannot restore terminal settings on fd %d: %s",
105 pipe->rfd, strerror(errno));
106 }
107 }
108 #endif /* WITH_TERMIOS */
109  
110 /* unlock */
111 if (pipe->havelock) {
112 xiounlock(pipe->lock.lockfile);
113 pipe->havelock = false;
114 }
115 if (pipe->opt_unlink_close && pipe->unlink_close) {
116 if (Unlink(pipe->unlink_close) < 0) {
117 Info2("unlink(\"%s\"): %s", pipe->unlink_close, strerror(errno));
118 }
119 free(pipe->unlink_close);
120 }
121  
122 pipe->tag = XIO_TAG_INVALID;
123 return 0; /*! */
124 }
125  
126  
127 /* close the xio fd */
128 int xioclose(xiofile_t *file) {
129 xiofile_t *xfd = file;
130 int result;
131  
132 if (file->tag == XIO_TAG_INVALID) {
133 Error("xioclose(): invalid file descriptor");
134 errno = EINVAL;
135 return -1;
136 }
137  
138 if (file->tag == XIO_TAG_DUAL) {
139 result = xioclose1(file->dual.stream[0]);
140 result |= xioclose1(file->dual.stream[1]);
141 file->tag = XIO_TAG_INVALID;
142 } else {
143 result = xioclose1(&file->stream);
144 }
145 if (xfd->tag != XIO_TAG_INVALID && xfd->stream.subthread != 0) {
146 Pthread_join(xfd->stream.subthread, NULL);
147 }
148 return result;
149 }
150