What is mode_t
in C?
Lots of man pages refer to mode_t
, but frustratingly, the manual does not include a page on it. (This is a general flaw in the manual: there are no pages for types.) The documentation for mode_t
is found buried in CHMOD(2)
:
A mode is created from or’d permission bit masks defined in <sys/stat.h>
:
#define S_IRWXU 0000700
#define S_IRUSR 0000400
#define S_IWUSR 0000200
#define S_IXUSR 0000100
#define S_IRWXG 0000070
#define S_IRGRP 0000040
#define S_IWGRP 0000020
#define S_IXGRP 0000010
#define S_IRWXO 0000007
#define S_IROTH 0000004
#define S_IWOTH 0000002
#define S_IXOTH 0000001
#define S_ISUID 0004000
#define S_ISGID 0002000
#define S_ISVTX 0001000
In other words, a mode_t
consists of a load of bit-packed booleans. Ignoring the last three, this gives their bit number:
|
Read? |
Write? |
Execute? |
Owner |
S_IRUSR = 1 << 8 |
S_IWUSR = 1 << 7 |
S_IXUSR = 1 << 6 |
Group |
S_IRGRP = 1 << 5 |
S_IWGRP = 1 << 4 |
S_IXGRP = 1 << 3 |
Other |
S_IROTH = 1 << 2 |
S_IWOTH = 1 << 1 |
S_IXOTH = 1 << 0 |
Those defined constants are almost as hard to read as the numbers!
Here are some examples, with some functions to manipulate mode_t
values.
#include <sys/stat.h>
#include <stdbool.h>
#include <stdio.h>
enum class { CLASS_OWNER, CLASS_GROUP, CLASS_OTHER };
enum permission { PERMISSION_READ, PERMISSION_WRITE, PERMISSION_EXECUTE };
const mode_t EMPTY_MODE = 0;
mode_t perm(enum class c, enum permission p) { return 1 << ((3-p) + (2-c)*3); }
bool mode_contains(mode_t mode, enum class c, enum permission p) { return mode & perm(c, p); }
mode_t mode_add(mode_t mode, enum class c, enum permission p) { return mode | perm(c, p); }
mode_t mode_rm(mode_t mode, enum class c, enum permission p) { return mode & ~perm(c, p); }
void strmode(mode_t mode, char * buf) {
const char chars[] = "rwxrwxrwx";
for (size_t i = 0; i < 9; i++) {
buf[i] = (mode & (1 << (8-i))) ? chars[i] : '-';
}
buf[9] = '\0';
}
int main(void) {
char buf[10];
mode_t examples[] = { 0, 0666, 0777, 0700, 0100, 01, 02, 03, 04, 05, 06, 07 };
size_t num_examples = sizeof(examples) / sizeof(examples[0]);
for (size_t i = 0; i < num_examples; i++) {
strmode(examples[i], buf);
printf("%04o is %s\n", examples[i], buf);
}
return 0;
}
This prints:
$ ./a.out
0000 is ---------
0666 is rw-rw-rw-
0777 is rwxrwxrwx
0700 is rwx------
0100 is --x------
0001 is --------x
0002 is -------w-
0003 is -------wx
0004 is ------r--
0005 is ------r-x
0006 is ------rw-
0007 is ------rwx
I’ve written the values in octal, because that’s how they’re traditionally written. It’s quite confusing, because the numbers don’t map obviously to the permission set. You have to think in binary. But in short, 1 is execute, 2 is write, 4 is read, and you add these together to get a permission set for a given class (e.g. group).
But now, what are those last three bits? I’ll cover those some other time ...
Similar posts
How do I duplicate a file descriptor in C?
Use the dup
system call to duplicate a file descriptor in C, allowing two references to the same underlying pipe. 2017-02-15
How do I close a file descriptor in C?
To close a file descriptor in C, use the close
system call. Multiple descriptors can reference the same underlying file or pipe. The pipe is only closed when all references are closed. 2017-02-16
What is a a FIFO, or “named pipe”? What is mkfifo
in C?
A FIFO is a special file that allows inter-process communication. The mkfifo
system call creates a FIFO, enabling processes to read from and write to it. 2017-02-21
What is lsof
?
lsof
lists open system resources, including pipes, sockets, and yes, files. It shows their type, owner, and location. 2017-02-20
Don’t use nscd
nscd
, a local DNS resolver within glibc
, is non-standard. Instead, use a local DNS server like named
or dnscache
. 2018-02-05
What does getaddrinfo
do?
getaddrinfo
ostensibly does DNS lookups. Sounds simple, but it uses more than 100 system calls! Let’s trace the crazy path of address lookup on Linux. 2018-02-03
More by Jim
What does the dot do in JavaScript?
foo.bar
, foo.bar()
, or foo.bar = baz
- what do they mean? A deep dive into prototypical inheritance and getters/setters. 2020-11-01
Smear phishing: a new Android vulnerability
Trick Android to display an SMS as coming from any contact. Convincing phishing vuln, but still unpatched. 2020-08-06
A probabilistic pub quiz for nerds
A “true or false” quiz where you respond with your confidence level, and the optimal strategy is to report your true belief. 2020-04-26
Time is running out to catch COVID-19
Simulation shows it’s rational to deliberately infect yourself with COVID-19 early on to get treatment, but after healthcare capacity is exceeded, it’s better to avoid infection. Includes interactive parameters and visualizations. 2020-03-14
The inception bar: a new phishing method
A new phishing technique that displays a fake URL bar in Chrome for mobile. A key innovation is the “scroll jail” that traps the user in a fake browser. 2019-04-27
The hacker hype cycle
I got started with simple web development, but because enamored with increasingly esoteric programming concepts, leading to a “trough of hipster technologies” before returning to more productive work. 2019-03-23
Project C-43: the lost origins of asymmetric crypto
Bob invents asymmetric cryptography by playing loud white noise to obscure Alice’s message, which he can cancel out but an eavesdropper cannot. This idea, published in 1944 by Walter Koenig Jr., is the forgotten origin of asymmetric crypto. 2019-02-16
How Hacker News stays interesting
Hacker News buried my post on conspiracy theories in my family due to overheated discussion, not censorship. Moderation keeps the site focused on interesting technical content. 2019-01-26
My parents are Flat-Earthers
For decades, my parents have been working up to Flat-Earther beliefs. From Egyptology to Jehovah’s Witnesses to theories that human built the Moon billions of years in the future. Surprisingly, it doesn’t affect their successful lives very much. For me, it’s a fun family pastime. 2019-01-20
The dots do matter: how to scam a Gmail user
Gmail’s “dots don’t matter” feature lets scammers create an account on, say, Netflix, with your email address but different dots. Results in convincing phishing emails. 2018-04-07
The sorry state of OpenSSL usability
OpenSSL’s inadequate documentation, confusing key formats, and deprecated interfaces make it difficult to use, despite its importance. 2017-12-02
I hate telephones
I hate telephones. Some rational reasons: lack of authentication, no spam filtering, forced synchronous communication. But also just a visceral fear. 2017-11-08
The Three Ts of Time, Thought and Typing: measuring cost on the web
Businesses often tout “free” services, but the real costs come in terms of time, thought, and typing required from users. Reducing these “Three Ts” is key to improving sign-up flows and increasing conversions. 2017-10-26
Granddad died today
Granddad died. The unspoken practice of death-by-dehydration in the NHS. The Liverpool Care Pathway. Assisted dying in the UK. The importance of planning in end-of-life care. 2017-05-19
How do I call a program in C, setting up standard pipes?
A C function to create a new process, set up its standard input/output/error pipes, and return a struct containing the process ID and pipe file descriptors. 2017-02-17
Your syntax highlighter is wrong
Syntax highlighters make value judgments about code. Most highlighters judge that comments are cruft, and try to hide them. Most diff viewers judge that code deletions are bad. 2014-05-11
Want to build a fantastic product using LLMs? I work at
Granola where we're building the future IDE for knowledge work. Come and work with us!
Read more or
get in touch! This page copyright James Fisher 2017. Content is not associated with my employer. Found an error? Edit this page.