LoginSignup
0
0

More than 1 year has passed since last update.

ふつうのLinuxプログラミングをやってみる その4

Last updated at Posted at 2020-11-04

有名な本らしいので買ってみました
ふつうのLinuxプログラミング Linuxの仕組みから学べるgccプログラミングの王道
ふつうのLinuxプログラミング 第2版 Linuxの仕組みから学べるgccプログラミングの王道

ふつうのLinuxプログラミングをやってみる その1
https://qiita.com/uturned0/items/b9ae846f2aff5865c074
ふつうのLinuxプログラミングをやってみる その2
https://qiita.com/uturned0/items/56beac990cdd6f1059ed
ふつうのLinuxプログラミングをやってみる その3
https://qiita.com/uturned0/items/675092da8aa89c4b1ff0
その4
https://qiita.com/uturned0/items/8f5765cfc0f0be8a1981
その5
https://qiita.com/uturned0/items/ab97deb489c994a836da
その6
https://qiita.com/uturned0/items/b7df48e87ae9170f3698
その7
https://qiita.com/uturned0/items/263151cf7c83bff7fac1
その7−3
https://qiita.com/uturned0/items/3fbf3ed6bb2a47325f59


Chapter 4

directoryは「他のファイルの情報を記録したファイル」と考える

directoryが -x だとその中のファイルパーミッションは関係なくアクセスできない。
基本必ずdirには +x されている
+r だけあってもだめなのね

そうだっけ?と思い試しに chmod 644 とかしてみたけど普通にdir内のfile listは見れた。中のfileはreadも何もできない。file listはrがあれば見れる。でもfileそのものの中身はxがないと見れない。あー、そうだっけか。そうかも。

ユーザAがアクセスする = user A のprocessがアクセスする. user Aをprocessが代行するので信任状、クレデンシャルと呼ぶ。俺はuser Aの許可で動いてるぞ的な証明書。警察手帳。

login時にprocessを作っている。子processにはcredencialsがコピーされる。

centosだとlogin時にbashが起動する。見るのが早い。ユーザ名を tanuki とする。

$ pstree -u tanuki
git-credential-

sshd───bash───tail        <--------これが親子関係

bash

これ面白いのが、 sshd はrootが起動してて、login後のbashその一歩手前から子ユーザになってる

$ ps -aef --forest -u tanuki
root     119287      1  0 Jul20 ?        00:00:00 /usr/sbin/sshd -D
root      37261 119287  0 22:42 ?        00:00:00  \_ sshd: tanuki [priv]
tanuki  37265  37261  0 22:42 ?        00:00:00      \_ sshd: tanuki@pts/0
tanuki  37266  37265  0 22:42 pts/0    00:00:00          \_ -bash
tanuki  37335  37266  0 22:42 pts/0    00:00:00              \_ tail -f /var/log/messages

とりあえず、tanukiがこの後何をしようと基本的にはtanukiのcredencialsでprocessを作ることになる。

4.2 shell/端末

tty = Teletype = 今ではinput/output(表示)ができる端末のこと
computerにモニタもキーボードもない時代
inputは紙テープ、outputはプリンタで紙に印刷
キャラクタ端末=キーボードがついたが画面がデジタル時計の直線な8のようなものの時代(解像度以前のレベル)
液晶ディスプレイのようになったもの
端末をソフトウェアシミュレートしたもの
進化してもしても、全部 tty = 端末
key入力と画面がセットで端末

consoleと端末はほぼ同じ意味

linuxには仮想コンソールがある。ctrl+alt+F1~6で切り替えられる。

が、アクセスの際は、リモートでなく、物理マシンから行う必要があります。sshではできない。

https://qiita.com/Kernel_OGSun/items/bdccbe8bc4092f33b43e

ASCII文字コードにはa-z 0-9以外にも謎のキャラクターがある

DEL, BS, LF, TAB など。キャラクタ端末を操作するのに使った。文字を表示するのがa-Zのような文字コードだがBSは文字を消した
文字コードだけど、命令コードが含まれている! あくまでこれが来たら何をする、という表だったのだ。文字だけを表すためのものじゃないものが、いつの間にかそう使われるようになった。へぇ。

