[BACK]Return to main.c CVS log [TXT][DIR] Up to [local] / prex-old / usr / server / boot

Annotation of prex-old/usr/server/boot/main.c, Revision 1.1.1.1.2.1

1.1       nbrk        1: /*-
                      2:  * Copyright (c) 2005-2007, Kohsuke Ohtani
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  * 3. Neither the name of the author nor the names of any co-contributors
                     14:  *    may be used to endorse or promote products derived from this software
                     15:  *    without specific prior written permission.
                     16:  *
                     17:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     18:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     19:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     20:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     21:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     22:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     23:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     24:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     25:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     26:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     27:  * SUCH DAMAGE.
                     28:  */
                     29:
                     30: /*
                     31:  * main.c - bootstrap server
                     32:  */
                     33:
                     34: /*
1.1.1.1.2.1! nbrk       35:  * A bootstrap server works to setup the POSIX environment for
        !            36:  * 'init' process. It sends a setup message to other servers in
        !            37:  * order to let them know that this task becomes 'init' process.
        !            38:  * The bootstrap server is gone after it launches (exec) the
        !            39:  * 'init' process.
1.1       nbrk       40:  */
                     41:
                     42: #include <prex/prex.h>
                     43: #include <sys/mount.h>
                     44: #include <server/fs.h>
                     45: #include <server/object.h>
                     46: #include <server/exec.h>
                     47: #include <server/proc.h>
                     48: #include <server/stdmsg.h>
                     49:
                     50: #include <unistd.h>
                     51: #include <string.h>
                     52: #include <stdlib.h>
                     53: #include <stdio.h>
                     54: #include <signal.h>
                     55: #include <errno.h>
                     56: #include <fstab.h>
                     57:
1.1.1.1.2.1! nbrk       58: #ifdef DEBUG
        !            59: #define DPRINTF(a) sys_log a
        !            60: #else
        !            61: #define DPRINTF(a)
        !            62: #endif
        !            63:
1.1       nbrk       64: extern const struct fstab fstab[];
                     65: extern const int fstab_size;
                     66:
                     67: #define PRIO_BOOT      131             /* priority of boot server */
                     68:
                     69: /* forward declarations */
1.1.1.1.2.1! nbrk       70: static void    wait_server(const char *);
        !            71: static void    process_init(void);
        !            72: static int     run_init(char *);
        !            73: static void    mount_fs(void);
1.1       nbrk       74:
                     75: static object_t proc_obj;
                     76:
                     77: static char *init_argv[] = { "arg", NULL };
                     78: static char *init_envp[] = { "HOME=/", NULL };
                     79:
1.1.1.1.2.1! nbrk       80: /*
        !            81:  * Base directories
        !            82:  */
1.1       nbrk       83: static char *base_dir[] = {
                     84:        "/bin",         /* essential user commands */
                     85:        "/boot",        /* static files for boot */
                     86:        "/dev",         /* device files */
                     87:        "/etc",         /* system conifguration */
                     88:        "/mnt",         /* mount point for file systems */
                     89:        "/mnt/floppy",  /* mount point for floppy */
                     90:        "/mnt/cdrom",   /* mount point for cdrom */
1.1.1.1.2.1! nbrk       91:        "/fifo",        /* mount point for fifo */
1.1       nbrk       92:        "/tmp",         /* temporary files */
                     93:        "/usr",         /* shareable read-only data */
                     94:        "/var",         /* log files, spool data */
                     95:        NULL
                     96: };
                     97:
                     98: /*
1.1.1.1.2.1! nbrk       99:  * Main routine for boot strap.
1.1       nbrk      100:  */
                    101: int
                    102: main(int argc, char *argv[])
                    103: {
                    104:
                    105:        sys_log("Starting Bootstrap Server\n");
                    106:
                    107:        /*
                    108:         * Boost current priority.
                    109:         */
                    110:        thread_setprio(thread_self(), PRIO_BOOT);
                    111:
                    112:        /*
1.1.1.1.2.1! nbrk      113:         * Wait until required system servers
        !           114:         * become available.
1.1       nbrk      115:         */
                    116:        wait_server(OBJNAME_PROC);
                    117:        wait_server(OBJNAME_FS);
                    118:        wait_server(OBJNAME_EXEC);
                    119:
                    120:        /*
                    121:         * Register this task to other servers.
                    122:         */
                    123:        process_init();
                    124:        fslib_init();
                    125:
                    126:        /*
                    127:         * Mount file systems
                    128:         */
                    129:        mount_fs();
                    130:
                    131:        /*
                    132:         * Run init process
                    133:         */
                    134:        run_init("/boot/init");
                    135:
1.1.1.1.2.1! nbrk      136:        sys_panic("boot: failed to run init");
        !           137:        /* NOTREACHED */
1.1       nbrk      138:        return 0;
                    139: }
                    140:
                    141: /*
                    142:  * Wait until specified server starts.
                    143:  */
                    144: static void
                    145: wait_server(const char *name)
                    146: {
                    147:        int i, err = 0;
                    148:        object_t obj = 0;
                    149:
                    150:        thread_yield();
                    151:
                    152:        /*
                    153:         * Wait for server loading. timeout is 2 sec.
                    154:         */
                    155:        for (i = 0; i < 200; i++) {
                    156:                err = object_lookup((char *)name, &obj);
                    157:                if (err == 0)
                    158:                        break;
                    159:
                    160:                /* Wait 10msec */
                    161:                timer_sleep(10, 0);
                    162:                thread_yield();
                    163:        }
                    164:        if (err)
                    165:                sys_panic("boot: server not found");
                    166: }
                    167:
