<commit_before>// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package rt

import (
	"fmt"
	"os"
	"os/user"
	"strconv"
	"syscall"

	"v.io/v23/context"
	"v.io/v23/naming"
	"v.io/v23/security"
	"v.io/v23/verror"
	"v.io/x/ref/envvar"
	"v.io/x/ref/lib/exec"
	"v.io/x/ref/lib/mgmt"
	vsecurity "v.io/x/ref/lib/security"
	inaming "v.io/x/ref/profiles/internal/naming"
	"v.io/x/ref/services/agent/agentlib"
)

func (r *Runtime) initPrincipal(ctx *context.T, credentials string) (principal security.Principal, deps []interface{}, err error) {
	if principal, _ = ctx.Value(principalKey).(security.Principal); principal != nil {
		return principal, nil, nil
	}
	if len(credentials) > 0 {
		// Explicitly specified credentials, ignore the agent.
		if _, fd, _ := agentEP(); fd >= 0 {
			syscall.Close(fd)
		}
		// TODO(ataly, ashankar): If multiple runtimes are getting
		// initialized at the same time from the same
		// envvar.Credentials we will need some kind of locking for the
		// credential files.
		if principal, err = vsecurity.LoadPersistentPrincipal(credentials, nil); err != nil {
			if os.IsNotExist(err) {
				if principal, err = vsecurity.CreatePersistentPrincipal(credentials, nil); err != nil {
					return principal, nil, err
				}
				return principal, nil, vsecurity.InitDefaultBlessings(principal, defaultBlessingName())
			}
			return nil, nil, err
		}
		return principal, nil, nil
	}
	// Use credentials stored in the agent.
	if ep, _, err := agentEP(); err != nil {
		return nil, nil, err
	} else if ep != nil {
		// Use a new stream manager and an "incomplete" client (the
		// principal is nil) to talk to the agent.
		//
		// The lack of a principal works out for the rpc.Client
		// only because the agent uses anonymous unix sockets and
		// the SecurityNone option.
		//
		// Using a distinct stream manager to manage agent-related
		// connections helps isolate these connections to the agent
		// from management of any other connections created in the
		// process (such as future RPCs to other services).
		if ctx, err = r.WithNewStreamManager(ctx); err != nil {
			return nil, nil, err
		}
		client := r.GetClient(ctx)
		if principal, err = agentlib.NewAgentPrincipal(ctx, ep, client); err != nil {
			client.Close()
			return nil, nil, err
		}
		return principal, []interface{}{client}, nil
	}
	// No agent, no explicit credentials specified: - create a new principal and blessing in memory.
	if principal, err = vsecurity.NewPrincipal(); err != nil {
		return principal, nil, err
	}
	return principal, nil, vsecurity.InitDefaultBlessings(principal, defaultBlessingName())
}

func parseAgentFD(ep naming.Endpoint) (int, error) {
	fd := ep.Addr().String()
	ifd, err := strconv.Atoi(fd)<commit_msg>rt: Reparent the agents context to the root.<commit_after>// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package rt

import (
	"fmt"
	"os"
	"os/user"
	"strconv"
	"syscall"

	"v.io/v23/context"
	"v.io/v23/naming"
	"v.io/v23/security"
	"v.io/v23/verror"
	"v.io/x/ref/envvar"
	"v.io/x/ref/lib/exec"
	"v.io/x/ref/lib/mgmt"
	vsecurity "v.io/x/ref/lib/security"
	inaming "v.io/x/ref/profiles/internal/naming"
	"v.io/x/ref/services/agent/agentlib"
)

func (r *Runtime) initPrincipal(ctx *context.T, credentials string) (principal security.Principal, deps []interface{}, err error) {
	if principal, _ = ctx.Value(principalKey).(security.Principal); principal != nil {
		return principal, nil, nil
	}
	if len(credentials) > 0 {
		// Explicitly specified credentials, ignore the agent.
		if _, fd, _ := agentEP(); fd >= 0 {
			syscall.Close(fd)
		}
		// TODO(ataly, ashankar): If multiple runtimes are getting
		// initialized at the same time from the same
		// envvar.Credentials we will need some kind of locking for the
		// credential files.
		if principal, err = vsecurity.LoadPersistentPrincipal(credentials, nil); err != nil {
			if os.IsNotExist(err) {
				if principal, err = vsecurity.CreatePersistentPrincipal(credentials, nil); err != nil {
					return principal, nil, err
				}
				return principal, nil, vsecurity.InitDefaultBlessings(principal, defaultBlessingName())
			}
			return nil, nil, err
		}
		return principal, nil, nil
	}
	// Use credentials stored in the agent.
	if ep, _, err := agentEP(); err != nil {
		return nil, nil, err
	} else if ep != nil {
		// Use a new stream manager and an "incomplete" client (the
		// principal is nil) to talk to the agent.
		//
		// The lack of a principal works out for the rpc.Client
		// only because the agent uses anonymous unix sockets and
		// the SecurityNone option.
		//
		// Using a distinct stream manager to manage agent-related
		// connections helps isolate these connections to the agent
		// from management of any other connections created in the
		// process (such as future RPCs to other services).
		if ctx, err = r.WithNewStreamManager(ctx); err != nil {
			return nil, nil, err
		}
		client := r.GetClient(ctx)

		// We reparent the context we use to construct the agent.
		// We do this because the agent needs to be able to make RPCs
		// during runtime shutdown.
		ctx, _ = context.WithRootCancel(ctx)

		if principal, err = agentlib.NewAgentPrincipal(ctx, ep, client); err != nil {
			client.Close()
			return nil, nil, err
		}
		return principal, []interface{}{client}, nil
	}
	// No agent, no explicit credentials specified: - create a new principal and blessing in memory.
	if principal, err = vsecurity.NewPrincipal(); err != nil {
		return principal, nil, err
	}
	return principal, nil, vsecurity.InitDefaultBlessings(principal, defaultBlessingName())
}

func parseAgentFD(ep naming.Endpoint) (int, error) {
	fd := ep.Addr().String()
	ifd, err := strconv.Atoi(fd)
{'fraction_non_alphanumeric': 0.11903149339654588, 'fraction_numerical': 0.004740941415509651, 'mean_word_length': 3.4648526077097506, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6624253', 'n_tokens_mistral': 2065, 'n_tokens_neox': 1887, 'n_words': 646}