今使ってるcentosにはttyは64個、ttySが4個あった。

$ ll /dev/tty*
crw-rw-rw- 1 root tty     5,  0 2020-11-04 09:34:24 /dev/tty
crw--w---- 1 root tty     4,  0 2020-07-16 18:30:24 /dev/tty0
crw--w---- 1 root tty     4,  1 2020-07-16 18:30:32 /dev/tty1
crw--w---- 1 root tty     4, 10 2020-07-16 18:30:24 /dev/tty10
crw--w---- 1 root tty     4, 11 2020-07-16 18:30:24 /dev/tty11
crw--w---- 1 root tty     4, 12 2020-07-16 18:30:24 /dev/tty12
crw--w---- 1 root tty     4, 13 2020-07-16 18:30:24 /dev/tty13
crw--w---- 1 root tty     4, 14 2020-07-16 18:30:24 /dev/tty14
crw--w---- 1 root tty     4, 15 2020-07-16 18:30:24 /dev/tty15
crw--w---- 1 root tty     4, 16 2020-07-16 18:30:24 /dev/tty16
crw--w---- 1 root tty     4, 17 2020-07-16 18:30:24 /dev/tty17
crw--w---- 1 root tty     4, 18 2020-07-16 18:30:24 /dev/tty18
crw--w---- 1 root tty     4, 19 2020-07-16 18:30:24 /dev/tty19
crw--w---- 1 root tty     4,  2 2020-07-16 18:30:24 /dev/tty2
crw--w---- 1 root tty     4, 20 2020-07-16 18:30:24 /dev/tty20
crw--w---- 1 root tty     4, 21 2020-07-16 18:30:24 /dev/tty21
crw--w---- 1 root tty     4, 22 2020-07-16 18:30:24 /dev/tty22
crw--w---- 1 root tty     4, 23 2020-07-16 18:30:24 /dev/tty23
crw--w---- 1 root tty     4, 24 2020-07-16 18:30:24 /dev/tty24
crw--w---- 1 root tty     4, 25 2020-07-16 18:30:24 /dev/tty25
crw--w---- 1 root tty     4, 26 2020-07-16 18:30:24 /dev/tty26
crw--w---- 1 root tty     4, 27 2020-07-16 18:30:24 /dev/tty27
crw--w---- 1 root tty     4, 28 2020-07-16 18:30:24 /dev/tty28
crw--w---- 1 root tty     4, 29 2020-07-16 18:30:24 /dev/tty29
crw--w---- 1 root tty     4,  3 2020-07-16 18:30:24 /dev/tty3
crw--w---- 1 root tty     4, 30 2020-07-16 18:30:24 /dev/tty30
crw--w---- 1 root tty     4, 31 2020-07-16 18:30:24 /dev/tty31
crw--w---- 1 root tty     4, 32 2020-07-16 18:30:24 /dev/tty32
crw--w---- 1 root tty     4, 33 2020-07-16 18:30:24 /dev/tty33
crw--w---- 1 root tty     4, 34 2020-07-16 18:30:24 /dev/tty34
crw--w---- 1 root tty     4, 35 2020-07-16 18:30:24 /dev/tty35
crw--w---- 1 root tty     4, 36 2020-07-16 18:30:24 /dev/tty36
crw--w---- 1 root tty     4, 37 2020-07-16 18:30:24 /dev/tty37
crw--w---- 1 root tty     4, 38 2020-07-16 18:30:24 /dev/tty38
crw--w---- 1 root tty     4, 39 2020-07-16 18:30:24 /dev/tty39
crw--w---- 1 root tty     4,  4 2020-07-16 18:30:24 /dev/tty4
crw--w---- 1 root tty     4, 40 2020-07-16 18:30:24 /dev/tty40
crw--w---- 1 root tty     4, 41 2020-07-16 18:30:24 /dev/tty41
crw--w---- 1 root tty     4, 42 2020-07-16 18:30:24 /dev/tty42
crw--w---- 1 root tty     4, 43 2020-07-16 18:30:24 /dev/tty43
crw--w---- 1 root tty     4, 44 2020-07-16 18:30:24 /dev/tty44
crw--w---- 1 root tty     4, 45 2020-07-16 18:30:24 /dev/tty45
crw--w---- 1 root tty     4, 46 2020-07-16 18:30:24 /dev/tty46
crw--w---- 1 root tty     4, 47 2020-07-16 18:30:24 /dev/tty47
crw--w---- 1 root tty     4, 48 2020-07-16 18:30:24 /dev/tty48
crw--w---- 1 root tty     4, 49 2020-07-16 18:30:24 /dev/tty49
crw--w---- 1 root tty     4,  5 2020-07-16 18:30:24 /dev/tty5
crw--w---- 1 root tty     4, 50 2020-07-16 18:30:24 /dev/tty50
crw--w---- 1 root tty     4, 51 2020-07-16 18:30:24 /dev/tty51
crw--w---- 1 root tty     4, 52 2020-07-16 18:30:24 /dev/tty52
crw--w---- 1 root tty     4, 53 2020-07-16 18:30:24 /dev/tty53
crw--w---- 1 root tty     4, 54 2020-07-16 18:30:24 /dev/tty54
crw--w---- 1 root tty     4, 55 2020-07-16 18:30:24 /dev/tty55
crw--w---- 1 root tty     4, 56 2020-07-16 18:30:24 /dev/tty56
crw--w---- 1 root tty     4, 57 2020-07-16 18:30:24 /dev/tty57
crw--w---- 1 root tty     4, 58 2020-07-16 18:30:24 /dev/tty58
crw--w---- 1 root tty     4, 59 2020-07-16 18:30:24 /dev/tty59
crw--w---- 1 root tty     4,  6 2020-07-16 18:30:24 /dev/tty6
crw--w---- 1 root tty     4, 60 2020-07-16 18:30:24 /dev/tty60
crw--w---- 1 root tty     4, 61 2020-07-16 18:30:24 /dev/tty61
crw--w---- 1 root tty     4, 62 2020-07-16 18:30:24 /dev/tty62
crw--w---- 1 root tty     4, 63 2020-07-16 18:30:24 /dev/tty63
crw--w---- 1 root tty     4,  7 2020-07-16 18:30:24 /dev/tty7
crw--w---- 1 root tty     4,  8 2020-07-16 18:30:24 /dev/tty8
crw--w---- 1 root tty     4,  9 2020-07-16 18:30:24 /dev/tty9
crw--w---- 1 root tty     4, 64 2020-07-16 18:30:32 /dev/ttyS0
crw-rw---- 1 root dialout 4, 65 2020-07-16 18:30:23 /dev/ttyS1
crw-rw---- 1 root dialout 4, 66 2020-07-16 18:30:23 /dev/ttyS2
crw-rw---- 1 root dialout 4, 67 2020-07-16 18:30:23 /dev/ttyS3

