Text file src/runtime/cgo/gcc_ios_arm64.c

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  #include <limits.h>
     6  #include <string.h> /* for strerror */
     7  #include <sys/param.h>
     8  #include <unistd.h>
     9  #include <stdlib.h>
    10  
    11  #include "libcgo.h"
    12  
    13  #include <CoreFoundation/CFBundle.h>
    14  #include <CoreFoundation/CFString.h>
    15  
    16  // init_working_dir sets the current working directory to the app root.
    17  // By default ios/arm64 processes start in "/".
    18  static void
    19  init_working_dir()
    20  {
    21  	CFBundleRef bundle;
    22  	CFURLRef url_ref;
    23  	CFStringRef url_str_ref;
    24  	char buf[MAXPATHLEN];
    25  	Boolean res;
    26  	int url_len;
    27  	char *dir;
    28  	CFStringRef wd_ref;
    29  
    30  	bundle = CFBundleGetMainBundle();
    31  	if (bundle == NULL) {
    32  		fprintf(stderr, "runtime/cgo: no main bundle\n");
    33  		return;
    34  	}
    35  	url_ref = CFBundleCopyResourceURL(bundle, CFSTR("Info"), CFSTR("plist"), NULL);
    36  	if (url_ref == NULL) {
    37  		// No Info.plist found. It can happen on Corellium virtual devices.
    38  		return;
    39  	}
    40  	url_str_ref = CFURLGetString(url_ref);
    41  	res = CFStringGetCString(url_str_ref, buf, sizeof(buf), kCFStringEncodingUTF8);
    42  	CFRelease(url_ref);
    43  	if (!res) {
    44  		fprintf(stderr, "runtime/cgo: cannot get URL string\n");
    45  		return;
    46  	}
    47  
    48  	// url is of the form "file:///path/to/Info.plist".
    49  	// strip it down to the working directory "/path/to".
    50  	url_len = strlen(buf);
    51  	if (url_len < sizeof("file://")+sizeof("/Info.plist")) {
    52  		fprintf(stderr, "runtime/cgo: bad URL: %s\n", buf);
    53  		return;
    54  	}
    55  	buf[url_len-sizeof("/Info.plist")+1] = 0;
    56  	dir = &buf[0] + sizeof("file://")-1;
    57  
    58  	if (chdir(dir) != 0) {
    59  		fprintf(stderr, "runtime/cgo: chdir(%s) failed\n", dir);
    60  	}
    61  
    62  	// The test harness in go_ios_exec passes the relative working directory
    63  	// in the GoExecWrapperWorkingDirectory property of the app bundle.
    64  	wd_ref = CFBundleGetValueForInfoDictionaryKey(bundle, CFSTR("GoExecWrapperWorkingDirectory"));
    65  	if (wd_ref != NULL) {
    66  		if (!CFStringGetCString(wd_ref, buf, sizeof(buf), kCFStringEncodingUTF8)) {
    67  			fprintf(stderr, "runtime/cgo: cannot get GoExecWrapperWorkingDirectory string\n");
    68  			return;
    69  		}
    70  		if (chdir(buf) != 0) {
    71  			fprintf(stderr, "runtime/cgo: chdir(%s) failed\n", buf);
    72  		}
    73  	}
    74  }
    75  
    76  static void
    77  init_platform()
    78  {
    79  	init_working_dir();
    80  }
    81  
    82  void (*x_cgo_init_platform)(void) = init_platform;
    83  

View as plain text