nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* source: xiowrite.c */ |
2 | /* Copyright Gerhard Rieger 2001-2012 */ |
||
3 | /* Published under the GNU General Public License V.2, see file COPYING */ |
||
4 | |||
5 | /* this is the source of the extended write function */ |
||
6 | |||
7 | |||
8 | #include "xiosysincludes.h" |
||
9 | #include "xioopen.h" |
||
10 | |||
11 | #include "xio-test.h" |
||
12 | #include "xio-readline.h" |
||
13 | #include "xio-openssl.h" |
||
14 | |||
15 | |||
16 | /* ... |
||
17 | note that the write() call can block even if the select()/poll() call |
||
18 | reported the FD writeable: in case the FD is not nonblocking and a lock |
||
19 | defers the operation. |
||
20 | on return value < 0: errno reflects the value from write() */ |
||
21 | ssize_t xiowrite(xiofile_t *file, const void *buff, size_t bytes) { |
||
22 | ssize_t writt; |
||
23 | struct single *pipe; |
||
24 | int fd; |
||
25 | int _errno; |
||
26 | |||
27 | if (file->tag == XIO_TAG_INVALID) { |
||
28 | Error1("xiowrite(): invalid xiofile descriptor %p", file); |
||
29 | errno = EINVAL; |
||
30 | return -1; |
||
31 | } |
||
32 | |||
33 | if (file->tag == XIO_TAG_DUAL) { |
||
34 | pipe = file->dual.stream[1]; |
||
35 | if (pipe->tag == XIO_TAG_INVALID) { |
||
36 | Error1("xiowrite(): invalid xiofile sub descriptor %p[1]", file); |
||
37 | errno = EINVAL; |
||
38 | return -1; |
||
39 | } |
||
40 | } else { |
||
41 | pipe = &file->stream; |
||
42 | } |
||
43 | |||
44 | #if WITH_READLINE |
||
45 | /* try to extract a prompt from the write data */ |
||
46 | if ((pipe->dtype & XIODATA_READMASK) == XIOREAD_READLINE) { |
||
47 | xioscan_readline(pipe, buff, bytes); |
||
48 | } |
||
49 | #endif /* WITH_READLINE */ |
||
50 | |||
51 | fd = XIO_GETWRFD(file); |
||
52 | |||
53 | switch (pipe->dtype & XIODATA_WRITEMASK) { |
||
54 | |||
55 | case XIOWRITE_STREAM: |
||
56 | writt = writefull(pipe->wfd, buff, bytes); |
||
57 | if (writt < 0) { |
||
58 | _errno = errno; |
||
59 | switch (_errno) { |
||
60 | case EPIPE: |
||
61 | case ECONNRESET: |
||
62 | if (pipe->cool_write) { |
||
63 | Notice4("write(%d, %p, "F_Zu"): %s", |
||
64 | fd, buff, bytes, strerror(_errno)); |
||
65 | break; |
||
66 | } |
||
67 | /*PASSTRHOUGH*/ |
||
68 | default: |
||
69 | Error4("write(%d, %p, "F_Zu"): %s", |
||
70 | fd, buff, bytes, strerror(_errno)); |
||
71 | } |
||
72 | errno = _errno; |
||
73 | return -1; |
||
74 | } |
||
75 | break; |
||
76 | |||
77 | #if _WITH_SOCKET |
||
78 | case XIOWRITE_SENDTO: |
||
79 | /*union { |
||
80 | char space[sizeof(struct sockaddr_un)]; |
||
81 | struct sockaddr sa; |
||
82 | } from;*/ |
||
83 | /*socklen_t fromlen;*/ |
||
84 | |||
85 | do { |
||
86 | writt = Sendto(fd, buff, bytes, 0, |
||
87 | &pipe->peersa.soa, pipe->salen); |
||
88 | } while (writt < 0 && errno == EINTR); |
||
89 | if (writt < 0) { |
||
90 | char infobuff[256]; |
||
91 | _errno = errno; |
||
92 | Error6("sendto(%d, %p, "F_Zu", 0, %s, "F_socklen"): %s", |
||
93 | fd, buff, bytes, |
||
94 | sockaddr_info(&pipe->peersa.soa, pipe->salen, |
||
95 | infobuff, sizeof(infobuff)), |
||
96 | pipe->salen, strerror(_errno)); |
||
97 | errno = _errno; |
||
98 | return -1; |
||
99 | } |
||
100 | if ((size_t)writt < bytes) { |
||
101 | char infobuff[256]; |
||
102 | Warn7("sendto(%d, %p, "F_Zu", 0, %s, "F_socklen") only wrote "F_Zu" of "F_Zu" bytes", |
||
103 | fd, buff, bytes, |
||
104 | sockaddr_info(&pipe->peersa.soa, pipe->salen, |
||
105 | infobuff, sizeof(infobuff)), |
||
106 | pipe->salen, writt, bytes); |
||
107 | } else { |
||
108 | } |
||
109 | { |
||
110 | char infobuff[256]; |
||
111 | union sockaddr_union us; |
||
112 | socklen_t uslen = sizeof(us); |
||
113 | Getsockname(fd, &us.soa, &uslen); |
||
114 | Notice1("local address: %s", |
||
115 | sockaddr_info(&us.soa, uslen, infobuff, sizeof(infobuff))); |
||
116 | } |
||
117 | break; |
||
118 | #endif /* _WITH_SOCKET */ |
||
119 | |||
120 | case XIOWRITE_PIPE: |
||
121 | case XIOWRITE_2PIPE: |
||
122 | writt = Write(pipe->wfd, buff, bytes); |
||
123 | _errno = errno; |
||
124 | if (writt < 0) { |
||
125 | Error4("write(%d, %p, "F_Zu"): %s", |
||
126 | fd, buff, bytes, strerror(_errno)); |
||
127 | errno = _errno; |
||
128 | return -1; |
||
129 | } |
||
130 | break; |
||
131 | |||
132 | #if WITH_TEST |
||
133 | case XIOWRITE_TEST: |
||
134 | /* this function prints its own error messages */ |
||
135 | return xiowrite_test(pipe, buff, bytes); |
||
136 | case XIOWRITE_TESTREV: |
||
137 | /* this function prints its own error messages */ |
||
138 | return xiowrite_testrev(pipe, buff, bytes); |
||
139 | #endif /* WITH_TEST */ |
||
140 | |||
141 | #if WITH_OPENSSL |
||
142 | case XIOWRITE_OPENSSL: |
||
143 | /* this function prints its own error messages */ |
||
144 | return xiowrite_openssl(pipe, buff, bytes); |
||
145 | #endif /* WITH_OPENSSL */ |
||
146 | |||
147 | default: |
||
148 | Error1("xiowrite(): bad data type specification %d", pipe->dtype); |
||
149 | errno = EINVAL; |
||
150 | return -1; |
||
151 | } |
||
152 | return writt; |
||
153 | } |