ttyS* は root:dialout だ。それ以外は root:tty 。数が固定ぽい。

最初の仮想コンソールが /dev/tty0. /dev/ttyS0 はRs-232C用だった歴史的には全部端末をつなぐためのもの。物理的に64個も繋ぐイメージでunixは使われていたのだろうか。

なんかへんだなと思ったら自分のsshは違うところだった

$ tty
/dev/pts/1

そこには接続数に寄って数が変わるデバイスファイルが合った

$ ll /dev/pts/*
crw--w---- 1 usera tty  136, 0 2020-11-04 23:09:36 /dev/pts/0
crw--w---- 1 usera tty  136, 1 2020-11-04 23:09:44 /dev/pts/1
crw--w---- 1 root     tty  136, 2 2020-11-04 22:43:36 /dev/pts/2
c--------- 1 root     root   5, 2 2020-07-16 18:30:22 /dev/pts/ptmx

keyboard -> (kernelに入る) -> 端末driver -> (kernelを出る) -> <ここがstream> -> process

端末は1ファイルになり、ただのデバイスになり、ストリームとしてプロセス、file systemから見えるように鳴る

shellはたぶん端末(keyboard)からのストリームを受けて実行するだけのガワ。ストリームが端末につながっている場合だけ $ とかを返す

finished chapter 4

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0