{"action":"created","comment":{"url":"https://api.github.com/repos/tessel/t2-firmware/pulls/comments/28186476","id":28186476,"diff_hunk":"@@ -0,0 +1,884 @@\n+#define _GNU_SOURCE\n+#include <sys/socket.h>\n+#include <sys/un.h>\n+#include <stdio.h>\n+#include <string.h>\n+#include <stdbool.h>\n+#include <stdint.h>\n+#include <errno.h>\n+#include <poll.h>\n+#include <sys/types.h>\n+#include <signal.h>\n+#include <sys/wait.h>\n+#include <sys/stat.h>\n+#include <fcntl.h>\n+#include <unistd.h>\n+#include <syslog.h>\n+#include <stdlib.h>\n+#include <sys/epoll.h>\n+#include <sys/signalfd.h>\n+\n+enum Commands {\n+    CMD_RESET = 0x0,\n+    CMD_OPEN = 0x1,\n+    CMD_CLOSE = 0x2,\n+    CMD_KILL = 0x3,\n+    CMD_EXIT_STATUS = 0x5,\n+    CMD_CLOSE_ACK = 0x6,\n+\n+    CMD_WRITE_CONTROL = 0x10,\n+    CMD_WRITE_STDIN = 0x11,\n+    CMD_WRITE_STDOUT = 0x12,\n+    CMD_WRITE_STDERR = 0x13,\n+\n+    CMD_ACK_CONTROL = 0x20,\n+    CMD_ACK_STDIN = 0x21,\n+    CMD_ACK_STDOUT = 0x22,\n+    CMD_ACK_STDERR = 0x23,\n+\n+    CMD_CLOSE_CONTROL = 0x30,\n+    CMD_CLOSE_STDIN = 0x31,\n+    CMD_CLOSE_STDOUT = 0x32,\n+    CMD_CLOSE_STDERR = 0x33,\n+};\n+\n+#define debug(args...)  syslog(LOG_DEBUG, args)\n+#define info(args...)   syslog(LOG_INFO, args)\n+#define error(args...)  syslog(LOG_ERR, args)\n+#define fatal(args...) ({ \\\n+    syslog (LOG_CRIT, args); \\\n+    exit(1); \\\n+})\n+\n+int sock = 0;\n+int epfd   = 0;\n+int sigfd  = 0;\n+\n+#define PIPE_BUF 4096\n+#define MAX_CTRL_ARGS 255\n+#define MAX_WRITE_LEN 255\n+\n+enum Roles {\n+    ROLE_CTRL = 0,\n+    ROLE_STDIN = 1,\n+    ROLE_STDOUT = 2,\n+    ROLE_STDERR = 3,\n+};\n+\n+typedef struct {\n+    uint8_t id;\n+    uint8_t role;\n+    int events;\n+    bool eof;\n+    int credit;\n+    int fd;\n+    int startpos;\n+    int endpos;\n+    int bufcount;\n+    char buffer[PIPE_BUF];\n+} pipebuf_t;\n+\n+typedef struct {\n+    int pid;\n+    pipebuf_t ctrl;\n+    pipebuf_t stdin;\n+    pipebuf_t stdout;\n+    pipebuf_t stderr;\n+} procinfo_t;\n+\n+#define N_PROC 256\n+procinfo_t* processes[N_PROC];\n+\n+void child(int ctrl, int stdin, int stdout, int stderr);\n+void pipebuf_out_ack(pipebuf_t* pb, size_t acksize);\n+void modify_pipebuf_epoll(pipebuf_t* pb, int operation);\n+void add_pipebuf_epoll(pipebuf_t* pb);\n+void delete_pipebuf_epoll(pipebuf_t* pb);\n+void pipebuf_interal_write(pipebuf_t* pb, uint8_t *data);\n+int write_from_pipebuf(pipebuf_t *pb, int fd, int len);\n+void pipebuf_out_to_internal_buffer(pipebuf_t* pb, int len);\n+void pipebuf_out_is_writable(pipebuf_t* pb);\n+\n+// Helper function to write a packet header to the domain socket\n+// Param: cmd - which command in the Commands enum to send\n+// Param: id - which process is sending this\n+// Param: arg - an argument for the command (like ACK)\n+// Param len - the length of the data being sent following this packet\n+void send_header(uint8_t cmd, uint8_t id, uint8_t arg, uint8_t len) {\n+    // Create the packet\n+    uint8_t buf[4] = {cmd, id, arg, len};\n+    // Write the packet to the socket\n+    int r = write(sock, buf, sizeof(buf));\n+    // Ensure it wrote all of the bytes\n+    if (r != sizeof(buf)) {\n+        fatal(\"send_header failed: %s\", strerror(errno));\n+    }\n+}\n+\n+void read_until(int fd, void *buf, int len) {\n+\n+    int total_read = 0, single_read = 0;\n+\n+    while (total_read < len) {\n+\n+        single_read = read(fd, &(((uint8_t *)buf)[total_read]), len - total_read);\n+\n+        // If we read nothing and the socket would have blocked\n+        if (single_read < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {\n+            // There is nothing to be read right now, try again\n+            continue;\n+        }\n+        // We have read more than zero bytes sucessfully\n+        else if (single_read > 0) {\n+\n+            // Increment the amount we have read\n+            total_read += single_read;\n+        }\n+        else {\n+            fatal(\"An error occured while reading child into pipebuf.\");\n+        }\n+    }\n+}\n+\n+// Constructor function for both read-only and write-only pipe buffers\n+int pipebuf_common_init(pipebuf_t* pb, int id, int role, uint32_t events, unsigned bufend) {\n+    // Assign all fields to the pipe buffer\n+    pb->id = id;\n+    pb->role = role;\n+    pb->eof = false;\n+    pb->startpos = pb->endpos = pb->bufcount = 0;\n+    pb->credit = 0;\n+    pb->events = events;\n+    // Allocate memory for file descriptors\n+    int pipefd[2];\n+    // Create the pipes\n+    int r = pipe2(pipefd, O_NONBLOCK);\n+    // Verify it worked\n+    if (r < 0) {\n+        fatal(\"pipe failed: %s\", strerror(errno));\n+    }\n+\n+    // Set the proper read or write pipe file descriptor\n+    pb->fd = pipefd[bufend];\n+\n+    // Return the end of the file descriptor for the daemon to write to\n+    return pipefd[!bufend];\n+}\n+\n+// Helper function to write form a pipe buffers internal buffer\n+// into its file descriptor\n+int write_from_pipebuf(pipebuf_t *pb, int fd, int write_len) {\n+    // The number of bytes to write in a single pass\n+    int to_write = 0;\n+    // Var to help us keep track of the number of bytes written\n+    int total_written = write_len;\n+\n+    // While we still have data to write\n+    while(write_len) {\n+\n+        // Attempt to write all of the bytes\n+        to_write = write_len;\n+\n+        // If the data would go past the end of the buffer\n+        if (pb->startpos + to_write > PIPE_BUF) {\n+            // Only write until the end of the buffer\n+            to_write = PIPE_BUF - pb->startpos - 1;\n+        }\n+\n+        info(\"In write_from_pipebuf... going write %d bytes to the sock\", to_write);","path":"soc/usbexecd.c","position":188,"original_position":188,"commit_id":"37278849d9099226742636457c86bffc83d7991a","original_commit_id":"37278849d9099226742636457c86bffc83d7991a","user":{"login":"kevinmehall","id":119951,"avatar_url":"https://avatars.githubusercontent.com/u/119951?v=3","gravatar_id":"","url":"https://api.github.com/users/kevinmehall","html_url":"https://github.com/kevinmehall","followers_url":"https://api.github.com/users/kevinmehall/followers","following_url":"https://api.github.com/users/kevinmehall/following{/other_user}","gists_url":"https://api.github.com/users/kevinmehall/gists{/gist_id}","starred_url":"https://api.github.com/users/kevinmehall/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kevinmehall/subscriptions","organizations_url":"https://api.github.com/users/kevinmehall/orgs","repos_url":"https://api.github.com/users/kevinmehall/repos","events_url":"https://api.github.com/users/kevinmehall/events{/privacy}","received_events_url":"https://api.github.com/users/kevinmehall/received_events","type":"User","site_admin":false},"body":"`debug` level or remove.","created_at":"2015-04-10T22:51:34Z","updated_at":"2015-04-10T22:51:34Z","html_url":"https://github.com/tessel/t2-firmware/pull/3#discussion_r28186476","pull_request_url":"https://api.github.com/repos/tessel/t2-firmware/pulls/3","_links":{"self":{"href":"https://api.github.com/repos/tessel/t2-firmware/pulls/comments/28186476"},"html":{"href":"https://github.com/tessel/t2-firmware/pull/3#discussion_r28186476"},"pull_request":{"href":"https://api.github.com/repos/tessel/t2-firmware/pulls/3"}}},"pull_request":{"url":"https://api.github.com/repos/tessel/t2-firmware/pulls/3","id":32152690,"html_url":"https://github.com/tessel/t2-firmware/pull/3","diff_url":"https://github.com/tessel/t2-firmware/pull/3.diff","patch_url":"https://github.com/tessel/t2-firmware/pull/3.patch","issue_url":"https://api.github.com/repos/tessel/t2-firmware/issues/3","number":3,"state":"open","locked":false,"title":"[WIP] USB Daemon","user":{"login":"johnnyman727","id":432909,"avatar_url":"https://avatars.githubusercontent.com/u/432909?v=3","gravatar_id":"","url":"https://api.github.com/users/johnnyman727","html_url":"https://github.com/johnnyman727","followers_url":"https://api.github.com/users/johnnyman727/followers","following_url":"https://api.github.com/users/johnnyman727/following{/other_user}","gists_url":"https://api.github.com/users/johnnyman727/gists{/gist_id}","starred_url":"https://api.github.com/users/johnnyman727/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/johnnyman727/subscriptions","organizations_url":"https://api.github.com/users/johnnyman727/orgs","repos_url":"https://api.github.com/users/johnnyman727/repos","events_url":"https://api.github.com/users/johnnyman727/events{/privacy}","received_events_url":"https://api.github.com/users/johnnyman727/received_events","type":"User","site_admin":false},"body":"I'll remove all of the extraneous logs before it's ready to be merged.","created_at":"2015-03-27T23:13:27Z","updated_at":"2015-04-10T22:51:34Z","closed_at":null,"merged_at":null,"merge_commit_sha":"2640d2a32e23f00519db0fafe3c6cf2a5353be4b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/tessel/t2-firmware/pulls/3/commits","review_comments_url":"https://api.github.com/repos/tessel/t2-firmware/pulls/3/comments","review_comment_url":"https://api.github.com/repos/tessel/t2-firmware/pulls/comments{/number}","comments_url":"https://api.github.com/repos/tessel/t2-firmware/issues/3/comments","statuses_url":"https://api.github.com/repos/tessel/t2-firmware/statuses/37278849d9099226742636457c86bffc83d7991a","head":{"label":"tessel:jon-compile-mips","ref":"jon-compile-mips","sha":"37278849d9099226742636457c86bffc83d7991a","user":{"login":"tessel","id":5573563,"avatar_url":"https://avatars.githubusercontent.com/u/5573563?v=3","gravatar_id":"","url":"https://api.github.com/users/tessel","html_url":"https://github.com/tessel","followers_url":"https://api.github.com/users/tessel/followers","following_url":"https://api.github.com/users/tessel/following{/other_user}","gists_url":"https://api.github.com/users/tessel/gists{/gist_id}","starred_url":"https://api.github.com/users/tessel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tessel/subscriptions","organizations_url":"https://api.github.com/users/tessel/orgs","repos_url":"https://api.github.com/users/tessel/repos","events_url":"https://api.github.com/users/tessel/events{/privacy}","received_events_url":"https://api.github.com/users/tessel/received_events","type":"Organization","site_admin":false},"repo":{"id":29262099,"name":"t2-firmware","full_name":"tessel/t2-firmware","owner":{"login":"tessel","id":5573563,"avatar_url":"https://avatars.githubusercontent.com/u/5573563?v=3","gravatar_id":"","url":"https://api.github.com/users/tessel","html_url":"https://github.com/tessel","followers_url":"https://api.github.com/users/tessel/followers","following_url":"https://api.github.com/users/tessel/following{/other_user}","gists_url":"https://api.github.com/users/tessel/gists{/gist_id}","starred_url":"https://api.github.com/users/tessel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tessel/subscriptions","organizations_url":"https://api.github.com/users/tessel/orgs","repos_url":"https://api.github.com/users/tessel/repos","events_url":"https://api.github.com/users/tessel/events{/privacy}","received_events_url":"https://api.github.com/users/tessel/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/tessel/t2-firmware","description":"Firmware for Tessel 2's SAMD21 coprocessor and related SoC code","fork":false,"url":"https://api.github.com/repos/tessel/t2-firmware","forks_url":"https://api.github.com/repos/tessel/t2-firmware/forks","keys_url":"https://api.github.com/repos/tessel/t2-firmware/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tessel/t2-firmware/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tessel/t2-firmware/teams","hooks_url":"https://api.github.com/repos/tessel/t2-firmware/hooks","issue_events_url":"https://api.github.com/repos/tessel/t2-firmware/issues/events{/number}","events_url":"https://api.github.com/repos/tessel/t2-firmware/events","assignees_url":"https://api.github.com/repos/tessel/t2-firmware/assignees{/user}","branches_url":"https://api.github.com/repos/tessel/t2-firmware/branches{/branch}","tags_url":"https://api.github.com/repos/tessel/t2-firmware/tags","blobs_url":"https://api.github.com/repos/tessel/t2-firmware/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tessel/t2-firmware/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tessel/t2-firmware/git/refs{/sha}","trees_url":"https://api.github.com/repos/tessel/t2-firmware/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tessel/t2-firmware/statuses/{sha}","languages_url":"https://api.github.com/repos/tessel/t2-firmware/languages","stargazers_url":"https://api.github.com/repos/tessel/t2-firmware/stargazers","contributors_url":"https://api.github.com/repos/tessel/t2-firmware/contributors","subscribers_url":"https://api.github.com/repos/tessel/t2-firmware/subscribers","subscription_url":"https://api.github.com/repos/tessel/t2-firmware/subscription","commits_url":"https://api.github.com/repos/tessel/t2-firmware/commits{/sha}","git_commits_url":"https://api.github.com/repos/tessel/t2-firmware/git/commits{/sha}","comments_url":"https://api.github.com/repos/tessel/t2-firmware/comments{/number}","issue_comment_url":"https://api.github.com/repos/tessel/t2-firmware/issues/comments{/number}","contents_url":"https://api.github.com/repos/tessel/t2-firmware/contents/{+path}","compare_url":"https://api.github.com/repos/tessel/t2-firmware/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tessel/t2-firmware/merges","archive_url":"https://api.github.com/repos/tessel/t2-firmware/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tessel/t2-firmware/downloads","issues_url":"https://api.github.com/repos/tessel/t2-firmware/issues{/number}","pulls_url":"https://api.github.com/repos/tessel/t2-firmware/pulls{/number}","milestones_url":"https://api.github.com/repos/tessel/t2-firmware/milestones{/number}","notifications_url":"https://api.github.com/repos/tessel/t2-firmware/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tessel/t2-firmware/labels{/name}","releases_url":"https://api.github.com/repos/tessel/t2-firmware/releases{/id}","created_at":"2015-01-14T19:38:00Z","updated_at":"2015-04-10T20:44:57Z","pushed_at":"2015-04-10T22:46:20Z","git_url":"git://github.com/tessel/t2-firmware.git","ssh_url":"git@github.com:tessel/t2-firmware.git","clone_url":"https://github.com/tessel/t2-firmware.git","svn_url":"https://github.com/tessel/t2-firmware","homepage":"","size":745,"stargazers_count":2,"watchers_count":2,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":10,"forks":0,"open_issues":10,"watchers":2,"default_branch":"master"}},"base":{"label":"tessel:master","ref":"master","sha":"bca690de0b0c165c912b737153a46113b3ab0d61","user":{"login":"tessel","id":5573563,"avatar_url":"https://avatars.githubusercontent.com/u/5573563?v=3","gravatar_id":"","url":"https://api.github.com/users/tessel","html_url":"https://github.com/tessel","followers_url":"https://api.github.com/users/tessel/followers","following_url":"https://api.github.com/users/tessel/following{/other_user}","gists_url":"https://api.github.com/users/tessel/gists{/gist_id}","starred_url":"https://api.github.com/users/tessel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tessel/subscriptions","organizations_url":"https://api.github.com/users/tessel/orgs","repos_url":"https://api.github.com/users/tessel/repos","events_url":"https://api.github.com/users/tessel/events{/privacy}","received_events_url":"https://api.github.com/users/tessel/received_events","type":"Organization","site_admin":false},"repo":{"id":29262099,"name":"t2-firmware","full_name":"tessel/t2-firmware","owner":{"login":"tessel","id":5573563,"avatar_url":"https://avatars.githubusercontent.com/u/5573563?v=3","gravatar_id":"","url":"https://api.github.com/users/tessel","html_url":"https://github.com/tessel","followers_url":"https://api.github.com/users/tessel/followers","following_url":"https://api.github.com/users/tessel/following{/other_user}","gists_url":"https://api.github.com/users/tessel/gists{/gist_id}","starred_url":"https://api.github.com/users/tessel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tessel/subscriptions","organizations_url":"https://api.github.com/users/tessel/orgs","repos_url":"https://api.github.com/users/tessel/repos","events_url":"https://api.github.com/users/tessel/events{/privacy}","received_events_url":"https://api.github.com/users/tessel/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/tessel/t2-firmware","description":"Firmware for Tessel 2's SAMD21 coprocessor and related SoC code","fork":false,"url":"https://api.github.com/repos/tessel/t2-firmware","forks_url":"https://api.github.com/repos/tessel/t2-firmware/forks","keys_url":"https://api.github.com/repos/tessel/t2-firmware/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tessel/t2-firmware/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tessel/t2-firmware/teams","hooks_url":"https://api.github.com/repos/tessel/t2-firmware/hooks","issue_events_url":"https://api.github.com/repos/tessel/t2-firmware/issues/events{/number}","events_url":"https://api.github.com/repos/tessel/t2-firmware/events","assignees_url":"https://api.github.com/repos/tessel/t2-firmware/assignees{/user}","branches_url":"https://api.github.com/repos/tessel/t2-firmware/branches{/branch}","tags_url":"https://api.github.com/repos/tessel/t2-firmware/tags","blobs_url":"https://api.github.com/repos/tessel/t2-firmware/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tessel/t2-firmware/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tessel/t2-firmware/git/refs{/sha}","trees_url":"https://api.github.com/repos/tessel/t2-firmware/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tessel/t2-firmware/statuses/{sha}","languages_url":"https://api.github.com/repos/tessel/t2-firmware/languages","stargazers_url":"https://api.github.com/repos/tessel/t2-firmware/stargazers","contributors_url":"https://api.github.com/repos/tessel/t2-firmware/contributors","subscribers_url":"https://api.github.com/repos/tessel/t2-firmware/subscribers","subscription_url":"https://api.github.com/repos/tessel/t2-firmware/subscription","commits_url":"https://api.github.com/repos/tessel/t2-firmware/commits{/sha}","git_commits_url":"https://api.github.com/repos/tessel/t2-firmware/git/commits{/sha}","comments_url":"https://api.github.com/repos/tessel/t2-firmware/comments{/number}","issue_comment_url":"https://api.github.com/repos/tessel/t2-firmware/issues/comments{/number}","contents_url":"https://api.github.com/repos/tessel/t2-firmware/contents/{+path}","compare_url":"https://api.github.com/repos/tessel/t2-firmware/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tessel/t2-firmware/merges","archive_url":"https://api.github.com/repos/tessel/t2-firmware/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tessel/t2-firmware/downloads","issues_url":"https://api.github.com/repos/tessel/t2-firmware/issues{/number}","pulls_url":"https://api.github.com/repos/tessel/t2-firmware/pulls{/number}","milestones_url":"https://api.github.com/repos/tessel/t2-firmware/milestones{/number}","notifications_url":"https://api.github.com/repos/tessel/t2-firmware/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tessel/t2-firmware/labels{/name}","releases_url":"https://api.github.com/repos/tessel/t2-firmware/releases{/id}","created_at":"2015-01-14T19:38:00Z","updated_at":"2015-04-10T20:44:57Z","pushed_at":"2015-04-10T22:46:20Z","git_url":"git://github.com/tessel/t2-firmware.git","ssh_url":"git@github.com:tessel/t2-firmware.git","clone_url":"https://github.com/tessel/t2-firmware.git","svn_url":"https://github.com/tessel/t2-firmware","homepage":"","size":745,"stargazers_count":2,"watchers_count":2,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":10,"forks":0,"open_issues":10,"watchers":2,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/tessel/t2-firmware/pulls/3"},"html":{"href":"https://github.com/tessel/t2-firmware/pull/3"},"issue":{"href":"https://api.github.com/repos/tessel/t2-firmware/issues/3"},"comments":{"href":"https://api.github.com/repos/tessel/t2-firmware/issues/3/comments"},"review_comments":{"href":"https://api.github.com/repos/tessel/t2-firmware/pulls/3/comments"},"review_comment":{"href":"https://api.github.com/repos/tessel/t2-firmware/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/tessel/t2-firmware/pulls/3/commits"},"statuses":{"href":"https://api.github.com/repos/tessel/t2-firmware/statuses/37278849d9099226742636457c86bffc83d7991a"}}}}