{"action":"created","comment":{"url":"https://api.github.com/repos/lxc/lxd/pulls/comments/176229631","pull_request_review_id":105897395,"id":176229631,"diff_hunk":"@@ -0,0 +1,266 @@\n+package main\n+\n+import (\n+\t\"fmt\"\n+\t\"io\"\n+\t\"net\"\n+\t\"os\"\n+\t\"strconv\"\n+\t\"strings\"\n+\t\"syscall\"\n+\n+\t\"github.com/spf13/cobra\"\n+\n+\t\"github.com/lxc/lxd/lxd/util\"\n+\t\"github.com/lxc/lxd/shared\"\n+)\n+\n+/*\n+#define _GNU_SOURCE\n+#include <errno.h>\n+#include <stdbool.h>\n+#include <stdio.h>\n+#include <stdlib.h>\n+#include <string.h>\n+#include <unistd.h>\n+\n+extern bool advance_arg(char *buf, char *cur, ssize_t size, bool required);\n+extern int dosetns(int pid, char *nstype);\n+\n+void forkproxy(char *buf, char *cur, ssize_t size) {\n+\tint cmdline, listen_pid, connect_pid, fdnum, forked, childPid, ret;\n+\tchar fdpath[80];\n+\tchar *logPath = NULL, *pidPath = NULL;\n+\tFILE *logFile = NULL, *pidFile = NULL;\n+\n+\t// Get the arguments\n+\tadvance_arg(buf, cur, size, true);\n+\tlisten_pid = atoi(cur);\n+\tadvance_arg(buf, cur, size, true);\n+\tadvance_arg(buf, cur, size, true);\n+\tconnect_pid = atoi(cur);\n+\tadvance_arg(buf, cur, size, true);\n+\tadvance_arg(buf, cur, size, true);\n+\tfdnum = atoi(cur);\n+\tadvance_arg(buf, cur, size, true);\n+\tforked = atoi(cur);\n+\tadvance_arg(buf, cur, size, true);\n+\tlogPath = cur;\n+\tadvance_arg(buf, cur, size, true);\n+\tpidPath = cur;\n+\n+\t// Check if proxy daemon already forked\n+\tif (forked == 0) {\n+\t\tlogFile = fopen(logPath, \"w+\");\n+\t\tif (logFile == NULL) {\n+\t\t\t_exit(1);\n+\t\t}\n+\n+\t\tif (dup2(fileno(logFile), STDOUT_FILENO) < 0) {\n+\t\t\tfprintf(logFile, \"Failed to redirect STDOUT to logfile: %s\\n\", strerror(errno));\n+\t\t\t_exit(1);\n+\t\t}\n+\t\tif (dup2(fileno(logFile), STDERR_FILENO) < 0) {\n+\t\t\tfprintf(logFile, \"Failed to redirect STDERR to logfile: %s\\n\", strerror(errno));\n+\t\t\t_exit(1);\n+\t\t}\n+\t\tfclose(logFile);\n+\n+\t\tpidFile = fopen(pidPath, \"w+\");\n+\t\tif (pidFile == NULL) {\n+\t\t\tfprintf(stderr, \"Failed to create pid file for proxy daemon: %s\\n\", strerror(errno));\n+\t\t\t_exit(1);\n+\t\t}\n+\n+\t\tchildPid = fork();\n+\t\tif (childPid < 0) {\n+\t\t\tfprintf(stderr, \"Failed to fork proxy daemon: %s\\n\", strerror(errno));\n+\t\t\t_exit(1);\n+\t\t} else if (childPid != 0) {\n+\t\t\tfprintf(pidFile, \"%d\", childPid);\n+\t\t\tfclose(pidFile);\n+\t\t\tfclose(stdin);\n+\t\t\tfclose(stdout);\n+\t\t\tfclose(stderr);\n+\t\t\t_exit(0);\n+\t\t} else {\n+\t\t\tret = setsid();\n+\t\t\tif (ret < 0) {\n+\t\t\t\tfprintf(stderr, \"Failed to setsid in proxy daemon: %s\\n\", strerror(errno));\n+\t\t\t\t_exit(1);\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\t// Cannot pass through -1 to runCommand since it is interpreted as a flag\n+\tfdnum = fdnum == 0 ? -1 : fdnum;\n+\n+\tret = snprintf(fdpath, sizeof(fdpath), \"/proc/self/fd/%d\", fdnum);\n+\tif (ret < 0 || (size_t)ret >= sizeof(fdpath)) {\n+\t\tfprintf(stderr, \"Failed to format file descriptor path\\n\");\n+\t\t_exit(1);\n+\t}\n+\n+\t// Join the listener ns if not already setup\n+\tif (access(fdpath, F_OK) < 0) {\n+\t\t// Attach to the network namespace of the listener\n+\t\tif (dosetns(listen_pid, \"net\") < 0) {\n+\t\t\tfprintf(stderr, \"Failed setns to listener network namespace: %s\\n\", strerror(errno));\n+\t\t\t_exit(1);\n+\t\t}\n+\t} else {\n+\t\t// Join the connector ns now\n+\t\tif (dosetns(connect_pid, \"net\") < 0) {\n+\t\t\tfprintf(stderr, \"Failed setns to connector network namespace: %s\\n\", strerror(errno));\n+\t\t\t_exit(1);\n+\t\t}\n+\t}\n+}\n+*/\n+import \"C\"\n+\n+type cmdForkproxy struct {\n+\tcmd    *cobra.Command\n+\tglobal *cmdGlobal\n+}\n+\n+func (c *cmdForkproxy) Command() *cobra.Command {\n+\t// Main subcommand\n+\tcmd := &cobra.Command{}\n+\tcmd.Use = \"forkproxy <listen PID> <listen address> <connect PID> <connect address> <fd> <reexec> <log path> <pid path>\"\n+\tcmd.Short = \"Setup network connection proxying\"\n+\tcmd.Long = `Description:\n+  Setup network connection proxying\n+\n+  This internal command will spawn a new proxy process for a particular\n+  container, connecting one side to the host and the other to the\n+  container.\n+`\n+\tcmd.RunE = c.Run\n+\tcmd.Hidden = true\n+\n+\tc.cmd = cmd\n+\treturn cmd\n+}\n+\n+func (c *cmdForkproxy) Run(cmd *cobra.Command, args []string) error {\n+\tif len(args) != 8 {\n+\t\tcmd.Help()\n+\n+\t\tif len(args) == 0 {\n+\t\t\treturn nil\n+\t\t}\n+\n+\t\treturn fmt.Errorf(\"Missing required arguments\")\n+\t}\n+\n+\t// Get all our arguments\n+\tlistenPid := args[0]\n+\tlistenAddr := args[1]\n+\tconnectPid := args[2]\n+\tconnectAddr := args[3]\n+\n+\tfd := -1\n+\tif args[4] != \"0\" {\n+\t\tfd, _ = strconv.Atoi(args[4])\n+\t}\n+\n+\t// At this point we have already forked and should set this flag to 1\n+\targs[5] = \"1\"\n+\n+\t// Check where we are in initialization\n+\tif !shared.PathExists(fmt.Sprintf(\"/proc/self/fd/%d\", fd)) {\n+\t\tfmt.Printf(\"Listening on %s in %s, forwarding to %s from %s\\n\", listenAddr, listenPid, connectAddr, connectPid)\n+\n+\t\tfile, err := getListenerFile(listenAddr)\n+\t\tif err != nil {\n+\t\t\treturn err\n+\t\t}\n+\t\tdefer file.Close()\n+\n+\t\tlistenerFd := file.Fd()\n+\t\tif err != nil {\n+\t\t\treturn fmt.Errorf(\"failed to duplicate the listener fd: %v\", err)","path":"lxd/main_forkproxy.go","position":191,"original_position":183,"commit_id":"7fcfad8f6eb859c90313219c213d31f8c48035ab","original_commit_id":"1a6722d88780b41dae796b49e5a2f17bc6ce350e","user":{"login":"stgraber","id":2230599,"avatar_url":"https://avatars0.githubusercontent.com/u/2230599?v=4","gravatar_id":"","url":"https://api.github.com/users/stgraber","html_url":"https://github.com/stgraber","followers_url":"https://api.github.com/users/stgraber/followers","following_url":"https://api.github.com/users/stgraber/following{/other_user}","gists_url":"https://api.github.com/users/stgraber/gists{/gist_id}","starred_url":"https://api.github.com/users/stgraber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stgraber/subscriptions","organizations_url":"https://api.github.com/users/stgraber/orgs","repos_url":"https://api.github.com/users/stgraber/repos","events_url":"https://api.github.com/users/stgraber/events{/privacy}","received_events_url":"https://api.github.com/users/stgraber/received_events","type":"User","site_admin":false},"body":"Fixed them","created_at":"2018-03-21T20:37:13Z","updated_at":"2018-03-21T20:37:13Z","html_url":"https://github.com/lxc/lxd/pull/4342#discussion_r176229631","pull_request_url":"https://api.github.com/repos/lxc/lxd/pulls/4342","author_association":"OWNER","_links":{"self":{"href":"https://api.github.com/repos/lxc/lxd/pulls/comments/176229631"},"html":{"href":"https://github.com/lxc/lxd/pull/4342#discussion_r176229631"},"pull_request":{"href":"https://api.github.com/repos/lxc/lxd/pulls/4342"}},"in_reply_to_id":176038248},"pull_request":{"url":"https://api.github.com/repos/lxc/lxd/pulls/4342","id":176384997,"html_url":"https://github.com/lxc/lxd/pull/4342","diff_url":"https://github.com/lxc/lxd/pull/4342.diff","patch_url":"https://github.com/lxc/lxd/pull/4342.patch","issue_url":"https://api.github.com/repos/lxc/lxd/issues/4342","number":4342,"state":"open","locked":false,"title":"Port lxd command to cobra","user":{"login":"stgraber","id":2230599,"avatar_url":"https://avatars0.githubusercontent.com/u/2230599?v=4","gravatar_id":"","url":"https://api.github.com/users/stgraber","html_url":"https://github.com/stgraber","followers_url":"https://api.github.com/users/stgraber/followers","following_url":"https://api.github.com/users/stgraber/following{/other_user}","gists_url":"https://api.github.com/users/stgraber/gists{/gist_id}","starred_url":"https://api.github.com/users/stgraber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stgraber/subscriptions","organizations_url":"https://api.github.com/users/stgraber/orgs","repos_url":"https://api.github.com/users/stgraber/repos","events_url":"https://api.github.com/users/stgraber/events{/privacy}","received_events_url":"https://api.github.com/users/stgraber/received_events","type":"User","site_admin":false},"body":"","created_at":"2018-03-21T05:23:42Z","updated_at":"2018-03-21T20:37:13Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4ec9c606a9eb1578dc44beea70da5acf9e4c52ba","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/lxc/lxd/pulls/4342/commits","review_comments_url":"https://api.github.com/repos/lxc/lxd/pulls/4342/comments","review_comment_url":"https://api.github.com/repos/lxc/lxd/pulls/comments{/number}","comments_url":"https://api.github.com/repos/lxc/lxd/issues/4342/comments","statuses_url":"https://api.github.com/repos/lxc/lxd/statuses/7fcfad8f6eb859c90313219c213d31f8c48035ab","head":{"label":"stgraber:master","ref":"master","sha":"7fcfad8f6eb859c90313219c213d31f8c48035ab","user":{"login":"stgraber","id":2230599,"avatar_url":"https://avatars0.githubusercontent.com/u/2230599?v=4","gravatar_id":"","url":"https://api.github.com/users/stgraber","html_url":"https://github.com/stgraber","followers_url":"https://api.github.com/users/stgraber/followers","following_url":"https://api.github.com/users/stgraber/following{/other_user}","gists_url":"https://api.github.com/users/stgraber/gists{/gist_id}","starred_url":"https://api.github.com/users/stgraber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stgraber/subscriptions","organizations_url":"https://api.github.com/users/stgraber/orgs","repos_url":"https://api.github.com/users/stgraber/repos","events_url":"https://api.github.com/users/stgraber/events{/privacy}","received_events_url":"https://api.github.com/users/stgraber/received_events","type":"User","site_admin":false},"repo":{"id":26225592,"name":"lxd","full_name":"stgraber/lxd","owner":{"login":"stgraber","id":2230599,"avatar_url":"https://avatars0.githubusercontent.com/u/2230599?v=4","gravatar_id":"","url":"https://api.github.com/users/stgraber","html_url":"https://github.com/stgraber","followers_url":"https://api.github.com/users/stgraber/followers","following_url":"https://api.github.com/users/stgraber/following{/other_user}","gists_url":"https://api.github.com/users/stgraber/gists{/gist_id}","starred_url":"https://api.github.com/users/stgraber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stgraber/subscriptions","organizations_url":"https://api.github.com/users/stgraber/orgs","repos_url":"https://api.github.com/users/stgraber/repos","events_url":"https://api.github.com/users/stgraber/events{/privacy}","received_events_url":"https://api.github.com/users/stgraber/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/stgraber/lxd","description":"Personal work copy of lxc/lxd","fork":true,"url":"https://api.github.com/repos/stgraber/lxd","forks_url":"https://api.github.com/repos/stgraber/lxd/forks","keys_url":"https://api.github.com/repos/stgraber/lxd/keys{/key_id}","collaborators_url":"https://api.github.com/repos/stgraber/lxd/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/stgraber/lxd/teams","hooks_url":"https://api.github.com/repos/stgraber/lxd/hooks","issue_events_url":"https://api.github.com/repos/stgraber/lxd/issues/events{/number}","events_url":"https://api.github.com/repos/stgraber/lxd/events","assignees_url":"https://api.github.com/repos/stgraber/lxd/assignees{/user}","branches_url":"https://api.github.com/repos/stgraber/lxd/branches{/branch}","tags_url":"https://api.github.com/repos/stgraber/lxd/tags","blobs_url":"https://api.github.com/repos/stgraber/lxd/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/stgraber/lxd/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/stgraber/lxd/git/refs{/sha}","trees_url":"https://api.github.com/repos/stgraber/lxd/git/trees{/sha}","statuses_url":"https://api.github.com/repos/stgraber/lxd/statuses/{sha}","languages_url":"https://api.github.com/repos/stgraber/lxd/languages","stargazers_url":"https://api.github.com/repos/stgraber/lxd/stargazers","contributors_url":"https://api.github.com/repos/stgraber/lxd/contributors","subscribers_url":"https://api.github.com/repos/stgraber/lxd/subscribers","subscription_url":"https://api.github.com/repos/stgraber/lxd/subscription","commits_url":"https://api.github.com/repos/stgraber/lxd/commits{/sha}","git_commits_url":"https://api.github.com/repos/stgraber/lxd/git/commits{/sha}","comments_url":"https://api.github.com/repos/stgraber/lxd/comments{/number}","issue_comment_url":"https://api.github.com/repos/stgraber/lxd/issues/comments{/number}","contents_url":"https://api.github.com/repos/stgraber/lxd/contents/{+path}","compare_url":"https://api.github.com/repos/stgraber/lxd/compare/{base}...{head}","merges_url":"https://api.github.com/repos/stgraber/lxd/merges","archive_url":"https://api.github.com/repos/stgraber/lxd/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/stgraber/lxd/downloads","issues_url":"https://api.github.com/repos/stgraber/lxd/issues{/number}","pulls_url":"https://api.github.com/repos/stgraber/lxd/pulls{/number}","milestones_url":"https://api.github.com/repos/stgraber/lxd/milestones{/number}","notifications_url":"https://api.github.com/repos/stgraber/lxd/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/stgraber/lxd/labels{/name}","releases_url":"https://api.github.com/repos/stgraber/lxd/releases{/id}","deployments_url":"https://api.github.com/repos/stgraber/lxd/deployments","created_at":"2014-11-05T15:24:41Z","updated_at":"2018-03-21T05:26:00Z","pushed_at":"2018-03-21T18:09:19Z","git_url":"git://github.com/stgraber/lxd.git","ssh_url":"git@github.com:stgraber/lxd.git","clone_url":"https://github.com/stgraber/lxd.git","svn_url":"https://github.com/stgraber/lxd","homepage":"","size":15833,"stargazers_count":3,"watchers_count":3,"language":"Go","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0"},"forks":0,"open_issues":0,"watchers":3,"default_branch":"master"}},"base":{"label":"lxc:master","ref":"master","sha":"bde029e8577d9d2f87d3084b7aef824906fc1e1d","user":{"login":"lxc","id":2301756,"avatar_url":"https://avatars2.githubusercontent.com/u/2301756?v=4","gravatar_id":"","url":"https://api.github.com/users/lxc","html_url":"https://github.com/lxc","followers_url":"https://api.github.com/users/lxc/followers","following_url":"https://api.github.com/users/lxc/following{/other_user}","gists_url":"https://api.github.com/users/lxc/gists{/gist_id}","starred_url":"https://api.github.com/users/lxc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lxc/subscriptions","organizations_url":"https://api.github.com/users/lxc/orgs","repos_url":"https://api.github.com/users/lxc/repos","events_url":"https://api.github.com/users/lxc/events{/privacy}","received_events_url":"https://api.github.com/users/lxc/received_events","type":"Organization","site_admin":false},"repo":{"id":26192117,"name":"lxd","full_name":"lxc/lxd","owner":{"login":"lxc","id":2301756,"avatar_url":"https://avatars2.githubusercontent.com/u/2301756?v=4","gravatar_id":"","url":"https://api.github.com/users/lxc","html_url":"https://github.com/lxc","followers_url":"https://api.github.com/users/lxc/followers","following_url":"https://api.github.com/users/lxc/following{/other_user}","gists_url":"https://api.github.com/users/lxc/gists{/gist_id}","starred_url":"https://api.github.com/users/lxc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lxc/subscriptions","organizations_url":"https://api.github.com/users/lxc/orgs","repos_url":"https://api.github.com/users/lxc/repos","events_url":"https://api.github.com/users/lxc/events{/privacy}","received_events_url":"https://api.github.com/users/lxc/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/lxc/lxd","description":"Daemon based on liblxc offering a REST API to manage containers","fork":false,"url":"https://api.github.com/repos/lxc/lxd","forks_url":"https://api.github.com/repos/lxc/lxd/forks","keys_url":"https://api.github.com/repos/lxc/lxd/keys{/key_id}","collaborators_url":"https://api.github.com/repos/lxc/lxd/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/lxc/lxd/teams","hooks_url":"https://api.github.com/repos/lxc/lxd/hooks","issue_events_url":"https://api.github.com/repos/lxc/lxd/issues/events{/number}","events_url":"https://api.github.com/repos/lxc/lxd/events","assignees_url":"https://api.github.com/repos/lxc/lxd/assignees{/user}","branches_url":"https://api.github.com/repos/lxc/lxd/branches{/branch}","tags_url":"https://api.github.com/repos/lxc/lxd/tags","blobs_url":"https://api.github.com/repos/lxc/lxd/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/lxc/lxd/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/lxc/lxd/git/refs{/sha}","trees_url":"https://api.github.com/repos/lxc/lxd/git/trees{/sha}","statuses_url":"https://api.github.com/repos/lxc/lxd/statuses/{sha}","languages_url":"https://api.github.com/repos/lxc/lxd/languages","stargazers_url":"https://api.github.com/repos/lxc/lxd/stargazers","contributors_url":"https://api.github.com/repos/lxc/lxd/contributors","subscribers_url":"https://api.github.com/repos/lxc/lxd/subscribers","subscription_url":"https://api.github.com/repos/lxc/lxd/subscription","commits_url":"https://api.github.com/repos/lxc/lxd/commits{/sha}","git_commits_url":"https://api.github.com/repos/lxc/lxd/git/commits{/sha}","comments_url":"https://api.github.com/repos/lxc/lxd/comments{/number}","issue_comment_url":"https://api.github.com/repos/lxc/lxd/issues/comments{/number}","contents_url":"https://api.github.com/repos/lxc/lxd/contents/{+path}","compare_url":"https://api.github.com/repos/lxc/lxd/compare/{base}...{head}","merges_url":"https://api.github.com/repos/lxc/lxd/merges","archive_url":"https://api.github.com/repos/lxc/lxd/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/lxc/lxd/downloads","issues_url":"https://api.github.com/repos/lxc/lxd/issues{/number}","pulls_url":"https://api.github.com/repos/lxc/lxd/pulls{/number}","milestones_url":"https://api.github.com/repos/lxc/lxd/milestones{/number}","notifications_url":"https://api.github.com/repos/lxc/lxd/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/lxc/lxd/labels{/name}","releases_url":"https://api.github.com/repos/lxc/lxd/releases{/id}","deployments_url":"https://api.github.com/repos/lxc/lxd/deployments","created_at":"2014-11-04T22:40:52Z","updated_at":"2018-03-21T04:15:11Z","pushed_at":"2018-03-21T18:09:51Z","git_url":"git://github.com/lxc/lxd.git","ssh_url":"git@github.com:lxc/lxd.git","clone_url":"https://github.com/lxc/lxd.git","svn_url":"https://github.com/lxc/lxd","homepage":"https://linuxcontainers.org/lxd","size":18488,"stargazers_count":1703,"watchers_count":1703,"language":"Go","has_issues":true,"has_projects":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":414,"mirror_url":null,"archived":false,"open_issues_count":45,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0"},"forks":414,"open_issues":45,"watchers":1703,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/lxc/lxd/pulls/4342"},"html":{"href":"https://github.com/lxc/lxd/pull/4342"},"issue":{"href":"https://api.github.com/repos/lxc/lxd/issues/4342"},"comments":{"href":"https://api.github.com/repos/lxc/lxd/issues/4342/comments"},"review_comments":{"href":"https://api.github.com/repos/lxc/lxd/pulls/4342/comments"},"review_comment":{"href":"https://api.github.com/repos/lxc/lxd/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/lxc/lxd/pulls/4342/commits"},"statuses":{"href":"https://api.github.com/repos/lxc/lxd/statuses/7fcfad8f6eb859c90313219c213d31f8c48035ab"}},"author_association":"OWNER"}}
{"actor":{"display_login":"stgraber"}}