Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int pty_getproc(lua_State* L)
- {
- int fd = lua_tonumber(L, 1);
- pid_t pgrp = tcgetpgrp(fd);
- if (pgrp == -1)
- return lua_return(L, nullptr, "failed to locate pid from fd");
- std::ifstream ifs("/proc/" + std::to_string(pgrp) + "/cmdline");
- if(!ifs.is_open())
- return lua_return(L, nullptr, "failed to open /proc/" + std::to_string(pgrp) + "/cmdline");
- std::string contents;
- ifs >> contents;
- ifs.close();
- lua_pushstring(L, contents.c_str());
- return 1;
- }
- int c_pty_getproc(lua_State* L)
- {
- int fd = lua_tonumber(L, 1);
- FILE *f;
- char *path, *buf;
- size_t len;
- int ch;
- pid_t pgrp;
- int r;
- if ((pgrp = tcgetpgrp(fd)) == -1)
- return lua_return(L, nullptr, "failed to locate pid from fd");
- r = asprintf(&path, "/proc/%lld/cmdline", (long long)pgrp);
- if (r == -1 || path == NULL)
- return lua_return(L, nullptr, "failed to open /proc/$pid/cmdline");
- if ((f = fopen(path, "r")) == NULL)
- {
- free(path);
- return lua_return(L, nullptr, "failed to open /proc/$pid/cmdline");
- }
- free(path);
- len = 0;
- buf = NULL;
- while ((ch = fgetc(f)) != EOF) {
- if (ch == '\0') break;
- buf = (char *)realloc(buf, len + 2);
- if (buf == NULL) return lua_return(L, nullptr, "failed to read file");
- buf[len++] = ch;
- }
- if (buf != NULL) {
- buf[len] = '\0';
- }
- fclose(f);
- lua_pushstring(L, buf);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement