-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Environment.GetEnvironmentVariables for Apple platforms using official API on iOS/tvOS/MacCatalyst #58161
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7d697e7
Implement mono_icall_get_environment_variable_names for Apple platfor…
filipnavara 33e5b5d
Move environment handling from Mono runtime to System.Native
filipnavara 5eb8dc2
Revert unnecessary changes
filipnavara 3346036
Split pal_environment, remove HAVE_NSGETENVIRON
filipnavara 0e520ab
Bring back _NSGetEnviron
filipnavara d9f527b
Remove the memory leak
filipnavara d55d4ad
Fix error with unused variable
filipnavara 30b72b4
Add try-finally block
filipnavara 8759796
Match Windows behavior for duplicate keys
filipnavara 64744d6
Update src/libraries/Native/Unix/System.Native/pal_environment.h
filipnavara 316b2f1
Update src/libraries/Native/Unix/System.Native/pal_environment.h
filipnavara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetEnv.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal unsafe partial class Sys | ||
{ | ||
[DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_GetEnv")] | ||
internal static extern unsafe IntPtr GetEnv(string name); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetEnviron.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal unsafe partial class Sys | ||
{ | ||
[DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_GetEnviron")] | ||
internal static extern unsafe IntPtr GetEnviron(); | ||
|
||
[DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_FreeEnviron")] | ||
internal static extern unsafe void FreeEnviron(IntPtr environ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#include "pal_config.h" | ||
#include "pal_environment.h" | ||
|
||
#include <stdlib.h> | ||
#include <string.h> | ||
#if HAVE_NSGETENVIRON | ||
#include <crt_externs.h> | ||
#endif | ||
|
||
char* SystemNative_GetEnv(const char* variable) | ||
{ | ||
return getenv(variable); | ||
} | ||
|
||
char** SystemNative_GetEnviron() | ||
{ | ||
#if HAVE_NSGETENVIRON | ||
return *(_NSGetEnviron()); | ||
#else | ||
extern char **environ; | ||
return environ; | ||
#endif | ||
} | ||
|
||
void SystemNative_FreeEnviron(char** environ) | ||
{ | ||
// no op | ||
(void)environ; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#pragma once | ||
|
||
#include "pal_compiler.h" | ||
#include "pal_types.h" | ||
|
||
PALEXPORT char* SystemNative_GetEnv(const char* variable); | ||
|
||
PALEXPORT char** SystemNative_GetEnviron(void); | ||
|
||
PALEXPORT void SystemNative_FreeEnviron(char** environ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#include "pal_config.h" | ||
#include "pal_environment.h" | ||
|
||
#include <Foundation/Foundation.h> | ||
#include <CoreFoundation/CoreFoundation.h> | ||
#include <objc/runtime.h> | ||
#include <objc/message.h> | ||
|
||
char* SystemNative_GetEnv(const char* variable) | ||
{ | ||
return getenv(variable); | ||
} | ||
|
||
static char *empty_key_value_pair = "="; | ||
|
||
static void get_environ_helper(const void *key, const void *value, void *context) | ||
{ | ||
char ***temp_environ_ptr = (char***)context; | ||
const char *utf8_key = [(NSString *)key UTF8String]; | ||
const char *utf8_value = [(NSString *)value UTF8String]; | ||
int utf8_key_length = strlen(utf8_key); | ||
int utf8_value_length = strlen(utf8_value); | ||
char *key_value_pair; | ||
|
||
key_value_pair = malloc(utf8_key_length + utf8_value_length + 2); | ||
if (key_value_pair != NULL) | ||
{ | ||
strcpy(key_value_pair, utf8_key); | ||
key_value_pair[utf8_key_length] = '='; | ||
strcpy(key_value_pair + utf8_key_length + 1, utf8_value); | ||
} | ||
else | ||
{ | ||
// In case of failed allocation add pointer to preallocated entry. This is | ||
// ignored on the managed side and skipped over in SystemNative_FreeEnviron. | ||
key_value_pair = empty_key_value_pair; | ||
} | ||
|
||
**temp_environ_ptr = key_value_pair; | ||
(*temp_environ_ptr)++; | ||
} | ||
|
||
char** SystemNative_GetEnviron() | ||
{ | ||
char **temp_environ; | ||
char **temp_environ_ptr; | ||
|
||
CFDictionaryRef environment = (CFDictionaryRef)[[NSProcessInfo processInfo] environment]; | ||
int count = CFDictionaryGetCount(environment); | ||
temp_environ = (char **)malloc((count + 1) * sizeof(char *)); | ||
if (temp_environ != NULL) | ||
{ | ||
temp_environ_ptr = temp_environ; | ||
CFDictionaryApplyFunction(environment, get_environ_helper, &temp_environ_ptr); | ||
*temp_environ_ptr = NULL; | ||
} | ||
|
||
return temp_environ; | ||
} | ||
|
||
void SystemNative_FreeEnviron(char** environ) | ||
{ | ||
if (environ != NULL) | ||
{ | ||
for (char** environ_ptr = environ; *environ_ptr != NULL; environ_ptr++) | ||
{ | ||
if (*environ_ptr != empty_key_value_pair) | ||
{ | ||
free(*environ_ptr); | ||
} | ||
} | ||
|
||
free(environ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is straight copy from NativeAOT.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then I think it would be better to have it in shared location
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally I would agree.
I started with a minimal fix because I wanted to limit the impact for backporting to .NET 6 branch.
Then I moved it to System.Native based on chat with @akoeplinger (and I agree that it belongs there). Proper sharing of the code with NativeAOT will need few more changes and I wanted to validate the approach first before proceeding with that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think this is fine for now, we can unify with NativeAOT in main later on.