/sbin/init
initがどうやって呼ばれるのか不思議だったので調べてみた。
init.cでは
while ((c = getopt(argc, argv, "sf")) != -1) switch (c) { case 's': requested_transition = single_user; break; case 'f': runcom_mode = FASTBOOT; break; default: warning("unrecognized flag '-%c'", c); break; }
と引数の解析をしている。
つまり、引数として考えられるのは、
initかinit sかinit fかinit .のはずと言うのがわかる。
では、どこで呼び出されているかというと、それは当然カーネルからということになるはず...
で、じっさいはinit_main.cから呼び出されている。
/* * Construct the boot flag argument. */ flagsp = flags; *flagsp++ = '-'; options = 0; if (boothowto & RB_SINGLE) { *flagsp++ = 's'; options = 1; } #ifdef notyet if (boothowto & RB_FASTBOOT) { *flagsp++ = 'f'; options = 1; } #endif /* * Move out the flags (arg 1), if necessary. */ if (options != 0) { *flagsp++ = '\0'; i = flagsp - flags; #ifdef DEBUG printf("init: copying out flags `%s' %d\n", flags, i); #endif (void)copyout((caddr_t)flags, (caddr_t)(ucp -= i), i); arg1 = ucp; } /* * Move out the file name (also arg 0). */ i = strlen(path) + 1; #ifdef DEBUG printf("init: copying out path `%s' %d\n", path, i); #endif (void)copyout((caddr_t)path, (caddr_t)(ucp -= i), i); arg0 = ucp; /* * Move out the arg pointers. */ uap = (char **)((long)ucp & ~ALIGNBYTES); (void)suword((caddr_t)--uap, 0); /* terminator */ if (options != 0) (void)suword((caddr_t)--uap, (long)arg1); (void)suword((caddr_t)--uap, (long)arg0); /* * Point at the arguments. */ SCARG(&args, path) = arg0; SCARG(&args, argp) = uap; SCARG(&args, envp) = NULL; /* * Now try to exec the program. If can't for any reason * other than it doesn't exist, complain. */ if ((error = sys_execve(p, &args, retval)) == 0) return;
う〜ん、おぼろげながら処理がわかったレベル。
っーか、suwordとかsys_execve関数なんて初めて知ったよ。
カーネル空間と、ユーザー空間のやり取りってこういう風にしていたんだね...