]>
git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/helper/helper_comm.c
2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <sys/types.h>
27 #include <CoreFoundation/CoreFoundation.h>
28 #include "helper_comm.h"
30 #include <SystemConfiguration/SCPrivate.h>
34 readn(int ref
, void *data
, size_t len
)
41 if ((n
= read(ref
, p
, left
)) == -1) {
58 writen(int ref
, const void *data
, size_t len
)
65 if ((n
= write(ref
, p
, left
)) == -1) {
79 __SCHelper_txMessage(int fd
, uint32_t msgID
, CFDataRef data
)
85 header
[1] = (data
!= NULL
) ? CFDataGetLength(data
) : 0;
87 n_written
= writen(fd
, header
, sizeof(header
));
88 if (n_written
!= sizeof(header
)) {
89 if ((n_written
== -1) && (errno
!= EPIPE
)) {
90 perror("write() failed while sending msgID");
100 n_written
= writen(fd
, CFDataGetBytePtr(data
), header
[1]);
101 if (n_written
!= header
[1]) {
102 if ((n_written
== -1) && (errno
!= EPIPE
)) {
103 perror("write() failed while sending data");
112 __SCHelper_rxMessage(int fd
, uint32_t *msgID
, CFDataRef
*data
)
118 n_read
= readn(fd
, header
, sizeof(header
));
119 if (n_read
!= sizeof(header
)) {
121 perror("read() failed while reading msgID");
130 if (header
[1] == 0) {
135 } else if ((int32_t)header
[1] < 0) {
136 perror("read() failed, invalid data length");
140 bytes
= CFAllocatorAllocate(NULL
, header
[1], 0);
141 n_read
= readn(fd
, bytes
, header
[1]);
142 if (n_read
!= header
[1]) {
144 perror("read() failed while reading data");
146 CFAllocatorDeallocate(NULL
, bytes
);
151 *data
= CFDataCreateWithBytesNoCopy(NULL
, bytes
, header
[1], NULL
);
154 CFAllocatorDeallocate(NULL
, bytes
);
162 _SCHelperExec(int fd
, uint32_t msgID
, CFDataRef data
, uint32_t *status
, CFDataRef
*reply
)
166 ok
= __SCHelper_txMessage(fd
, msgID
, data
);
171 if ((status
== NULL
) && (reply
== NULL
)) {
172 // if no reply expected (one way)
176 ok
= __SCHelper_rxMessage(fd
, status
, reply
);