libc_unix.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. // Copyright 2020 The Libc Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build linux || darwin || freebsd || netbsd || openbsd
  5. // +build linux darwin freebsd netbsd openbsd
  6. package libc // import "modernc.org/libc"
  7. import (
  8. "bufio"
  9. "io/ioutil"
  10. "math"
  11. "math/rand"
  12. "os"
  13. gosignal "os/signal"
  14. "reflect"
  15. "strconv"
  16. "strings"
  17. "sync"
  18. "syscall"
  19. "time"
  20. "unsafe"
  21. guuid "github.com/google/uuid"
  22. "golang.org/x/sys/unix"
  23. "modernc.org/libc/errno"
  24. "modernc.org/libc/grp"
  25. "modernc.org/libc/poll"
  26. "modernc.org/libc/pwd"
  27. "modernc.org/libc/signal"
  28. "modernc.org/libc/stdio"
  29. "modernc.org/libc/stdlib"
  30. "modernc.org/libc/sys/types"
  31. ctime "modernc.org/libc/time"
  32. )
  33. var staticGetpwnam pwd.Passwd
  34. func init() {
  35. atExit = append(atExit, func() { closePasswd(&staticGetpwnam) })
  36. }
  37. // sighandler_t signal(int signum, sighandler_t handler);
  38. func Xsignal(t *TLS, signum int32, handler uintptr) uintptr { //TODO use sigaction?
  39. signalsMu.Lock()
  40. defer signalsMu.Unlock()
  41. r := signals[signum]
  42. signals[signum] = handler
  43. switch handler {
  44. case signal.SIG_DFL:
  45. panic(todo("%v %#x", syscall.Signal(signum), handler))
  46. case signal.SIG_IGN:
  47. switch r {
  48. case signal.SIG_DFL:
  49. gosignal.Ignore(syscall.Signal(signum)) //TODO
  50. case signal.SIG_IGN:
  51. gosignal.Ignore(syscall.Signal(signum))
  52. default:
  53. panic(todo("%v %#x", syscall.Signal(signum), handler))
  54. }
  55. default:
  56. switch r {
  57. case signal.SIG_DFL:
  58. c := make(chan os.Signal, 1)
  59. gosignal.Notify(c, syscall.Signal(signum))
  60. go func() { //TODO mechanism to stop/cancel
  61. for {
  62. <-c
  63. var f func(*TLS, int32)
  64. *(*uintptr)(unsafe.Pointer(&f)) = handler
  65. tls := NewTLS()
  66. f(tls, signum)
  67. tls.Close()
  68. }
  69. }()
  70. case signal.SIG_IGN:
  71. panic(todo("%v %#x", syscall.Signal(signum), handler))
  72. default:
  73. panic(todo("%v %#x", syscall.Signal(signum), handler))
  74. }
  75. }
  76. return r
  77. }
  78. // void rewind(FILE *stream);
  79. func Xrewind(t *TLS, stream uintptr) {
  80. Xfseek(t, stream, 0, stdio.SEEK_SET)
  81. }
  82. // int putchar(int c);
  83. func Xputchar(t *TLS, c int32) int32 {
  84. if _, err := write([]byte{byte(c)}); err != nil {
  85. return stdio.EOF
  86. }
  87. return int32(c)
  88. }
  89. // int gethostname(char *name, size_t len);
  90. func Xgethostname(t *TLS, name uintptr, slen types.Size_t) int32 {
  91. if slen < 0 {
  92. t.setErrno(errno.EINVAL)
  93. return -1
  94. }
  95. if slen == 0 {
  96. return 0
  97. }
  98. s, err := os.Hostname()
  99. if err != nil {
  100. panic(todo(""))
  101. }
  102. n := len(s)
  103. if len(s) >= int(slen) {
  104. n = int(slen) - 1
  105. }
  106. sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
  107. copy((*RawMem)(unsafe.Pointer(name))[:n:n], (*RawMem)(unsafe.Pointer(sh.Data))[:n:n])
  108. *(*byte)(unsafe.Pointer(name + uintptr(n))) = 0
  109. return 0
  110. }
  111. // int remove(const char *pathname);
  112. func Xremove(t *TLS, pathname uintptr) int32 {
  113. panic(todo(""))
  114. }
  115. // long pathconf(const char *path, int name);
  116. func Xpathconf(t *TLS, path uintptr, name int32) long {
  117. panic(todo(""))
  118. }
  119. // ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);
  120. func Xrecvfrom(t *TLS, sockfd int32, buf uintptr, len types.Size_t, flags int32, src_addr, addrlen uintptr) types.Ssize_t {
  121. panic(todo(""))
  122. }
  123. // ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen);
  124. func Xsendto(t *TLS, sockfd int32, buf uintptr, len types.Size_t, flags int32, src_addr uintptr, addrlen socklen_t) types.Ssize_t {
  125. panic(todo(""))
  126. }
  127. // void srand48(long int seedval);
  128. func Xsrand48(t *TLS, seedval long) {
  129. panic(todo(""))
  130. }
  131. // long int lrand48(void);
  132. func Xlrand48(t *TLS) long {
  133. panic(todo(""))
  134. }
  135. // ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
  136. func Xsendmsg(t *TLS, sockfd int32, msg uintptr, flags int32) types.Ssize_t {
  137. panic(todo(""))
  138. }
  139. // int poll(struct pollfd *fds, nfds_t nfds, int timeout);
  140. func Xpoll(t *TLS, fds uintptr, nfds poll.Nfds_t, timeout int32) int32 {
  141. if nfds == 0 {
  142. panic(todo(""))
  143. }
  144. // if dmesgs {
  145. // dmesg("%v: %#x %v %v, %+v", origin(1), fds, nfds, timeout, (*[1000]unix.PollFd)(unsafe.Pointer(fds))[:nfds:nfds])
  146. // }
  147. n, err := unix.Poll((*[1000]unix.PollFd)(unsafe.Pointer(fds))[:nfds:nfds], int(timeout))
  148. // if dmesgs {
  149. // dmesg("%v: %v %v", origin(1), n, err)
  150. // }
  151. if err != nil {
  152. t.setErrno(err)
  153. return -1
  154. }
  155. return int32(n)
  156. }
  157. // ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
  158. func Xrecvmsg(t *TLS, sockfd int32, msg uintptr, flags int32) types.Ssize_t {
  159. n, _, err := unix.Syscall(unix.SYS_RECVMSG, uintptr(sockfd), msg, uintptr(flags))
  160. if err != 0 {
  161. t.setErrno(err)
  162. return -1
  163. }
  164. return types.Ssize_t(n)
  165. }
  166. // struct cmsghdr *CMSG_NXTHDR(struct msghdr *msgh, struct cmsghdr *cmsg);
  167. func X__cmsg_nxthdr(t *TLS, msgh, cmsg uintptr) uintptr {
  168. panic(todo(""))
  169. }
  170. // wchar_t *wcschr(const wchar_t *wcs, wchar_t wc);
  171. func Xwcschr(t *TLS, wcs uintptr, wc wchar_t) wchar_t {
  172. panic(todo(""))
  173. }
  174. // gid_t getegid(void);
  175. func Xgetegid(t *TLS) types.Gid_t {
  176. panic(todo(""))
  177. }
  178. // gid_t getgid(void);
  179. func Xgetgid(t *TLS) types.Gid_t {
  180. panic(todo(""))
  181. }
  182. // void *shmat(int shmid, const void *shmaddr, int shmflg);
  183. func Xshmat(t *TLS, shmid int32, shmaddr uintptr, shmflg int32) uintptr {
  184. panic(todo(""))
  185. }
  186. // int shmctl(int shmid, int cmd, struct shmid_ds *buf);
  187. func Xshmctl(t *TLS, shmid, cmd int32, buf uintptr) int32 {
  188. panic(todo(""))
  189. }
  190. // int shmdt(const void *shmaddr);
  191. func Xshmdt(t *TLS, shmaddr uintptr) int32 {
  192. panic(todo(""))
  193. }
  194. // int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
  195. func Xgetresuid(t *TLS, ruid, euid, suid uintptr) int32 {
  196. panic(todo(""))
  197. }
  198. // int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
  199. func Xgetresgid(t *TLS, rgid, egid, sgid uintptr) int32 {
  200. panic(todo(""))
  201. }
  202. // FILE *tmpfile(void);
  203. func Xtmpfile(t *TLS) uintptr {
  204. f, err := ioutil.TempFile("", "tmpfile-")
  205. if err != nil {
  206. t.setErrno(err)
  207. return 0
  208. }
  209. cf := newFile(t, int32(f.Fd()))
  210. AtExit(func() {
  211. nm := f.Name()
  212. file(cf).close(t)
  213. os.Remove(nm)
  214. })
  215. return cf
  216. }
  217. // FILE *fdopen(int fd, const char *mode);
  218. func Xfdopen(t *TLS, fd int32, mode uintptr) uintptr {
  219. m := strings.ReplaceAll(GoString(mode), "b", "")
  220. switch m {
  221. case
  222. "a",
  223. "a+",
  224. "r",
  225. "r+",
  226. "w",
  227. "w+":
  228. default:
  229. t.setErrno(errno.EINVAL)
  230. return 0
  231. }
  232. if p := newFile(t, fd); p != 0 {
  233. return p
  234. }
  235. t.setErrno(errno.EINVAL)
  236. return 0
  237. }
  238. // struct passwd *getpwnam(const char *name);
  239. func Xgetpwnam(t *TLS, name uintptr) uintptr {
  240. f, err := os.Open("/etc/passwd")
  241. if err != nil {
  242. panic(todo("", err))
  243. }
  244. defer f.Close()
  245. sname := GoString(name)
  246. sc := bufio.NewScanner(f)
  247. for sc.Scan() {
  248. s := strings.TrimSpace(sc.Text())
  249. if s == "" || strings.HasPrefix(s, "#") {
  250. continue
  251. }
  252. // eg. "root:x:0:0:root:/root:/bin/bash"
  253. a := strings.Split(s, ":")
  254. if len(a) < 7 {
  255. panic(todo(""))
  256. }
  257. if a[0] == sname {
  258. uid, err := strconv.Atoi(a[2])
  259. if err != nil {
  260. panic(todo(""))
  261. }
  262. gid, err := strconv.Atoi(a[3])
  263. if err != nil {
  264. panic(todo(""))
  265. }
  266. closePasswd(&staticGetpwnam)
  267. gecos := a[4]
  268. if strings.Contains(gecos, ",") {
  269. a := strings.Split(gecos, ",")
  270. gecos = a[0]
  271. }
  272. initPasswd(t, &staticGetpwnam, a[0], a[1], uint32(uid), uint32(gid), gecos, a[5], a[6])
  273. return uintptr(unsafe.Pointer(&staticGetpwnam))
  274. }
  275. }
  276. if sc.Err() != nil {
  277. panic(todo(""))
  278. }
  279. return 0
  280. }
  281. // int getpwnam_r(char *name, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result);
  282. func Xgetpwnam_r(t *TLS, name, cpwd, buf uintptr, buflen types.Size_t, result uintptr) int32 {
  283. f, err := os.Open("/etc/passwd")
  284. if err != nil {
  285. panic(todo("", err))
  286. }
  287. defer f.Close()
  288. sname := GoString(name)
  289. sc := bufio.NewScanner(f)
  290. for sc.Scan() {
  291. s := strings.TrimSpace(sc.Text())
  292. if s == "" || strings.HasPrefix(s, "#") {
  293. continue
  294. }
  295. // eg. "root:x:0:0:root:/root:/bin/bash"
  296. a := strings.Split(s, ":")
  297. if len(a) < 7 {
  298. panic(todo("%q", s))
  299. }
  300. if a[0] == sname {
  301. uid, err := strconv.Atoi(a[2])
  302. if err != nil {
  303. panic(todo(""))
  304. }
  305. gid, err := strconv.Atoi(a[3])
  306. if err != nil {
  307. panic(todo(""))
  308. }
  309. gecos := a[4]
  310. if strings.Contains(gecos, ",") {
  311. a := strings.Split(gecos, ",")
  312. gecos = a[0]
  313. }
  314. var v pwd.Passwd
  315. if initPasswd2(t, buf, buflen, &v, a[0], a[1], uint32(uid), uint32(gid), gecos, a[5], a[6]) {
  316. *(*pwd.Passwd)(unsafe.Pointer(cpwd)) = v
  317. *(*uintptr)(unsafe.Pointer(result)) = cpwd
  318. return 0
  319. }
  320. *(*uintptr)(unsafe.Pointer(result)) = 0
  321. return errno.ERANGE
  322. }
  323. }
  324. if sc.Err() != nil {
  325. panic(todo(""))
  326. }
  327. *(*uintptr)(unsafe.Pointer(result)) = 0
  328. return 0
  329. }
  330. func init() {
  331. atExit = append(atExit, func() { closeGroup(&staticGetgrgid) })
  332. }
  333. var staticGetgrgid grp.Group
  334. // struct group *getgrgid(gid_t gid);
  335. func Xgetgrgid(t *TLS, gid uint32) uintptr {
  336. f, err := os.Open("/etc/group")
  337. if err != nil {
  338. panic(todo(""))
  339. }
  340. defer f.Close()
  341. sid := strconv.Itoa(int(gid))
  342. sc := bufio.NewScanner(f)
  343. for sc.Scan() {
  344. s := strings.TrimSpace(sc.Text())
  345. if s == "" || strings.HasPrefix(s, "#") {
  346. continue
  347. }
  348. // eg. "root:x:0:"
  349. a := strings.Split(s, ":")
  350. if len(a) < 4 {
  351. panic(todo("%q", s))
  352. }
  353. if a[2] == sid {
  354. closeGroup(&staticGetgrgid)
  355. var names []string
  356. if a[3] != "" {
  357. names = strings.Split(a[3], ",")
  358. }
  359. initGroup(t, &staticGetgrgid, a[0], a[1], gid, names)
  360. return uintptr(unsafe.Pointer(&staticGetgrgid))
  361. }
  362. }
  363. if sc.Err() != nil {
  364. panic(todo(""))
  365. }
  366. return 0
  367. }
  368. // int getgrgid_r(gid_t gid, struct group *grp, char *buf, size_t buflen, struct group **result);
  369. func Xgetgrgid_r(t *TLS, gid uint32, pGrp, buf uintptr, buflen types.Size_t, result uintptr) int32 {
  370. f, err := os.Open("/etc/group")
  371. if err != nil {
  372. panic(todo(""))
  373. }
  374. defer f.Close()
  375. sid := strconv.Itoa(int(gid))
  376. sc := bufio.NewScanner(f)
  377. for sc.Scan() {
  378. s := strings.TrimSpace(sc.Text())
  379. if s == "" || strings.HasPrefix(s, "#") {
  380. continue
  381. }
  382. // eg. "root:x:0:"
  383. a := strings.Split(s, ":")
  384. if len(a) < 4 {
  385. panic(todo("%q", s))
  386. }
  387. if a[2] == sid {
  388. var names []string
  389. if a[3] != "" {
  390. names = strings.Split(a[3], ",")
  391. }
  392. var x grp.Group
  393. if initGroup2(buf, buflen, &x, a[0], a[1], gid, names) {
  394. *(*grp.Group)(unsafe.Pointer(pGrp)) = x
  395. *(*uintptr)(unsafe.Pointer(result)) = pGrp
  396. return 0
  397. }
  398. *(*uintptr)(unsafe.Pointer(result)) = 0
  399. return 0
  400. }
  401. }
  402. if sc.Err() != nil {
  403. panic(todo(""))
  404. }
  405. *(*uintptr)(unsafe.Pointer(result)) = 0
  406. return 0
  407. }
  408. func initPasswd2(t *TLS, buf uintptr, buflen types.Size_t, p *pwd.Passwd, name, pwd string, uid, gid uint32, gecos, dir, shell string) bool {
  409. p.Fpw_name, buf, buflen = bufString(buf, buflen, name)
  410. if buf == 0 {
  411. return false
  412. }
  413. p.Fpw_passwd, buf, buflen = bufString(buf, buflen, pwd)
  414. if buf == 0 {
  415. return false
  416. }
  417. p.Fpw_uid = uid
  418. p.Fpw_gid = gid
  419. if buf == 0 {
  420. return false
  421. }
  422. p.Fpw_gecos, buf, buflen = bufString(buf, buflen, gecos)
  423. if buf == 0 {
  424. return false
  425. }
  426. p.Fpw_dir, buf, buflen = bufString(buf, buflen, dir)
  427. if buf == 0 {
  428. return false
  429. }
  430. p.Fpw_shell, buf, buflen = bufString(buf, buflen, shell)
  431. if buf == 0 {
  432. return false
  433. }
  434. return true
  435. }
  436. func bufString(buf uintptr, buflen types.Size_t, s string) (uintptr, uintptr, types.Size_t) {
  437. buf0 := buf
  438. rq := len(s) + 1
  439. if rq > int(buflen) {
  440. return 0, 0, 0
  441. }
  442. copy((*RawMem)(unsafe.Pointer(buf))[:len(s):len(s)], s)
  443. buf += uintptr(len(s))
  444. *(*byte)(unsafe.Pointer(buf)) = 0
  445. return buf0, buf + 1, buflen - types.Size_t(rq)
  446. }
  447. func closeGroup(p *grp.Group) {
  448. Xfree(nil, p.Fgr_name)
  449. Xfree(nil, p.Fgr_passwd)
  450. if p := p.Fgr_mem; p != 0 {
  451. for {
  452. q := *(*uintptr)(unsafe.Pointer(p))
  453. if q == 0 {
  454. break
  455. }
  456. Xfree(nil, q)
  457. p += unsafe.Sizeof(uintptr(0))
  458. }
  459. }
  460. *p = grp.Group{}
  461. }
  462. func initGroup(t *TLS, p *grp.Group, name, pwd string, gid uint32, names []string) {
  463. p.Fgr_name = cString(t, name)
  464. p.Fgr_passwd = cString(t, pwd)
  465. p.Fgr_gid = gid
  466. a := Xcalloc(t, 1, types.Size_t(unsafe.Sizeof(uintptr(0)))*types.Size_t((len(names)+1)))
  467. if a == 0 {
  468. panic("OOM")
  469. }
  470. for p := a; len(names) != 0; p += unsafe.Sizeof(uintptr(0)) {
  471. *(*uintptr)(unsafe.Pointer(p)) = cString(t, names[0])
  472. names = names[1:]
  473. }
  474. p.Fgr_mem = a
  475. }
  476. func initGroup2(buf uintptr, buflen types.Size_t, p *grp.Group, name, pwd string, gid uint32, names []string) bool {
  477. p.Fgr_name, buf, buflen = bufString(buf, buflen, name)
  478. if buf == 0 {
  479. return false
  480. }
  481. p.Fgr_passwd, buf, buflen = bufString(buf, buflen, pwd)
  482. if buf == 0 {
  483. return false
  484. }
  485. p.Fgr_gid = gid
  486. rq := unsafe.Sizeof(uintptr(0)) * uintptr(len(names)+1)
  487. if rq > uintptr(buflen) {
  488. return false
  489. }
  490. a := buf
  491. buf += rq
  492. for ; len(names) != 0; buf += unsafe.Sizeof(uintptr(0)) {
  493. if len(names[0])+1 > int(buflen) {
  494. return false
  495. }
  496. *(*uintptr)(unsafe.Pointer(buf)), buf, buflen = bufString(buf, buflen, names[0])
  497. names = names[1:]
  498. }
  499. *(*uintptr)(unsafe.Pointer(buf)) = 0
  500. p.Fgr_mem = a
  501. return true
  502. }
  503. func init() {
  504. atExit = append(atExit, func() { closeGroup(&staticGetgrgid) })
  505. }
  506. var staticGetpwuid pwd.Passwd
  507. func init() {
  508. atExit = append(atExit, func() { closePasswd(&staticGetpwuid) })
  509. }
  510. func closePasswd(p *pwd.Passwd) {
  511. Xfree(nil, p.Fpw_name)
  512. Xfree(nil, p.Fpw_passwd)
  513. Xfree(nil, p.Fpw_gecos)
  514. Xfree(nil, p.Fpw_dir)
  515. Xfree(nil, p.Fpw_shell)
  516. *p = pwd.Passwd{}
  517. }
  518. var staticGetgrnam grp.Group
  519. func init() {
  520. atExit = append(atExit, func() { closeGroup(&staticGetgrnam) })
  521. }
  522. // struct passwd *getpwuid(uid_t uid);
  523. func Xgetpwuid(t *TLS, uid uint32) uintptr {
  524. f, err := os.Open("/etc/passwd")
  525. if err != nil {
  526. panic(todo("", err))
  527. }
  528. defer f.Close()
  529. sid := strconv.Itoa(int(uid))
  530. sc := bufio.NewScanner(f)
  531. for sc.Scan() {
  532. s := strings.TrimSpace(sc.Text())
  533. if len(s) == 0 || strings.HasPrefix(s, "#") {
  534. continue
  535. }
  536. // eg. "root:x:0:0:root:/root:/bin/bash"
  537. a := strings.Split(s, ":")
  538. if len(a) < 7 {
  539. panic(todo("%q", s))
  540. }
  541. if a[2] == sid {
  542. uid, err := strconv.Atoi(a[2])
  543. if err != nil {
  544. panic(todo(""))
  545. }
  546. gid, err := strconv.Atoi(a[3])
  547. if err != nil {
  548. panic(todo(""))
  549. }
  550. closePasswd(&staticGetpwuid)
  551. gecos := a[4]
  552. if strings.Contains(gecos, ",") {
  553. a := strings.Split(gecos, ",")
  554. gecos = a[0]
  555. }
  556. initPasswd(t, &staticGetpwuid, a[0], a[1], uint32(uid), uint32(gid), gecos, a[5], a[6])
  557. return uintptr(unsafe.Pointer(&staticGetpwuid))
  558. }
  559. }
  560. if sc.Err() != nil {
  561. panic(todo(""))
  562. }
  563. return 0
  564. }
  565. func initPasswd(t *TLS, p *pwd.Passwd, name, pwd string, uid, gid uint32, gecos, dir, shell string) {
  566. p.Fpw_name = cString(t, name)
  567. p.Fpw_passwd = cString(t, pwd)
  568. p.Fpw_uid = uid
  569. p.Fpw_gid = gid
  570. p.Fpw_gecos = cString(t, gecos)
  571. p.Fpw_dir = cString(t, dir)
  572. p.Fpw_shell = cString(t, shell)
  573. }
  574. // struct group *getgrnam(const char *name);
  575. func Xgetgrnam(t *TLS, name uintptr) uintptr {
  576. f, err := os.Open("/etc/group")
  577. if err != nil {
  578. panic(todo(""))
  579. }
  580. defer f.Close()
  581. sname := GoString(name)
  582. sc := bufio.NewScanner(f)
  583. for sc.Scan() {
  584. s := strings.TrimSpace(sc.Text())
  585. if len(s) == 0 || strings.HasPrefix(s, "#") {
  586. continue
  587. }
  588. // eg. "root:x:0:"
  589. a := strings.Split(s, ":")
  590. if len(a) < 4 {
  591. panic(todo("%q", s))
  592. }
  593. if a[0] == sname {
  594. closeGroup(&staticGetgrnam)
  595. gid, err := strconv.Atoi(a[2])
  596. if err != nil {
  597. panic(todo(""))
  598. }
  599. var names []string
  600. if a[3] != "" {
  601. names = strings.Split(a[3], ",")
  602. }
  603. initGroup(t, &staticGetgrnam, a[0], a[1], uint32(gid), names)
  604. return uintptr(unsafe.Pointer(&staticGetgrnam))
  605. }
  606. }
  607. if sc.Err() != nil {
  608. panic(todo(""))
  609. }
  610. return 0
  611. }
  612. // int getgrnam_r(const char *name, struct group *grp, char *buf, size_t buflen, struct group **result);
  613. func Xgetgrnam_r(t *TLS, name, pGrp, buf uintptr, buflen types.Size_t, result uintptr) int32 {
  614. f, err := os.Open("/etc/group")
  615. if err != nil {
  616. panic(todo(""))
  617. }
  618. defer f.Close()
  619. sname := GoString(name)
  620. sc := bufio.NewScanner(f)
  621. for sc.Scan() {
  622. s := strings.TrimSpace(sc.Text())
  623. if len(s) == 0 || strings.HasPrefix(s, "#") {
  624. continue
  625. }
  626. // eg. "root:x:0:"
  627. a := strings.Split(s, ":")
  628. if len(a) < 4 {
  629. panic(todo("%q", s))
  630. }
  631. if a[0] == sname {
  632. gid, err := strconv.Atoi(a[2])
  633. if err != nil {
  634. panic(todo(""))
  635. }
  636. var names []string
  637. if a[3] != "" {
  638. names = strings.Split(a[3], ",")
  639. }
  640. var x grp.Group
  641. if initGroup2(buf, buflen, &x, a[0], a[1], uint32(gid), names) {
  642. *(*grp.Group)(unsafe.Pointer(pGrp)) = x
  643. *(*uintptr)(unsafe.Pointer(result)) = pGrp
  644. return 0
  645. }
  646. *(*uintptr)(unsafe.Pointer(result)) = 0
  647. return 0
  648. }
  649. }
  650. if sc.Err() != nil {
  651. panic(todo(""))
  652. }
  653. *(*uintptr)(unsafe.Pointer(result)) = 0
  654. return 0
  655. }
  656. // int getpwuid_r(uid_t uid, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result);
  657. func Xgetpwuid_r(t *TLS, uid types.Uid_t, cpwd, buf uintptr, buflen types.Size_t, result uintptr) int32 {
  658. f, err := os.Open("/etc/passwd")
  659. if err != nil {
  660. panic(todo("", err))
  661. }
  662. defer f.Close()
  663. sid := strconv.Itoa(int(uid))
  664. sc := bufio.NewScanner(f)
  665. for sc.Scan() {
  666. s := strings.TrimSpace(sc.Text())
  667. if len(s) == 0 || strings.HasPrefix(s, "#") {
  668. continue
  669. }
  670. // eg. "root:x:0:0:root:/root:/bin/bash"
  671. a := strings.Split(s, ":")
  672. if len(a) < 7 {
  673. panic(todo("%q", s))
  674. }
  675. if a[2] == sid {
  676. uid, err := strconv.Atoi(a[2])
  677. if err != nil {
  678. panic(todo(""))
  679. }
  680. gid, err := strconv.Atoi(a[3])
  681. if err != nil {
  682. panic(todo(""))
  683. }
  684. gecos := a[4]
  685. if strings.Contains(gecos, ",") {
  686. a := strings.Split(gecos, ",")
  687. gecos = a[0]
  688. }
  689. var v pwd.Passwd
  690. if initPasswd2(t, buf, buflen, &v, a[0], a[1], uint32(uid), uint32(gid), gecos, a[5], a[6]) {
  691. *(*pwd.Passwd)(unsafe.Pointer(cpwd)) = v
  692. *(*uintptr)(unsafe.Pointer(result)) = cpwd
  693. return 0
  694. }
  695. *(*uintptr)(unsafe.Pointer(result)) = 0
  696. return errno.ERANGE
  697. }
  698. }
  699. if sc.Err() != nil {
  700. panic(todo(""))
  701. }
  702. *(*uintptr)(unsafe.Pointer(result)) = 0
  703. return 0
  704. }
  705. // int mkostemp(char *template, int flags);
  706. func Xmkostemp(t *TLS, template uintptr, flags int32) int32 {
  707. len := uintptr(Xstrlen(t, template))
  708. x := template + uintptr(len-6)
  709. for i := uintptr(0); i < 6; i++ {
  710. if *(*byte)(unsafe.Pointer(x + i)) != 'X' {
  711. t.setErrno(errno.EINVAL)
  712. return -1
  713. }
  714. }
  715. fd, err := tempFile(template, x, flags)
  716. if err != nil {
  717. t.setErrno(err)
  718. return -1
  719. }
  720. return int32(fd)
  721. }
  722. // void uuid_generate_random(uuid_t out);
  723. func Xuuid_generate_random(t *TLS, out uintptr) {
  724. x := guuid.New()
  725. copy((*RawMem)(unsafe.Pointer(out))[:], x[:])
  726. }
  727. // void uuid_unparse(uuid_t uu, char *out);
  728. func Xuuid_unparse(t *TLS, uu, out uintptr) {
  729. s := (*guuid.UUID)(unsafe.Pointer(uu)).String()
  730. copy((*RawMem)(unsafe.Pointer(out))[:], s)
  731. *(*byte)(unsafe.Pointer(out + uintptr(len(s)))) = 0
  732. }
  733. var staticRandomData = &rand.Rand{}
  734. // char *initstate(unsigned seed, char *state, size_t size);
  735. func Xinitstate(t *TLS, seed uint32, statebuf uintptr, statelen types.Size_t) uintptr {
  736. staticRandomData = rand.New(rand.NewSource(int64(seed)))
  737. return 0
  738. }
  739. // char *setstate(const char *state);
  740. func Xsetstate(t *TLS, state uintptr) uintptr {
  741. t.setErrno(errno.EINVAL) //TODO
  742. return 0
  743. }
  744. // The initstate_r() function is like initstate(3) except that it initializes
  745. // the state in the object pointed to by buf, rather than initializing the
  746. // global state variable. Before calling this function, the buf.state field
  747. // must be initialized to NULL. The initstate_r() function records a pointer
  748. // to the statebuf argument inside the structure pointed to by buf. Thus,
  749. // state‐ buf should not be deallocated so long as buf is still in use. (So,
  750. // statebuf should typically be allocated as a static variable, or allocated on
  751. // the heap using malloc(3) or similar.)
  752. //
  753. // char *initstate_r(unsigned int seed, char *statebuf, size_t statelen, struct random_data *buf);
  754. func Xinitstate_r(t *TLS, seed uint32, statebuf uintptr, statelen types.Size_t, buf uintptr) int32 {
  755. if buf == 0 {
  756. panic(todo(""))
  757. }
  758. randomDataMu.Lock()
  759. defer randomDataMu.Unlock()
  760. randomData[buf] = rand.New(rand.NewSource(int64(seed)))
  761. return 0
  762. }
  763. var (
  764. randomData = map[uintptr]*rand.Rand{}
  765. randomDataMu sync.Mutex
  766. )
  767. // int mkstemps(char *template, int suffixlen);
  768. func Xmkstemps(t *TLS, template uintptr, suffixlen int32) int32 {
  769. return Xmkstemps64(t, template, suffixlen)
  770. }
  771. // int mkstemps(char *template, int suffixlen);
  772. func Xmkstemps64(t *TLS, template uintptr, suffixlen int32) int32 {
  773. len := uintptr(Xstrlen(t, template))
  774. x := template + uintptr(len-6) - uintptr(suffixlen)
  775. for i := uintptr(0); i < 6; i++ {
  776. if *(*byte)(unsafe.Pointer(x + i)) != 'X' {
  777. t.setErrno(errno.EINVAL)
  778. return -1
  779. }
  780. }
  781. fd, err := tempFile(template, x, 0)
  782. if err != nil {
  783. t.setErrno(err)
  784. return -1
  785. }
  786. return int32(fd)
  787. }
  788. // int mkstemp(char *template);
  789. func Xmkstemp(t *TLS, template uintptr) int32 {
  790. return Xmkstemp64(t, template)
  791. }
  792. // int mkstemp(char *template);
  793. func Xmkstemp64(t *TLS, template uintptr) int32 {
  794. return Xmkstemps64(t, template, 0)
  795. }
  796. // int random_r(struct random_data *buf, int32_t *result);
  797. func Xrandom_r(t *TLS, buf, result uintptr) int32 {
  798. randomDataMu.Lock()
  799. defer randomDataMu.Unlock()
  800. mr := randomData[buf]
  801. if stdlib.RAND_MAX != math.MaxInt32 {
  802. panic(todo(""))
  803. }
  804. *(*int32)(unsafe.Pointer(result)) = mr.Int31()
  805. return 0
  806. }
  807. // int strerror_r(int errnum, char *buf, size_t buflen);
  808. func Xstrerror_r(t *TLS, errnum int32, buf uintptr, buflen size_t) int32 {
  809. panic(todo(""))
  810. }
  811. // void endpwent(void);
  812. func Xendpwent(t *TLS) {
  813. // nop
  814. }
  815. var ctimeStaticBuf [32]byte
  816. // char *ctime(const time_t *timep);
  817. func Xctime(t *TLS, timep uintptr) uintptr {
  818. return Xctime_r(t, timep, uintptr(unsafe.Pointer(&ctimeStaticBuf[0])))
  819. }
  820. // char *ctime_r(const time_t *timep, char *buf);
  821. func Xctime_r(t *TLS, timep, buf uintptr) uintptr {
  822. ut := *(*ctime.Time_t)(unsafe.Pointer(timep))
  823. tm := time.Unix(int64(ut), 0).Local()
  824. s := tm.Format(time.ANSIC) + "\n\x00"
  825. copy((*RawMem)(unsafe.Pointer(buf))[:26:26], s)
  826. return buf
  827. }