1.1.1.1.2.1! nbrk      168: /*
        !           169:  * Notify the process server.
        !           170:  */
1.1       nbrk      171: static void
                    172: process_init(void)
                    173: {
                    174:        struct msg m;
                    175:
                    176:        /*
1.1.1.1.2.1! nbrk      177:         * We will become an init process later.
1.1       nbrk      178:         */
                    179:        object_lookup(OBJNAME_PROC, &proc_obj);
                    180:        m.hdr.code = PS_SETINIT;
                    181:        msg_send(proc_obj, &m, sizeof(m));
                    182: }
                    183:
                    184: /*
                    185:  * Run init process
                    186:  */
                    187: static int
                    188: run_init(char *path)
                    189: {
                    190:        struct exec_msg *msg;
                    191:        int err, i, argc, envc;
                    192:        size_t bufsz;
                    193:        char *dest, *src;
                    194:        object_t obj;
                    195:
1.1.1.1.2.1! nbrk      196:        DPRINTF(("boot: Run init process\n"));
1.1       nbrk      197:
                    198:        /*
                    199:         * Allocate a message buffer with arg/env data.
                    200:         */
                    201:        bufsz = 0;
                    202:        argc = 0;
1.1.1.1.2.1! nbrk      203:        while (init_argv[argc] != NULL) {
1.1       nbrk      204:                bufsz += (strlen(init_argv[argc]) + 1);
                    205:                argc++;
                    206:        }
                    207:        envc = 0;
1.1.1.1.2.1! nbrk      208:        while (init_envp[envc] != NULL) {
1.1       nbrk      209:                bufsz += (strlen(init_envp[envc]) + 1);
                    210:                envc++;
                    211:        }
1.1.1.1.2.1! nbrk      212:        msg = malloc(sizeof(struct exec_msg) + bufsz);
1.1       nbrk      213:        if (msg == NULL)
                    214:                return -1;
                    215:
                    216:        /*
                    217:         * Build exec message.
                    218:         */
                    219:        dest = (char *)&msg->buf;
                    220:        for (i = 0; i < argc; i++) {
                    221:                src = init_argv[i];
                    222:                while ((*dest++ = *src++) != 0);
                    223:        }
                    224:        for (i = 0; i < envc; i++) {
                    225:                src = init_envp[i];
                    226:                while ((*dest++ = *src++) != 0);
                    227:        }
                    228:        msg->argc = argc;
                    229:        msg->envc = envc;
                    230:        msg->bufsz = bufsz;
                    231:        strcpy((char *)&msg->path, path);
                    232:
                    233:        /*
                    234:         * Request exec() to exec server
                    235:         */
                    236:        object_lookup(OBJNAME_EXEC, &obj);
                    237:        do {
                    238:                msg->hdr.code = EX_EXEC;
                    239:                err = msg_send(obj, msg,
                    240:                               sizeof(struct exec_msg) + bufsz);
                    241:                /*
1.1.1.1.2.1! nbrk      242:                 * If exec server can execute new process
        !           243:                 * properly, it will terminate the caller task
        !           244:                 * automatically. So, the control never comes
        !           245:                 * here in that case.
1.1       nbrk      246:                 */
                    247:        } while (err == EINTR);
                    248:        return -1;
                    249: }
                    250:
                    251: static void
                    252: mount_fs(void)
                    253: {
                    254:        int i;
                    255:
1.1.1.1.2.1! nbrk      256:        DPRINTF(("boot: Mounting file systems\n"));
1.1       nbrk      257:
                    258:        /*
                    259:         * Mount RAMFS as root file system.
                    260:         */
                    261:        if (mount("", "/", "ramfs", 0, NULL) < 0)
                    262:                sys_panic("boot: mount failed");
                    263:
                    264:        /*
                    265:         * Create some default directories on RAMFS.
                    266:         */
1.1.1.1.2.1! nbrk      267:        i = 0;
        !           268:        while (base_dir[i] != NULL) {
1.1       nbrk      269:                mkdir(base_dir[i], 0);
1.1.1.1.2.1! nbrk      270:                i++;
1.1       nbrk      271:        }
                    272:
                    273:        /*
                    274:         * Mount other file systems.
                    275:         */
                    276:        for (i = 0; i < fstab_size; i++) {
                    277:                mount(fstab[i].fs_spec, fstab[i].fs_file,
                    278:                      fstab[i].fs_vfstype, 0, (void *)fstab[i].fs_mntops);
                    279:        }
                    280: }

CVSweb