this repo has no description
at fixPythonPipStalling 190 lines 5.8 kB view raw
1/* 2 * Copyright (c) 2017 Apple Inc. All rights reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 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 11 * file. 12 * 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. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23 24/* 25 * Modification History 26 * 27 * May 18, 2017 Allan Nathanson <ajn@apple.com> 28 * - initial revision 29 */ 30 31#define _LIBLOG_SYSTEMCONFIGURATION_ 32 33#import <Foundation/Foundation.h> 34#import <os/log_private.h> 35#import <os/state_private.h> 36#import <string.h> 37 38#import <dnsinfo.h> 39#import "dnsinfo_internal.h" 40#import <network_information.h> 41 42#define my_log(__level, __format, ...) [string appendFormat:@(__format "\n"), ## __VA_ARGS__] 43#define my_log_context_type NSMutableString * 44#define my_log_context_name string 45#import "dnsinfo_logging.h" 46#import "network_state_information_logging.h" 47#undef my_log_context_name 48#undef my_log_context_type 49#undef my_log 50 51#define SCAS(str) \ 52 [[NSAttributedString alloc] initWithString:str] 53 54#define SCASWithFormat(format, ...) \ 55 SCAS(([[NSString alloc] initWithFormat:format, ##__VA_ARGS__])) 56 57#pragma mark - 58#pragma mark os_log formatting entry point 59 60struct SC_OSLog_Formatters { 61 const char *type; 62 NS_RETURNS_RETAINED NSAttributedString * (*function)(id value); 63}; 64 65NS_RETURNS_RETAINED 66NSAttributedString * 67OSLogCopyFormattedString(const char *type, id value, os_log_type_info_t info) 68{ 69#pragma unused(info) 70 // add functions for each type into this list 71 static const struct SC_OSLog_Formatters formatters[] = { 72// { .type = "???", .function = _SC_OSLogCopyFormattedString_??? }, 73 }; 74 75 for (int i = 0; i < (int)(sizeof(formatters) / sizeof(formatters[0])); i++) { 76 if (strcmp(type, formatters[i].type) == 0) { 77 return formatters[i].function(value); 78 } 79 } 80 81 return SCASWithFormat(@"liblog_SystemConfiguration: Not yet supported os_log formatting type: %s", type); 82} 83 84#pragma mark - 85#pragma mark os_state formatting entry point 86 87#define SCNS(str) \ 88 [[NSString alloc] initWithString:(str)] 89 90#define SCNSWithFormat(format, ...) \ 91 [[NSString alloc] initWithFormat:format, ##__VA_ARGS__] 92 93static NS_RETURNS_RETAINED NSString * 94_SC_OSStateCopyFormattedString_dnsinfo(uint32_t data_size, void *data) 95{ 96 dns_config_t *dns_config = NULL; 97 _dns_config_buf_t *dns_config_buf; 98 NSMutableString *string; 99 100 // os_state_add_handler w/ 101 // osd_type = OS_STATE_DATA_CUSTOM 102 // osd_decoder.osdd_library = "SystemConfiguration 103 // osd_decoder.osdd_type = "dnsinfo" 104 105 if ((data_size == 0) || (data == NULL)) { 106 return @"No DNS configuration"; 107 } else if (data_size < sizeof(_dns_config_buf_t)) { 108 return SCNSWithFormat(@"DNS configuration: size error (%d < %zd)", 109 data_size, 110 sizeof(_dns_config_buf_t)); 111 } 112 113 dns_config_buf = _dns_configuration_buffer_create(data, data_size); 114 if (dns_config_buf == NULL) { 115 return @"DNS configuration: data error"; 116 } 117 118 dns_config = _dns_configuration_buffer_expand(dns_config_buf); 119 if (dns_config == NULL) { 120 // if we were unable to expand the configuration 121 _dns_configuration_buffer_free(&dns_config_buf); 122 return @"DNS configuration: expansion error"; 123 } 124 125 string = [NSMutableString string]; 126 _dns_configuration_log(dns_config, TRUE, string); 127 if (string.length == 0) { 128 [string appendString:@"DNS configuration: not available"]; 129 } 130 free(dns_config); 131 132 return string; 133} 134 135static NS_RETURNS_RETAINED NSString * 136_SC_OSStateCopyFormattedString_nwi(uint32_t data_size, void *data) 137{ 138 nwi_state_t state = (nwi_state_t)data; 139 NSMutableString *string; 140 141 // os_state_add_handler w/ 142 // osd_type = OS_STATE_DATA_CUSTOM 143 // osd_decoder.osdd_library = "SystemConfiguration 144 // osd_decoder.osdd_type = "nwi" 145 146 if ((data_size == 0) || (data == NULL)) { 147 return @"No network information"; 148 } else if (data_size < sizeof(nwi_state)) { 149 return SCNSWithFormat(@"Network information: size error (%d < %zd)", 150 data_size, 151 sizeof(_dns_config_buf_t)); 152 } else if (state->version != NWI_STATE_VERSION) { 153 return SCNSWithFormat(@"Network information: version error (%d != %d)", 154 state->version, 155 NWI_STATE_VERSION); 156 } 157 158 string = [NSMutableString string]; 159 _nwi_state_log(state, TRUE, string); 160 if (string.length == 0) { 161 [string appendString:@"Network information: not available"]; 162 } 163 164 return string; 165} 166 167struct SC_OSState_Formatters { 168 const char *type; 169 NS_RETURNS_RETAINED NSString * (*function)(uint32_t data_size, void *data); 170}; 171 172NS_RETURNS_RETAINED 173NSString * 174OSStateCreateStringWithData(const char *type, uint32_t data_size, void *data) 175{ 176 // add functions for each type into this list 177 static const struct SC_OSState_Formatters formatters[] = { 178 { .type = "dnsinfo", .function = _SC_OSStateCopyFormattedString_dnsinfo }, 179 { .type = "nwi", .function = _SC_OSStateCopyFormattedString_nwi }, 180 }; 181 182 for (int i = 0; i < (int)(sizeof(formatters) / sizeof(formatters[0])); i++) { 183 if (strcmp(type, formatters[i].type) == 0) { 184 return formatters[i].function(data_size, data); 185 } 186 } 187 188 return SCNSWithFormat(@"liblog_SystemConfiguration: Not yet supported os_state formatting type: %s", type); 189} 190