projects
/
oweals
/
musl.git
/ blob
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
history
|
raw
|
HEAD
fix fd leak on races and cancellation in ctermid
[oweals/musl.git]
/
src
/
unistd
/
ctermid.c
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
#include <fcntl.h>
5
#include <unistd.h>
6
#include <limits.h>
7
#include "syscall.h"
8
9
char *ctermid(char *s)
10
{
11
static char *s2;
12
int fd;
13
if (!s) {
14
if (!s2) s2 = malloc(L_ctermid);
15
s = s2;
16
}
17
fd = open("/dev/tty", O_WRONLY | O_NOCTTY | O_CLOEXEC);
18
if (fd < 0)
19
return strcpy(s, "");
20
if (ttyname_r(fd, s, L_ctermid))
21
strcpy(s, "");
22
__syscall(SYS_close, fd);
23
return s;
24
}