int main (int argc, char **argv) { error_t err; mach_port_t bootstrap; argp_parse (&argp, argc, argv, 0, 0, 0); if (MACH_PORT_VALID (opt_device_master)) { err = open_console (opt_device_master); assert_perror (err); mach_port_deallocate (mach_task_self (), opt_device_master); } save_argv = argv; task_get_bootstrap_port (mach_task_self (), &bootstrap); if (bootstrap == MACH_PORT_NULL) error (2, 0, "Must be started as a translator"); /* Fetch our proc server port for easy use. If we are booting, it is not set yet and `getproc' returns MACH_PORT_NULL; we reset PROCSERVER in S_exec_init (below). */ procserver = getproc (); err = trivfs_add_port_bucket (&port_bucket); if (err) error (1, 0, "error creating port bucket"); err = trivfs_add_control_port_class (&trivfs_control_class); if (err) error (1, 0, "error creating control port class"); err = trivfs_add_protid_port_class (&trivfs_protid_class); if (err) error (1, 0, "error creating protid port class"); execboot_portclass = ports_create_class (deadboot, NULL); /* Reply to our parent. */ err = trivfs_startup (bootstrap, 0, trivfs_control_class, port_bucket, trivfs_protid_class, port_bucket, &fsys); /* Reply to our parent. */ err = trivfs_startup (bootstrap, 0, trivfs_control_class, port_bucket, trivfs_protid_class, port_bucket, &fsys); mach_port_deallocate (mach_task_self (), bootstrap); if (err) error (3, err, "Contacting parent"); /* Launch. */ ports_manage_port_operations_multithread (port_bucket, exec_demuxer, 2 * 60 * 1000, 0, 0); return 0; }
int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hprevinstance,PSTR cmdline,int cmdshow) { int result=0; MSG msg={0}; INITCOMMONCONTROLSEX ctrls={0}; ghinstance=hinstance; open_console(); OleInitialize(0); InitializeCriticalSection(&mutex); ctrls.dwSize=sizeof(ctrls); ctrls.dwICC = ICC_LISTVIEW_CLASSES|ICC_TREEVIEW_CLASSES|ICC_BAR_CLASSES; InitCommonControlsEx(&ctrls); ghmain=CreateDialog(ghinstance,MAKEINTRESOURCE(IDD_MAIN_DLG),NULL,MainDlg); if(!ghmain){ MessageBox(NULL,TEXT("Could not create main dialog"),TEXT("ERROR"),MB_SYSTEMMODAL|MB_ICONERROR|MB_OK); return -1; } ShowWindow(ghmain,cmdshow); while(GetMessage(&msg,NULL,0,0)) { if(!IsDialogMessage(ghmain,&msg)){ TranslateMessage(&msg); DispatchMessage(&msg); } } return result; }
static void doit (DDEV_REQ *packet) { switch (packet->header.msgtyp) { case DEV_OPN: /* デバイスのオープン */ if (!initialized) { init_console (); } open_console (packet->header.mbfid, &(packet->body.opn_req)); break; case DEV_CLS: /* デバイスのクローズ */ close_console (packet->header.mbfid, &(packet->body.cls_req)); break; case DEV_REA: read_console (packet->header.mbfid, &(packet->body.rea_req)); break; case DEV_WRI: write_console (packet->header.mbfid, &(packet->body.wri_req)); break; case DEV_CTL: control_console (packet->header.mbfid, &(packet->body.ctl_req)); break; } }
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow) { MSG msg; HACCEL haccel; int ret; ghinstance=hInstance; #ifdef _DEBUG open_console(); move_console(); #endif hwindow=CreateDialog(ghinstance,MAKEINTRESOURCE(IDD_DIALOG),NULL,MainDlg); if(!hwindow){ MessageBox(NULL,"Could not create main dialog","ERROR",MB_ICONERROR | MB_OK); return 0; } ShowWindow(hwindow,iCmdShow); UpdateWindow(hwindow); if(szCmdLine!=0 && szCmdLine[0]!=0) cmdline=szCmdLine; haccel=LoadAccelerators(ghinstance,MAKEINTRESOURCE(IDR_ACCELERATOR)); while((ret=GetMessage(&msg,NULL,0,0))!=0) { if(ret==-1) break; { int _msg,lparam,wparam; _msg=msg.message; lparam=msg.lParam; wparam=msg.wParam; if(FALSE) if(_msg!=WM_MOUSEFIRST&&_msg!=WM_NCHITTEST&&_msg!=WM_SETCURSOR&&_msg!=WM_ENTERIDLE&&_msg!=WM_DRAWITEM &&_msg!=WM_CTLCOLORBTN&&_msg!=WM_CTLCOLOREDIT) //if(_msg!=WM_NCHITTEST&&_msg!=WM_SETCURSOR&&_msg!=WM_ENTERIDLE) { static DWORD tick=0; if((GetTickCount()-tick)>500) printf("--\n"); printf("*"); print_msg(_msg,lparam,wparam); tick=GetTickCount(); } } if(haccel!=0) TranslateAccelerator(hwindow,haccel,&msg); if(!IsDialogMessage(hwindow,&msg)){ TranslateMessage(&msg); DispatchMessage(&msg); } } return msg.wParam; }
void factor_vm::init_factor(vm_parameters *p) { /* Kilobytes */ p->datastack_size = align_page(p->datastack_size << 10); p->retainstack_size = align_page(p->retainstack_size << 10); p->callstack_size = align_page(p->callstack_size << 10); p->callback_size = align_page(p->callback_size << 10); /* Megabytes */ p->young_size <<= 20; p->aging_size <<= 20; p->tenured_size <<= 20; p->code_size <<= 20; /* Disable GC during init as a sanity check */ gc_off = true; /* OS-specific initialization */ early_init(); const vm_char *executable_path = vm_executable_path(); if(executable_path) p->executable_path = executable_path; if(p->image_path == NULL) p->image_path = default_image_path(); srand((unsigned int)nano_count()); init_ffi(); init_contexts(p->datastack_size,p->retainstack_size,p->callstack_size); init_callbacks(p->callback_size); load_image(p); init_c_io(); init_inline_caching((int)p->max_pic_size); if(p->signals) init_signals(); if(p->console) open_console(); init_profiler(); special_objects[OBJ_CPU] = allot_alien(false_object,(cell)FACTOR_CPU_STRING); special_objects[OBJ_OS] = allot_alien(false_object,(cell)FACTOR_OS_STRING); special_objects[OBJ_CELL_SIZE] = tag_fixnum(sizeof(cell)); special_objects[OBJ_EXECUTABLE] = allot_alien(false_object,(cell)p->executable_path); special_objects[OBJ_ARGS] = false_object; special_objects[OBJ_EMBEDDED] = false_object; special_objects[OBJ_VM_COMPILER] = allot_alien(false_object,(cell)FACTOR_COMPILER_VERSION); /* We can GC now */ gc_off = false; if(!to_boolean(special_objects[OBJ_STAGE2])) prepare_boot_image(); }
bool FrameBuffer::open(void) { if (!open_framebuffer()) return false; if (!open_console()) return false; return true; }
void factorvm::init_factor(vm_parameters *p) { /* Kilobytes */ p->ds_size = align_page(p->ds_size << 10); p->rs_size = align_page(p->rs_size << 10); /* Megabytes */ p->young_size <<= 20; p->aging_size <<= 20; p->tenured_size <<= 20; p->code_size <<= 20; /* Disable GC during init as a sanity check */ gc_off = true; /* OS-specific initialization */ early_init(); const vm_char *executable_path = vm_executable_path(); if(executable_path) p->executable_path = executable_path; if(p->image_path == NULL) p->image_path = default_image_path(); srand(current_micros()); init_ffi(); init_stacks(p->ds_size,p->rs_size); load_image(p); init_c_io(); init_inline_caching(p->max_pic_size); init_signals(); if(p->console) open_console(); init_profiler(); userenv[CPU_ENV] = allot_alien(F,(cell)FACTOR_CPU_STRING); userenv[OS_ENV] = allot_alien(F,(cell)FACTOR_OS_STRING); userenv[CELL_SIZE_ENV] = tag_fixnum(sizeof(cell)); userenv[EXECUTABLE_ENV] = allot_alien(F,(cell)p->executable_path); userenv[ARGS_ENV] = F; userenv[EMBEDDED_ENV] = F; /* We can GC now */ gc_off = false; if(userenv[STAGE2_ENV] == F) { userenv[STACK_TRACES_ENV] = tag_boolean(p->stack_traces); do_stage1_init(); } }
/* These are the special functions that perform special actions */ int twiddler_console(char *s) { int consolenr = atoi(s); /* atoi never fails :) */ int fd; if (consolenr == 0) return 0; /* nothing to do */ fd=open_console(O_RDONLY); if (fd < 0) return -1; if (ioctl(fd, VT_ACTIVATE, consolenr)<0) {close(fd); return -1;} /*if (ioctl(fd, VT_WAITACTIVE, consolenr)<0) {close(fd); return -1;} */ close(fd); return 0; }
/* Set the device mode */ void pcfb_set_mode(int mode) { int i, mode1; open_console(); cur_mode = mode; mode1 = -1; if (mode == 0x10) mode = SW_ENH_CG640; #ifdef SW_VGA12 else if (mode == 0x12) mode = SW_VGA12; #endif else if (mode == 0x03) { #ifdef SW_VGA80x25 mode = SW_VGA80x25; mode1 = SW_ENHC80x25; #else mode = SW_ENHC80x25; #endif } else { eprintf1("can not set to video mode %d\n", mode); exit(1); } i = ioctl(console_fd, mode, 0L); if (i == -1 && mode1 != -1) i = ioctl(console_fd, mode1, 0L); if (i == -1) { ega_close((gx_device *) NULL); eprintf("unable to set console mode\n"); perror("pcfb_set_mode"); exit(1); } #ifdef VGA_IOPRIVL if (ioctl(console_fd, VGA_IOPRIVL, 1) == -1) { ega_close((gx_device *) NULL); eprintf("unable to get I/O privilege\n"); perror("pcfb_set_mode"); exit(1); } #endif i = ioctl(console_fd, MAPCONS, 0L); if (i == -1) { ega_close((gx_device *) NULL); eprintf("unable to map console adaptor's display memory\n"); perror("pcfb_set_mode"); exit(1); } fb_addr = (fb_ptr) (i); }
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow) { MSG msg; ghInstance=hInstance; #ifdef _DEBUG open_console(); #endif hWindow=CreateDialog(ghInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,MainDlg); if(!hWindow){ MessageBox(NULL,"Could not create main dialog","ERROR",MB_ICONERROR | MB_OK); return 0; } ShowWindow(hWindow,iCmdShow); UpdateWindow(hWindow); #ifdef _DEBUG adjust_windows(hWindow); #endif while(GetMessage(&msg,NULL,0,0)) { if(!IsDialogMessage(hWindow,&msg)){ // Translate messages for the dialog TranslateMessage(&msg); DispatchMessage(&msg); } else{ // debug_printf("msg.message=%08X msg.lparam=%08X msg.wparam=%08X\n",msg.message,msg.lparam,msg.wparam); // DispatchMessage(&msg); //if(msg.message == WM_KEYDOWN && msg.wparam!=VK_ESCAPE){ if((msg.message == WM_KEYDOWN && msg.wParam!=VK_ESCAPE && msg.wParam!=VK_SHIFT && msg.wParam!=VK_CONTROL) || (msg.message==WM_KEYUP && msg.wParam!=VK_ESCAPE && msg.wParam!=VK_SHIFT && msg.wParam!=VK_CONTROL)){ static DWORD time=0; //if((GetTickCount()-time)>100){ { //if(screen_updated) { screen_updated=FALSE; SendMessage(hWindow,msg.message,msg.wParam,msg.lParam); time=GetTickCount(); } } } } } return msg.wParam; }
/* Get things started */ void init_factor(F_PARAMETERS *p) { /* Kilobytes */ p->ds_size = align_page(p->ds_size << 10); p->rs_size = align_page(p->rs_size << 10); /* Megabytes */ p->young_size <<= 20; p->aging_size <<= 20; p->tenured_size <<= 20; p->code_size <<= 20; /* Disable GC during init as a sanity check */ gc_off = true; /* OS-specific initialization */ early_init(); if(p->image == NULL) p->image = default_image_path(); srand(current_micros()); init_ffi(); init_stacks(p->ds_size,p->rs_size); load_image(p); init_c_io(); init_signals(); if(p->console) open_console(); stack_chain = NULL; profiling_p = false; performing_gc = false; last_code_heap_scan = NURSERY; collecting_aging_again = false; userenv[CPU_ENV] = tag_object(from_char_string(FACTOR_CPU_STRING)); userenv[OS_ENV] = tag_object(from_char_string(FACTOR_OS_STRING)); userenv[CELL_SIZE_ENV] = tag_fixnum(sizeof(CELL)); userenv[STACK_TRACES_ENV] = tag_boolean(p->stack_traces); /* We can GC now */ gc_off = false; if(!stage2) do_stage1_init(); }
void cmd_start(struct CONSOLE *cons, char *cmdline, int memtotal) { struct SHTCTL *shtctl = (struct SHTCTL *) *((int *) 0x0fe4); struct SHEET *sht = open_console(shtctl, memtotal); struct FIFO32 *fifo = &sht->task->fifo; int i; sheet_slide(sht, 32, 4); sheet_updown(sht, shtctl->top); /* コマンドラインに入力された文字列を、一文字ずつ新しいコンソールに入力 */ for (i = 6; cmdline[i] != 0; i++) { fifo32_put(fifo, cmdline[i] + 256); } fifo32_put(fifo, 10 + 256); /* Enter */ cons_newline(cons); return; }
int main(int argc, char *argv[]) { install_crash_handler(); #ifdef _WIN32 open_console(); #endif #ifdef CHOWDREN_ENABLE_STEAM extern int init_steam(); int ret = init_steam(); if (ret != 0) return ret; #endif manager.run(); return 0; }
/* Output to a port */ void outportb(uint port, byte data) { int i; struct port_io_arg pio; if (console_fd == -1) open_console(); pio.args[0].dir = OUT_ON_PORT; pio.args[0].port = port; pio.args[0].data = data; pio.args[1].port = 0; pio.args[2].port = 0; pio.args[3].port = 0; i = ioctl(console_fd, CONSIO, (long)(&pio)); if (i == -1) { ega_close((gx_device *) NULL); eprintf("error setting device register\n"); perror("outportb"); exit(1); } }
/* * プロセスマネージャの初期化 * */ static void init_process_manager () { /* * */ int error; open_console (); /* コンソールのオープン */ init_processinfo (); /* プロセス情報の初期化 */ recvport = get_port (sizeof (struct pm_msg), sizeof (struct pm_msg)); if (recvport == 0) /* メッセージポートの取得に失敗した */ { /* error */ } error = regist_port (PROCESS_MANAGER_NAME, recvport); if (error != E_OK) /* メッセージポートの登録に失敗した */ { /* error */ } }
/* And this uses the item to push keys */ static inline int twiddler_use_item(char *item) { int fd = open_console(O_WRONLY); int i, retval = 0; unsigned char pushthis, unblank=4; /* 4 == TIOCLINUX unblank */ /* a special function */ /* a single byte */ if (((unsigned long)item & 0xff) == (unsigned long)item) { pushthis = (unsigned long)item & 0xff; retval = ioctl(fd,TIOCSTI,&pushthis); } else if(i = (struct twiddler_active_fun *)item - twiddler_active_funs, i>=0 && i < active_fun_nr) twiddler_do_fun(i); else /* a string */ for (; *item!='\0' && retval==0; item++) retval = ioctl(fd,TIOCSTI,item); ioctl(fd,TIOCLINUX,&unblank); if (retval) gpm_report(GPM_PR_ERR,GPM_MESS_IOCTL_TIOCSTI, option.progname, strerror(errno)); close(fd); return retval; }
int wait_text(int *fdptr) { int fd; int kd_mode; close(*fdptr); do { sleep(2); fd = open_console(O_RDONLY); if (ioctl(fd, KDGETMODE, &kd_mode)<0) gpm_report(GPM_PR_OOPS,GPM_MESS_IOCTL_KDGETMODE); close(fd); } while (kd_mode != KD_TEXT); /* reopen, reinit (the function is only used if we have one mouse device) */ if ((*fdptr=open((which_mouse->opt_dev),O_RDWR))<0) gpm_report(GPM_PR_OOPS,GPM_MESS_OPEN,(which_mouse->opt_dev)); if ((which_mouse->m_type)->init) (which_mouse->m_type)=((which_mouse->m_type)->init)(*fdptr, (which_mouse->m_type)->flags, (which_mouse->m_type), mouse_argc[1], mouse_argv[1]); return (1); }
/* * Run a shell script. * Returns 0 on success, otherwise the next transition to enter: * - single_user if fork/execv/waitpid failed, or if the script * terminated with a signal or exit code != 0. * - death_single if a SIGTERM was delivered to init(8). */ static state_func_t run_script(const char *script) { pid_t pid, wpid; int status; char *argv[4]; const char *shell; struct sigaction sa; shell = get_shell(); if ((pid = fork()) == 0) { sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = SIG_IGN; sigaction(SIGTSTP, &sa, (struct sigaction *)0); sigaction(SIGHUP, &sa, (struct sigaction *)0); open_console(); char _sh[] = "sh"; char _autoboot[] = "autoboot"; argv[0] = _sh; argv[1] = __DECONST(char *, script); argv[2] = runcom_mode == AUTOBOOT ? _autoboot : 0; argv[3] = 0; sigprocmask(SIG_SETMASK, &sa.sa_mask, (sigset_t *) 0); #ifdef LOGIN_CAP setprocresources(RESOURCE_RC); #endif execv(shell, argv); stall("can't exec %s for %s: %m", shell, script); _exit(1); /* force single user mode */ }
/* Read the device mode */ void pcfb_get_state(pcfb_bios_state * pbs) { int mode; open_console(); mode = ioctl(console_fd, CONS_CURRENT, 0L); if (mode == -1) { #ifdef __linux__ mode = M_ENH_C80x25; #else ega_close((gx_device *) NULL); eprintf("unable to get current console mode\n"); perror("pcfb_get_state"); exit(1); #endif } pbs->display_mode = (mode == M_ENH_CG640 || mode == M_CG640x350 ? 0x10 : #ifdef M_VGA12 mode == M_VGA12 ? 0x12 : #endif 0x03); }
int main(int argc, char **argv) { int result = 0; int i; userui_ops[0] = &userui_text_ops; userui_ops[1] = FBSPLASH_OPS; userui_ops[2] = USPLASH_OPS; active_ops = &userui_text_ops; handle_params(argc, argv); setup_signal_handlers(); open_console(); open_misc(); if (!test_run) { open_netlink(); get_nofreeze(); get_info(); } lock_memory(); prepare_console(); /* Initialise all that we can, use the first */ // active_ops = NULL; for (i = 0; i < NUM_UIS; i++) { if (userui_ops[i] && userui_ops[i]->load) { result = userui_ops[i]->load(); if (result) { if (test_run) fprintf(stderr, "Failed to initialise %s module.\n", userui_ops[i]->name); else printk("Failed to initialise %s module.\n", userui_ops[i]->name); } else if (!active_ops) active_ops = userui_ops[i]; } } if (active_ops->prepare) active_ops->prepare(); register_keypress_handler(); need_cleanup = 1; running = 1; result = nice(1); if (active_ops->memory_required) reserve_memory(active_ops->memory_required()); else reserve_memory(4*1024*1024); /* say 4MB */ enforce_lifesavers(); if (test_run) { safe_to_exit = 0; do_test_run(); return 0; } if (send_ready()) message_loop(); /* The only point we ever reach here is if message_loop crashed out. * If this is the case, we should spin for a few hours before exiting to * ensure that we don't corrupt stuff on disk (if we're past the atomic * copy). */ sleep(60*60*1); /* 1 hours */ _exit(1); }
void HariMain(void) { int i; int mmx = -1, mmy = -1, mmx2 = 0; struct SHEET *sht = 0, *key_win; struct BOOTINFO *binfo = (struct BOOTINFO *)0x0ff0; int xsize = (*binfo).scrnx; int ysize = (*binfo).scrny; int mx = xsize/2; int my = ysize/2; int new_mx = -1, new_my = 0, new_wx = 0x7fffffff, new_wy = 0; int fifobuf[128], keycmd_buf[32], *cons_fifo[2]; struct MOUSE_DEC mdec; unsigned char s[32]; unsigned int memtotal; struct MEMMAN *memman = (struct MEMMAN *)MEMMAN_ADDR; int key_shift = 0, key_leds = (binfo->leds >> 4) & 7, keycmd_wait = -1; struct SHTCTL *shtctl; struct SHEET *sht_back, *sht_mouse; unsigned char *buf_back, buf_mouse[256], *buf_cons[2]; struct TIMER *timer; struct FIFO32 fifo, keycmd; struct TASK *task_a, *task_cons[2]; static char keytable0[0x80] = { 0, 0, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '^', 0x08, 0, 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '@', '[', 0x0a, 0, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ';', ':', 0, 0, ']', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', ',', '.', '/', 0, '*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x5c, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x5c, 0, 0 }; static char keytable1[0x80] = { 0, 0, '!', 0x22, '#', '$', '%', '&', 0x27, '(', ')', '~', '=', '~', 0x08, 0, 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '`', '{', 0x0a, 0, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', '+', '*', 0, 0, '}', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 0, '*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '_', 0, 0, 0, 0, 0, 0, 0, 0, 0, '|', 0, 0 }; init_gdtidt (); init_pic (); io_sti (); fifo32_init(&fifo, 32, fifobuf, 0); fifo32_init(&keycmd, 32, keycmd_buf, 0); init_pit(); io_out8(PIC0_IMR, 0xf8); /* Allow PIT and Keyboard (11111000) */ io_out8(PIC1_IMR, 0xef); /* Allow Mouse (11101111) */ init_keyboard (&fifo, 256); enable_mouse (&fifo, 512, &mdec); timer = timer_alloc(); timer_init(timer, &fifo, 1); timer_settime(timer, 50); memtotal = memtest(0x00400000, 0xbfffffff); memman_init (memman); memman_free (memman, 0x00001000, 0x009e000); /* 0x00001000 - 0x0009efff */ memman_free (memman, 0x00400000, memtotal - 0x00400000); init_pallete(); shtctl = shtctl_init (memman, binfo->vram, binfo->scrnx, binfo->scrny); sht_back = sheet_alloc(shtctl); sht_mouse = sheet_alloc(shtctl); buf_back = (unsigned char *)memman_alloc_4k (memman, binfo->scrnx * binfo->scrny); sheet_setbuf (sht_back, buf_back, binfo->scrnx, binfo->scrny, -1); sheet_setbuf (sht_mouse, buf_mouse, 16, 16, 99); init_screen (buf_back, xsize, ysize); init_mouse_cursor8 (buf_mouse, 99); *((int *) 0x0fec) = (int) &fifo; //===================== // Task Settings //===================== task_a = task_init(memman); fifo.task = task_a; task_run (task_a, 1, 0); /* console sheet */ key_win = open_console (shtctl, memtotal); //======================= // Sheet Setting //======================= *((int *) 0x0fe4) = (int) shtctl; sheet_slide (sht_back, 0, 0); sheet_slide (key_win, 32, 4); sheet_slide (sht_mouse, mx, my); sheet_updown (sht_back, 0); sheet_updown (key_win, 1); sheet_updown (sht_mouse, 2); sheet_refresh (sht_back, 0, 0, binfo->scrnx, 48); keywin_on (key_win); fifo32_put (&keycmd, KEYCMD_LED); fifo32_put (&keycmd, key_leds); for (;;) { if (fifo32_status(&keycmd) > 0 && keycmd_wait < 0) { keycmd_wait = fifo32_get(&keycmd); wait_KBC_sendready (); io_out8(PORT_KEYDAT, keycmd_wait); } io_cli(); if (fifo32_status(&fifo) == 0) { /* execute draw if FIFO is empty */ if (new_mx >= 0) { io_sti (); sheet_slide (sht_mouse, new_mx, new_my); new_mx = -1; } else if (new_wx != 0x7fffffff) { io_sti (); sheet_slide (sht, new_wx, new_wy); new_wx = 0x7fffffff; } else { task_sleep(task_a); io_sti(); } } else { i = fifo32_get(&fifo); io_sti(); if (key_win != 0 && key_win->flags == 0) { // input Window is closed if (shtctl->top == 1) { // ony mouse and background key_win = 0; } else { key_win = shtctl->sheets[shtctl->top - 1]; keywin_on (key_win); } } if (256 <= i && i <= 511) { // Keyboard Data if (i < 0x80 + 256) { if (key_shift == 0) { s[0] = keytable0[i - 256]; } else { s[0] = keytable1[i - 256]; } } else { s[0] = 0; } if ('A' <= s[0] && s[0] <= 'Z') { if (((key_leds & 4) == 0 && key_shift == 0) || ((key_leds & 4) != 0 && key_shift != 0)) { s[0] += 0x20; } } if (s[0] != 0 && key_win != 0) { fifo32_put(&key_win->task->fifo, s[0] + 256); } if (i == 256 + 0x0f && key_win != 0) { // Tab keywin_off (key_win); int j = key_win->height - 1; if (j == 0) { j = shtctl->top - 1; } key_win = shtctl->sheets[j]; keywin_on (key_win); } if (i == 256 + 0x2a) { // Left Shift ON key_shift |= 1; } if (i == 256 + 0x36) { // Right Shift ON key_shift |= 2; } if (i == 256 + 0xaa) { // Left Shift OFF key_shift &= ~1; } if (i == 256 + 0xb6) { // Right Shift ON key_shift &= ~2; } if (i == 256 + 0x3a) { // CapsLock key_leds ^= 4; fifo32_put(&keycmd, KEYCMD_LED); fifo32_put(&keycmd, key_leds); } if (i == 256 + 0x45) { // NumLock key_leds ^= 2; fifo32_put(&keycmd, KEYCMD_LED); fifo32_put(&keycmd, key_leds); } if (i == 256 + 0x46) { // ScrollLock key_leds ^= 1; fifo32_put(&keycmd, KEYCMD_LED); fifo32_put(&keycmd, key_leds); } if (i == 256 + 0x3b && key_shift != 0 && key_win != 0) { struct TASK *task = key_win->task; if (task != 0 && task->tss.ss0 != 0) { /* Shift+F1 */ cons_putstr0 (task->cons, "\nBreak(key) : \n"); io_cli (); task->tss.eax = (int) &(task->tss.esp0); task->tss.eip = (int) asm_end_app; io_sti (); } } if (i == 256 + 0x3c && key_shift != 0) { /* Shift+F2 */ if (key_win != 0) { keywin_off (key_win); } key_win = open_console (shtctl, memtotal); sheet_slide (key_win, 32, 4); sheet_updown (key_win, shtctl->top); keywin_on (key_win); } if (i == 256 + 0x57) { /* F11 */ sheet_updown (shtctl->sheets[1], shtctl->top-1); } if (i == 256 + 0xfa) { // KeyBoard receive Data keycmd_wait = -1; } if (i == 256 + 0xfe) { // KeyBoard failed to receive Data wait_KBC_sendready (); io_out8(PORT_KEYDAT, keycmd_wait); } } else if (512 <= i && i <= 767) { // Mouse Data if (mouse_decode(&mdec, i-512) != 0) { mx += mdec.x; my += mdec.y; if (mx < 0) { mx = 0; } if (mx > binfo->scrnx - 1) { mx = binfo->scrnx - 1; } if (my < 0) { my = 0; } if (my > binfo->scrny - 1) { my = binfo->scrny - 1; } // sprintf(s, "(%d, %d)", mx, my); // putfonts8_asc_sht (sht_back, 0, 0, COL8_FFFFFF, COL8_008484, s, 10); sheet_slide (sht_mouse, mx, my); new_mx = mx; new_my = my; if ((mdec.btn & 0x01) != 0) { /* Left Mouse Button is down */ if (mmx < 0) { /* Normal Mode */ for (int j = shtctl->top - 1; j > 0; j--) { sht = shtctl->sheets[j]; int x = mx - sht->vx0; int y = my - sht->vy0; if (0 <= x && x < sht->bxsize && 0 <= y && y < sht->bysize) { if (sht->buf[y * sht->bxsize + x] != sht->col_inv) { sheet_updown (sht, shtctl->top - 1); if (sht != key_win) { keywin_off (key_win); key_win = sht; keywin_on (key_win); } if (3 <= x && x < sht->bxsize - 3 && 3 <= y && y < 21) { mmx = mx; mmy = my; mmx2 = sht->vx0; new_wy = sht->vy0; } if (sht->bxsize - 21 <= x && x < sht->bxsize - 5 && 5 <= y && y < 19) { if ((sht->flags & 0x10) != 0) { struct TASK *task = sht->task; cons_putstr0 (task->cons, "\nBreak(mouse) :\n"); io_cli (); task->tss.eax = (int) & (task->tss.esp0); task->tss.eip = (int) asm_end_app; io_sti (); } else { struct TASK *task = sht->task; io_cli (); fifo32_put (&task->fifo, 4); io_sti (); } } break; } } } } else { // Moving window mode int x = mx - mmx; int y = my - mmy; new_wx = (mmx2 + x + 2) & ~3; new_wy = new_wy + y; mmy = my; } } else { mmx = -1; if (new_wx != 0x7fffffff) { sheet_slide (sht, new_wx, new_wy); new_wx = 0x7fffffff; } } } } else if (768 <= i && i <= 1023) { close_console (shtctl->sheets0 + (i - 768)); } else if (1024 <= i && i < 2023) { close_constask (taskctl->tasks0 + (i - 1024)); } } } }
void HariMain(void) { /* **最初的这部分变量没有通过内存管理来分配,它们本身属于操作系统的一部分,存在于bootpack.hrb所在的那块儿内存空间中 */ BOOTINFO *binfo = (BOOTINFO *) ADR_BOOTINFO; MOUSE_DECODE mouse_dec; MEMMANAGE *memmanage = (MEMMANAGE *)MEMMANAGE_ADDR; LAYER_MANAGE *layer_manage; LAYER *layer_bg, *layer_mouse; FIFO fifo_mutual; //缓冲区管理,所有中断公用的 FIFO keycmd_fifo; TASK *task_a, *task_console[2], *task; //后面的那个task只是作为一个临时变量来用的 CONSOLE *console; int fifobuf[128], keycmd_buf[32], *console_fifo[2]; //缓冲区 int mCursorX, mCursorY; //鼠标光标显示位置的横纵坐标 int key_shift = 0, key_ctrl = 0, key_leds = (binfo->leds >> 4) & 7, keycmd_wait = -1; //标志键盘输入到哪个窗口中 int j, x, y, last_mcursorX = -1, last_mcursorY= -1, tmp_layerX = 0; //last_mcursorX这两个变量记录的是鼠标移动之前的坐标 int new_mcursorX = 0, new_mcursorY = 0, new_layerX = 0x7fffffff, new_layerY = 0; LAYER *layer = 0, *key_to_window; //key_to_window这个变量记录当前的键盘输入到哪个窗口中 unsigned int memory_total, i; char buf_cursor[mCursorWidth * mCursorHeight]; unsigned char *buf_bg, *buf_console[2]; //屏幕的大背景会在init_screen的时候画出来,这里只需要一个指向它的指针即可 unsigned char strings[40]; mCursorX = binfo->scrnx / 2; mCursorY = (binfo->scrny - 28)/ 2; //减去下方任务栏的高度 /*内存检查*/ i = memtest(0x00400000, 0xbfffffff) / (1024 * 1024); //i的单位是MB /*内存管理*/ memmanage_init(memmanage); memory_total = i * 1024 * 1024; memmanage_free_4K(memmanage, 0x00001000, 0x0009e000); memmanage_free_4K(memmanage, 0x00400000, memory_total - 0x00400000); init_fifo(&keycmd_fifo, 32, keycmd_buf, 0); /*初始化接收中断的缓冲区*/ init_fifo(&fifo_mutual, 128, fifobuf, 0); //初始化mouseFIFO缓冲区,现在还没任务,先指定为0 /*初始化GDT和IDT表以及PIC板的数据*/ init_GDTandIDT(); //初始化GDT和IDT表 init_pic(); //初始化PIC主从板数据,除了IRQ2禁止了全部中断 io_sti(); //开始接收中断 /*初始化PIT中断控制*/ init_PIT(); /*若要接收鼠标中断需要两个步骤,首先必须使鼠标控制电路(就是键盘控制电路的一部分)有效,然后要使鼠标本身有效*/ init_keyboard(&fifo_mutual, 256); //初始化键盘控制器电路 enable_mouse(&fifo_mutual, 512, &mouse_dec); //激活鼠标 /*开放各种中断*/ io_out8(PIC0_IMR, 0xf8); //PIC0开发IRQ(11111000),开放IRQ0、IRQ1和IRQ2,定时器、键盘中断和从PIC板 io_out8(PIC1_IMR, 0xef); //PIC1开放IRQ(11101111), 开放鼠标中断 /*初始化任务切换管理*/ task_a = task_init(memmanage); //这个task_a其实代表的就是这些鼠标键盘等的任务 fifo_mutual.task = task_a; //为鼠标键盘等的缓冲区指定唤醒任务为task_a task_run(task_a, 1, 2); /*初始化调色板,为图形界面做准备*/ init_palette(); //初始化调色板 /*初始化图层管理,并且初始化鼠标光标和背景的图层*/ layer_manage = layer_man_init(memmanage, binfo->vram, binfo->scrnx, binfo->scrny); *((int *) 0x0fe4) = (int) layer_manage; layer_bg = layer_alloc(layer_manage); //为背景分配图层 layer_mouse = layer_alloc(layer_manage); //为鼠标分配图层 buf_bg = (unsigned char *)memmanage_alloc_4K(memmanage, binfo->scrnx * binfo->scrny); //为背景图形的内容分配内存 /*为各个图形的图层内容进行设定*/ layer_set(layer_bg, buf_bg, binfo->scrnx, binfo->scrny, -1); layer_set(layer_mouse, buf_cursor, 16, 16 ,99); /*初始化整个桌面背景*/ init_screen(buf_bg, binfo->scrnx, binfo->scrny); //这个时候的init_screen不再是直接画出背景,而是在mBg内存地址中填写好背景内容 layer_slide(layer_bg, 0, 0); //把背景图层从(0,0)坐标开始画 /*初始化鼠标图标*/ init_mouse_cursor(buf_cursor, 99); //初始化鼠标光标 layer_slide(layer_mouse, mCursorX, mCursorY); //现在显示图形不需要再用displayShape函数了,直接用这个图层管理的绘图函数就行 /*执行两个命令行窗口任务*/ key_to_window = open_console(layer_manage, memory_total); //最初的时候由第一个console接收键盘输入 layer_slide(key_to_window, 8, 2); /*设置好各个图层的高度*/ layer_switch(layer_bg, 0); //把背景图层调为最底层,高度为0 layer_switch(key_to_window, 1); //命令行窗口图层调节为第三层,高度为2 layer_switch(layer_mouse, 2); //鼠标图层调为最高层,高度为3 keywindow_on(key_to_window); // fifo_put(&keycmd_fifo, KEYCMD_LED); fifo_put(&keycmd_fifo, key_leds); *((int *) 0x0fec) = (int) &fifo_mutual; for(;;) { if(fifo_status(&keycmd_fifo) > 0 && keycmd_wait < 0) { keycmd_wait = fifo_get(&keycmd_fifo); wait_KBC_sendready(); io_out8(PORT_KEYDATA, keycmd_wait); } /*只有在从中断返回的缓冲区中读取数据的时候才需要禁止中断,因为如果这个时候来了中断而没有禁止的话, **有可能读脏数据,即把还没有读出的数据的给抹掉或换成别的数据 */ io_cli(); if(0 == fifo_status(&fifo_mutual)) //当前没有中断产生 { //利用CPU空闲的时间来执行耗时的绘图工作(这样的话会造成反应很快的表象,但其实,如果中断很多,尤其是鼠标中断很多的时候, //会把之前记录下的new_mcursorX覆盖掉,而只绘制最新的图层坐标) if(new_mcursorX >= 0) { io_sti(); layer_slide(layer_mouse, new_mcursorX, new_mcursorY); new_mcursorX = -1; //绘制完鼠标之后,就把它的坐标设置为负数,反正鼠标不会出去屏幕,为负数的时候就说明不需要重绘鼠标 } //图层的横坐标为0x7fffffff的时候就不需要重绘了,这是作为一个标志的,它为其他值的时候就需要重绘了 else if(new_layerX != 0x7fffffff) { io_sti(); layer_slide(layer, new_layerX, new_layerY); new_layerX = 0x7fffffff; } //如果不需要重绘鼠标和图层的话,就没有什么工作可做了,进入休眠就可以了 else { task_sleep(task_a); //如果没有需要处理的数据,就自己让自己休眠 /*上面的那个函数直接就跳转到另一个任务中去了,不过,因为每个人物的默认eflags都是开启中断的, **所以不用担心,这个任务还是能被唤醒的,耳环醒后的第一个动作就是为了以防万一先开启中断 */ io_sti(); //万一只有自己在运行的话,则无法睡眠,那么就执行hlt指令好了,这样省电嘛 } } else { i = fifo_get(&fifo_mutual); io_sti(); //窗口被关闭的情况 if(0 != key_to_window && 0 == key_to_window->flags) { //如果当前画面上只剩下鼠标和背景的时候 if(1 == layer_manage->top) { key_to_window = 0; } else { key_to_window = layer_manage->layers_order[layer_manage->top - 1]; //当窗口被关闭的时候,自动切换成最上层的layer接收键盘输入 keywindow_on(key_to_window); } } //如果是键盘中断的话,需要处理键盘发送来的中断数据 if((keyboard_offset <= i) && (i <= keyboard_offset + 255)) { //判断shift键是否按下,然后使用不同的keytable表 if(i < 0x80 + keyboard_offset) { if(0 == key_shift) { strings[0] = key_table[i - keyboard_offset]; } else { strings[0] = key_table_shift[i - keyboard_offset]; } } else { strings[0] = 0; } //判断Caps和Shift键的情况以确定输入大写还是小写字母 if('A' <= strings[0] && strings[0] <= 'Z') { //小写字母的情况 if((0 == (key_leds & 4) && 0 == key_shift) || (0 != (key_leds & 4) && 0 != key_shift)) { strings[0] += 0x20; } } //判断按下的字符是否为一般字符,0对应的字符不处理,对退格键和回车键加上了对应字符,他们对应的table表里的元素不再是0 if(strings[0] != 0 && key_to_window != 0) { fifo_put(&key_to_window->task->fifo, strings[0] + 256); } //如果按下了ctrl+d,就打开一个console if(('D' == strings[0] || 'd' == strings[0]) && (0 != key_ctrl)) { if(key_to_window != 0) { keywindow_off(key_to_window); } key_to_window = open_console(layer_manage, memory_total); layer_slide(key_to_window, 300, 2); layer_switch(key_to_window, layer_manage->top); //自动把键盘输入切换到这个新打开的console中 keywindow_on(key_to_window); } //如果同时按下了ctrl+x,那么就关闭应用程序 if(('X' == strings[0] || 'x' == strings[0]) && (0 != key_ctrl) && key_to_window != 0) { task = key_to_window->task; //找到当前接收键盘输入的(即最前面的)图层所对应的任务 if(0 != task && task->tss.ss0 != 0) { console_putstring_toend(task->console, "\nTerminate program! :\n"); io_cli(); //让CPU直接跳转到console中执行,不再管应用程序,也就是所谓的停止应用程序继续运行 task->tss.eax = (int) &(task->tss.esp0); task->tss.eip = (int) asm_end_app; io_sti(); } } /*这些都是那些不能打印的特殊按键,它们对应的都是0*/ //按下“TAB键”的处理情况,切换输入窗口 if(i == keyboard_offset + 0x0f && key_to_window != 0) { //先让当前的窗口的光标OFF掉 keywindow_off(key_to_window); j = key_to_window->height - 1; if(0 == j) { //如果切换之后的图层是最底层的操作系统的图层,那么就切换成最上层的图层 j = layer_manage->top - 1; } key_to_window = layer_manage->layers_order[j]; //使切换到的窗口的光标ON keywindow_on(key_to_window); } //左或右Ctrl ON if(0x1d + keyboard_offset == i) { key_ctrl = 1; } //左或右Ctrl OFF if(0x9d + keyboard_offset == i) { key_ctrl = 0; } //左shift ON if(0x2a + keyboard_offset == i) { key_shift |= 1; } //右shift ON if(0x36 + keyboard_offset == i) { key_shift |= 2; } //左shift OFF if(0xaa + keyboard_offset == i) { key_shift &= ~1; } //左shift OFF if(0xb6 + keyboard_offset == i) { key_shift &= ~2; } /*对各种锁定键的处理*/ //CapsLock键 if(i == keyboard_offset + 0x3a) { key_leds ^= 4; fifo_put(&keycmd_fifo, KEYCMD_LED); fifo_put(&keycmd_fifo, key_leds); } //NumLock键 if(i == keyboard_offset + 0x45) { key_leds ^= 2; fifo_put(&keycmd_fifo, KEYCMD_LED); fifo_put(&keycmd_fifo, key_leds); } //ScrollLock键 if(i == keyboard_offset + 0x46) { key_leds ^= 1; fifo_put(&keycmd_fifo, KEYCMD_LED); fifo_put(&keycmd_fifo, key_leds); } //键盘成功接收到数据 if(i == keyboard_offset + 0xfa) { keycmd_wait = -1; } //键盘没有成功接收到数据 if(i == keyboard_offset + 0xfe) { wait_KBC_sendready(); io_out8(PORT_KEYDATA, keycmd_wait); } } //如果是鼠标中断的话,需要处理鼠标发送来的中断数据 else if((mouse_offset <= i) && (i <= mouse_offset + 255)) { if(0 != mouse_decode(&mouse_dec, i - mouse_offset)) //只有返回值为1的时候才说明成功接收完成一次鼠标的中断 { /*鼠标的移动*/ //根据mouse_dec里存储的鼠标信息画出新的鼠标图像 mCursorX += mouse_dec.x; mCursorY += mouse_dec.y; //不能让鼠标移出画面 if(mCursorX < 0) { mCursorX = 0; } if(mCursorY < 0) { mCursorY = 0; } if(mCursorX > binfo->scrnx - 1) { mCursorX = binfo->scrnx - 1; } if(mCursorY > binfo->scrny - 1) { mCursorY = binfo->scrny - 1; } //记录下鼠标光标的移动到的位置,等待fifo中为空的时候开始绘图 new_mcursorX = mCursorX; new_mcursorY = mCursorY; //按下了左键 if(0 != (mouse_dec.btn & 0x01)) { //如果last_mcursorX小于0,说明当前不是处于窗口移动模式,那么就需要检查现在鼠标按下左键之后有没有落在某个图层的标题栏上 if(last_mcursorX < 0) { for(j = layer_manage->top - 1; j > 0; j--) { layer = layer_manage->layers_order[j]; //计算一下鼠标的坐标在不在这个图层里面 x = mCursorX - layer->x; y = mCursorY - layer->y; if(0 <= x && x < layer->length && 0 <= y && y <= layer->width) { //鼠标所在区域也不能是涂层的透明区域 if(layer->buffer[y * layer->length + x] != layer->color_luc) { layer_switch(layer, layer_manage->top - 1); //如果当前的layer不是接收键盘输入的图层(即最前面的图层),那么就需要切换 if(layer != key_to_window) { //鼠标左击窗口所做的事情跟按下TAB键的事情差不多 keywindow_off(key_to_window); key_to_window = layer; keywindow_on(key_to_window); } //查看鼠标当前点击的区域是不是窗口的标题栏,并且不能是关闭按钮所在的区域 if(3 <= x && x < layer->length - 21 && 3 <= y && y <= 21) { last_mcursorX = mCursorX; last_mcursorY = mCursorY; tmp_layerX = layer->x; //这个tmp_layerX只是个要绘制的图层横坐标的过渡部分,真正的坐标还需要处理 new_layerY = layer->y; } //这个是鼠标点击到关闭按钮的情况 if(layer->length - 21 <= x && x <= layer->length - 5 && 5 <= y && y <= 19) { //查看该窗口是否为应用程序的窗口,如果是应用程序产生的窗口而不是console的那个黑窗口,那么这个窗口的flags有0x10 if(0 != (layer->flags & 0x10)) { //这部分处理跟按下ctrl+x的处理基本一样 task = layer->task; console_putstring_toend(task->console, "Break by mouse!\n"); io_cli(); task->tss.eax = (int)&(task->tss.esp0); task->tss.eip = (int)asm_end_app; io_sti(); } //不是应用程序的话就是console命令行窗口产生的layer else { //不能再这里的task_a中直接关闭,因为还有些console自身管理的定时器和fat等东西无法释放了 task = layer->task; io_cli(); fifo_put(&task->fifo, 4); io_sti(); } } break; } } } } //只要last_mcursorX大于0,说明当前处于窗口移动模式,说明当前鼠标正点击着某个图层的标题栏呢 else { x = mCursorX - last_mcursorX; y = mCursorY - last_mcursorY; new_layerX = (tmp_layerX + x + 2) & ~3; new_layerY = new_layerY + y; last_mcursorY = mCursorY; //更新到移动后的坐标 } } //如果当前没有按下左键,就把last_mcursorX设置为负数,作为当前不处于窗口移动的依据 else { last_mcursorX = -1; if(0x7fffffff != new_layerX) { layer_slide(layer, new_layerX, new_layerY); //一旦鼠标不点击这窗口移动了,就立即最后一次绘制窗口并且停止窗口的移动 new_layerX = 0x7fffffff; } } } } //收到这个之间的数据,说明是让关闭console的 else if(768 <= i && i <= 1023) { close_console(layer_manage->layers + (i - 768)); } } } }
int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) { open_console(); main(0,0); return 0; }
int main(int argc, char **argv, char **envp) { int fd_stdin=-1, fd_stdout=-1; pid_t pid; int guess = 0; /* char *argv[] = { "/sbin/init", NULL }; char *envp[] = { "PATH=/bin:/sbin:/usr/bin:/usr/sbin", NULL }; */ char *rootdev = NULL; open_console(); /* create mountpoints /dev and /proc /rootfs */ mkmountpoint("/dev"); mkmountpoint("/proc"); mkmountpoint("/rootfs"); open_console(); /* make sure /dev is mounted */ if(mount("devtmpfs", "/dev", "devtmpfs", 0, "")) { if(fstdout) { if(fstdout) { fprintf(fstdout, "INIT: could not mount devtmpfs on /dev: %s\n", strerror(errno)); fprintf(fstdout, "INIT: waiting 10 seconds ..\n"); sleep(5); sleep(5); } } } open_console(); /* mount /proc */ if(mount("proc", "/proc", "proc", 0, "")) { if(fstdout) { fprintf(fstdout, "INIT: could not mount proc on /proc: %s\n", strerror(errno)); fprintf(fstdout, "INIT: waiting 10 seconds ..\n"); sleep(5); sleep(5); } } rootdev = fsprobe_get_devname_by_spec(ROOTFSLABEL); if(!rootdev) { if(fstdout) { fprintf(fstdout, "INIT: could not find rootdevice\n"); fprintf(fstdout, "INIT: waiting 10 seconds ..\n"); sleep(5); sleep(5); fprintf(fstdout, "INIT: guessing that rootdev is /dev/sda1\n\n"); } rootdev="/dev/sda1"; guess = 1; } else { if(fstdout) fprintf(fstdout, "INIT: probed rootdev is %s\n", rootdev); } sleep(1); /* e2fsck -y rootdev */ /* fork + exec("/e2fsck", "/e2fsck"-y", rootdev) */ if(!guess) { if((pid=fork())==0) { execl("/e2fsck", "/e2fsck", "-y", rootdev, NULL); exit(0); } if(pid != -1) wait(NULL); } /* unlink /e2sck to save some memory */ if(unlink("/e2fsck")) { fprintf(fstdout, "INIT: unlink(\"/e2fsck\") failed: %s\n", strerror(errno)); } /* mount /rootfs, try ext4, ext3, ext2 */ if(mount(rootdev, "/rootfs", "ext4", MS_NOATIME, "")) { if(mount(rootdev, "/rootfs", "ext3", MS_NOATIME, "")) { if(mount(rootdev, "/rootfs", "ext2", MS_NOATIME, "")) { if(fstdout) fprintf(fstdout, "INIT: failed to mount %s: %s\n", rootdev, strerror(errno)); goto forever; } } } if(fstdout) fprintf(fstdout, "INIT: %s mounted.\n", rootdev); sleep(1); if (mount("/dev", "/rootfs/dev", NULL, MS_MOVE, NULL) < 0) { if(fstdout) fprintf(fstdout, "INIT: failed to mount moving /dev to /rootfs/dev"); sleep(1); } if (mount("/proc", "/rootfs/proc", NULL, MS_MOVE, NULL) < 0) { if(fstdout) fprintf(fstdout, "INIT: failed to mount moving /proc to /rootfs/proc"); sleep(1); } if(chdir("/rootfs")) { if(fstdout) fprintf(fstdout, "INIT: chdir(\"/rootfs\") failed: %s\n", strerror(errno)); goto forever; } if (mount("/rootfs", "/", NULL, MS_MOVE, NULL) < 0) { if(fstdout) fprintf(fstdout, "INIT: failed to mount moving /rootfs to /"); goto forever; } /* Now, the init process may still access the old root via its executable, shared libraries, standard input/output/error, and its current root directory. All these references are dropped by the following command: # exec chroot . /sbin/init <dev/console >dev/console 2>&1 */ if(chroot(".")) { if(fstdout) { fprintf(fstdout, "INIT: chroot(\".\") failed: %s\n", strerror(errno)); sleep(5); sleep(5); } } fd_stdin = open("/dev/console", O_RDONLY|O_NOCTTY); if(fd_stdin == -1) { if(fstdout) fprintf(fstdout, "INIT: open(\"/dev/console\", O_RDONLY) failed: %s\n", strerror(errno)); } else { if(fd_stdin != 0) { dup2(fd_stdin, 0); close(fd_stdin); } } fd_stdout = open("/dev/console", O_WRONLY|O_NOCTTY); if(fd_stdout == -1) { if(fstdout) fprintf(fstdout, "INIT: open(\"/dev/console\", O_WRONLY) failed: %s\n", strerror(errno)); } else { if(fd_stdout != 1) { dup2(fd_stdout, 1); dup2(fd_stdout, 2); close(fd_stdout); } } if(fstdout) fprintf(fstdout, "INIT: now execing \"/sbin/init\"\n"); sleep(1); if(fstdout) { fclose(fstdout); fstdout = NULL; } argv[0] = "/sbin/init"; execve("/sbin/init", argv, envp); open_console(); if(fstdout) { fprintf(fstdout, "INIT: execve(\"/sbin/init\") failed: %s\n", strerror(errno)); } forever: if(fstdout) { fprintf(fstdout, "INIT: pid is %u", getpid()); fprintf(fstdout, "INIT: boot cannot proceed from here.\n"); fprintf(fstdout, "INIT: turn off the computer.\n"); } while(1) sleep(1000); return 2; }
void HariMain(void) { struct BOOTINFO *binfo = (struct BOOTINFO *) ADR_BOOTINFO; struct SHTCTL *shtctl; char s[40]; struct FIFO32 fifo, keycmd; int fifobuf[128], keycmd_buf[32]; int mx, my, i, new_mx = -1, new_my = 0, new_wx = 0x7fffffff, new_wy = 0; unsigned int memtotal; struct MOUSE_DEC mdec; struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR; unsigned char *buf_back, buf_mouse[256]; struct SHEET *sht_back, *sht_mouse; struct TASK *task_a, *task; static char keytable0[0x80] = { 0, 0, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '^', 0x08, 0, 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '@', '[', 0x0a, 0, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ';', ':', 0, 0, ']', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', ',', '.', '/', 0, '*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x5c, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x5c, 0, 0 }; static char keytable1[0x80] = { 0, 0, '!', 0x22, '#', '$', '%', '&', 0x27, '(', ')', '~', '=', '~', 0x08, 0, 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '`', '{', 0x0a, 0, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', '+', '*', 0, 0, '}', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 0, '*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '_', 0, 0, 0, 0, 0, 0, 0, 0, 0, '|', 0, 0 }; int key_shift = 0, key_leds = (binfo->leds >> 4) & 7, keycmd_wait = -1; int j, x, y, mmx = -1, mmy = -1, mmx2 = 0; struct SHEET *sht = 0, *key_win; init_gdtidt(); init_pic(); io_sti(); /* IDT/PICの初期化が終わったのでCPUの割り込み禁止を解除 */ fifo32_init(&fifo, 128, fifobuf, 0); *((int *) 0x0fec) = (int) &fifo; init_pit(); init_keyboard(&fifo, 256); enable_mouse(&fifo, 512, &mdec); io_out8(PIC0_IMR, 0xf8); /* PITとPIC1とキーボードを許可(11111000) */ io_out8(PIC1_IMR, 0xef); /* マウスを許可(11101111) */ fifo32_init(&keycmd, 32, keycmd_buf, 0); memtotal = memtest(0x00400000, 0xbfffffff); memman_init(memman); memman_free(memman, 0x00001000, 0x0009e000); /* 0x00001000 - 0x0009efff */ memman_free(memman, 0x00400000, memtotal - 0x00400000); init_palette(); shtctl = shtctl_init(memman, binfo->vram, binfo->scrnx, binfo->scrny); task_a = task_init(memman); fifo.task = task_a; task_run(task_a, 1, 2); *((int *) 0x0fe4) = (int) shtctl; /* sht_back */ sht_back = sheet_alloc(shtctl); buf_back = (unsigned char *) memman_alloc_4k(memman, binfo->scrnx * binfo->scrny); sheet_setbuf(sht_back, buf_back, binfo->scrnx, binfo->scrny, -1); /* 透明色なし */ init_screen8(buf_back, binfo->scrnx, binfo->scrny); /* sht_cons */ key_win = open_console(shtctl, memtotal); /* sht_mouse */ sht_mouse = sheet_alloc(shtctl); sheet_setbuf(sht_mouse, buf_mouse, 16, 16, 99); init_mouse_cursor8(buf_mouse, 99); mx = (binfo->scrnx - 16) / 2; /* 画面中央になるように座標計算 */ my = (binfo->scrny - 28 - 16) / 2; sheet_slide(sht_back, 0, 0); sheet_slide(key_win, 32, 4); sheet_slide(sht_mouse, mx, my); sheet_updown(sht_back, 0); sheet_updown(key_win, 1); sheet_updown(sht_mouse, 2); keywin_on(key_win); /* 最初にキーボード状態との食い違いがないように、設定しておくことにする */ fifo32_put(&keycmd, KEYCMD_LED); fifo32_put(&keycmd, key_leds); for (;;) { if (fifo32_status(&keycmd) > 0 && keycmd_wait < 0) { /* キーボードコントローラに送るデータがあれば、送る */ keycmd_wait = fifo32_get(&keycmd); wait_KBC_sendready(); io_out8(PORT_KEYDAT, keycmd_wait); } io_cli(); if (fifo32_status(&fifo) == 0) { /* FIFOがからっぽになったので、保留している描画があれば実行する */ if (new_mx >= 0) { io_sti(); sheet_slide(sht_mouse, new_mx, new_my); new_mx = -1; } else if (new_wx != 0x7fffffff) { io_sti(); sheet_slide(sht, new_wx, new_wy); new_wx = 0x7fffffff; } else { task_sleep(task_a); io_sti(); } } else { i = fifo32_get(&fifo); io_sti(); if (key_win != 0 && key_win->flags == 0) { /* ウィンドウが閉じられた */ if (shtctl->top == 1) { /* もうマウスと背景しかない */ key_win = 0; } else { key_win = shtctl->sheets[shtctl->top - 1]; keywin_on(key_win); } } if (256 <= i && i <= 511) { /* キーボードデータ */ if (i < 0x80 + 256) { /* キーコードを文字コードに変換 */ if (key_shift == 0) { s[0] = keytable0[i - 256]; } else { s[0] = keytable1[i - 256]; } } else { s[0] = 0; } if ('A' <= s[0] && s[0] <= 'Z') { /* 入力文字がアルファベット */ if (((key_leds & 4) == 0 && key_shift == 0) || ((key_leds & 4) != 0 && key_shift != 0)) { s[0] += 0x20; /* 大文字を小文字に変換 */ } } if (s[0] != 0 && key_win != 0) { /* 通常文字、バックスペース、Enter */ fifo32_put(&key_win->task->fifo, s[0] + 256); } if (i == 256 + 0x0f && key_win != 0) { /* Tab */ keywin_off(key_win); j = key_win->height - 1; if (j == 0) { j = shtctl->top - 1; } key_win = shtctl->sheets[j]; keywin_on(key_win); } if (i == 256 + 0x2a) { /* 左シフト ON */ key_shift |= 1; } if (i == 256 + 0x36) { /* 右シフト ON */ key_shift |= 2; } if (i == 256 + 0xaa) { /* 左シフト OFF */ key_shift &= ~1; } if (i == 256 + 0xb6) { /* 右シフト OFF */ key_shift &= ~2; } if (i == 256 + 0x3a) { /* CapsLock */ key_leds ^= 4; fifo32_put(&keycmd, KEYCMD_LED); fifo32_put(&keycmd, key_leds); } if (i == 256 + 0x45) { /* NumLock */ key_leds ^= 2; fifo32_put(&keycmd, KEYCMD_LED); fifo32_put(&keycmd, key_leds); } if (i == 256 + 0x46) { /* ScrollLock */ key_leds ^= 1; fifo32_put(&keycmd, KEYCMD_LED); fifo32_put(&keycmd, key_leds); } if (i == 256 + 0x3b && key_shift != 0 && key_win != 0) { /* Shift+F1 */ task = key_win->task; if (task != 0 && task->tss.ss0 != 0) { cons_putstr0(task->cons, "\nBreak(key) :\n"); io_cli(); /* 強制終了処理中にタスクが変わると困るから */ task->tss.eax = (int) &(task->tss.esp0); task->tss.eip = (int) asm_end_app; io_sti(); } } if (i == 256 + 0x3c && key_shift != 0) { /* Shift+F2 */ /* 新しく作ったコンソールを入力選択状態にする(そのほうが親切だよね?) */ if (key_win != 0) { keywin_off(key_win); } key_win = open_console(shtctl, memtotal); sheet_slide(key_win, 32, 4); sheet_updown(key_win, shtctl->top); keywin_on(key_win); } if (i == 256 + 0x57) { /* F11 */ sheet_updown(shtctl->sheets[1], shtctl->top - 1); } if (i == 256 + 0xfa) { /* キーボードがデータを無事に受け取った */ keycmd_wait = -1; } if (i == 256 + 0xfe) { /* キーボードがデータを無事に受け取れなかった */ wait_KBC_sendready(); io_out8(PORT_KEYDAT, keycmd_wait); } } else if (512 <= i && i <= 767) { /* マウスデータ */ if (mouse_decode(&mdec, i - 512) != 0) { /* マウスカーソルの移動 */ mx += mdec.x; my += mdec.y; if (mx < 0) { mx = 0; } if (my < 0) { my = 0; } if (mx > binfo->scrnx - 1) { mx = binfo->scrnx - 1; } if (my > binfo->scrny - 1) { my = binfo->scrny - 1; } new_mx = mx; new_my = my; if ((mdec.btn & 0x01) != 0) { /* 左ボタンを押している */ if (mmx < 0) { /* 通常モードの場合 */ /* 上の下じきから順番にマウスが指している下じきを探す */ for (j = shtctl->top - 1; j > 0; j--) { sht = shtctl->sheets[j]; x = mx - sht->vx0; y = my - sht->vy0; if (0 <= x && x < sht->bxsize && 0 <= y && y < sht->bysize) { if (sht->buf[y * sht->bxsize + x] != sht->col_inv) { sheet_updown(sht, shtctl->top - 1); if (sht != key_win) { keywin_off(key_win); key_win = sht; keywin_on(key_win); } if (3 <= x && x < sht->bxsize - 3 && 3 <= y && y < 21) { mmx = mx; /* ウィンドウ移動モードへ */ mmy = my; mmx2 = sht->vx0; new_wy = sht->vy0; } if (sht->bxsize - 21 <= x && x < sht->bxsize - 5 && 5 <= y && y < 19) { /* 「×」ボタンクリック */ if ((sht->flags & 0x10) != 0) { /* アプリが作ったウィンドウか? */ task = sht->task; cons_putstr0(task->cons, "\nBreak(mouse) :\n"); io_cli(); /* 強制終了処理中にタスクが変わると困るから */ task->tss.eax = (int) &(task->tss.esp0); task->tss.eip = (int) asm_end_app; io_sti(); } } break; } } } } else { /* ウィンドウ移動モードの場合 */ x = mx - mmx; /* マウスの移動量を計算 */ y = my - mmy; new_wx = (mmx2 + x + 2) & ~3; new_wy = new_wy + y; mmy = my; /* 移動後の座標に更新 */ } } else { /* 左ボタンを押していない */ mmx = -1; /* 通常モードへ */ if (new_wx != 0x7fffffff) { sheet_slide(sht, new_wx, new_wy); /* 一度確定させる */ new_wx = 0x7fffffff; } } } } else if (768 <= i && i <= 1023) { /* コンソール終了処理 */ close_console(shtctl->sheets0 + (i - 768)); } } } }
void service_start(struct service *svc, const char *dynamic_args) { struct stat s; pid_t pid; int needs_console; int n; #ifdef HAVE_SELINUX char *scon = NULL; int rc; #endif /* starting a service removes it from the disabled or reset * state and immediately takes it out of the restarting * state if it was in there */ svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET)); svc->time_started = 0; /* running processes require no additional work -- if * they're in the process of exiting, we've ensured * that they will immediately restart on exit, unless * they are ONESHOT */ if (svc->flags & SVC_RUNNING) { return; } needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0; if (needs_console && (!have_console)) { ERROR("service '%s' requires console\n", svc->name); svc->flags |= SVC_DISABLED; return; } if (stat(svc->args[0], &s) != 0) { ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name); svc->flags |= SVC_DISABLED; return; } if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) { ERROR("service '%s' must be one-shot to use dynamic args, disabling\n", svc->args[0]); svc->flags |= SVC_DISABLED; return; } #ifdef HAVE_SELINUX if (is_selinux_enabled() > 0) { char *mycon = NULL, *fcon = NULL; INFO("computing context for service '%s'\n", svc->args[0]); rc = getcon(&mycon); if (rc < 0) { ERROR("could not get context while starting '%s'\n", svc->name); return; } rc = getfilecon(svc->args[0], &fcon); if (rc < 0) { ERROR("could not get context while starting '%s'\n", svc->name); freecon(mycon); return; } rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon); freecon(mycon); freecon(fcon); if (rc < 0) { ERROR("could not get context while starting '%s'\n", svc->name); return; } } #endif NOTICE("starting '%s'\n", svc->name); pid = fork(); if (pid == 0) { struct socketinfo *si; struct svcenvinfo *ei; char tmp[32]; int fd, sz; umask(077); #ifdef __arm__ /* * b/7188322 - Temporarily revert to the compat memory layout * to avoid breaking third party apps. * * THIS WILL GO AWAY IN A FUTURE ANDROID RELEASE. * * http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commitdiff;h=7dbaa466 * changes the kernel mapping from bottom up to top-down. * This breaks some programs which improperly embed * an out of date copy of Android's linker. */ int current = personality(0xffffFFFF); personality(current | ADDR_COMPAT_LAYOUT); #endif if (properties_inited()) { get_property_workspace(&fd, &sz); sprintf(tmp, "%d,%d", dup(fd), sz); add_environment("ANDROID_PROPERTY_WORKSPACE", tmp); } for (ei = svc->envvars; ei; ei = ei->next) add_environment(ei->name, ei->value); #ifdef HAVE_SELINUX setsockcreatecon(scon); #endif for (si = svc->sockets; si; si = si->next) { int socket_type = ( !strcmp(si->type, "stream") ? SOCK_STREAM : (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET)); int s = create_socket(si->name, socket_type, si->perm, si->uid, si->gid); if (s >= 0) { publish_socket(si->name, s); } } #ifdef HAVE_SELINUX freecon(scon); scon = NULL; setsockcreatecon(NULL); #endif if (svc->ioprio_class != IoSchedClass_NONE) { if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) { ERROR("Failed to set pid %d ioprio = %d,%d: %s\n", getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno)); } } if (needs_console) { setsid(); open_console(); } else { zap_stdio(); } #if 0 for (n = 0; svc->args[n]; n++) { INFO("args[%d] = '%s'\n", n, svc->args[n]); } for (n = 0; ENV[n]; n++) { INFO("env[%d] = '%s'\n", n, ENV[n]); } #endif setpgid(0, getpid()); /* as requested, set our gid, supplemental gids, and uid */ if (svc->gid) { if (setgid(svc->gid) != 0) { ERROR("setgid failed: %s\n", strerror(errno)); _exit(127); } } if (svc->nr_supp_gids) { if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) { ERROR("setgroups failed: %s\n", strerror(errno)); _exit(127); } } if (svc->uid) { if (setuid(svc->uid) != 0) { ERROR("setuid failed: %s\n", strerror(errno)); _exit(127); } } #ifdef HAVE_SELINUX if (svc->seclabel) { if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) { ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno)); _exit(127); } } #endif if (!dynamic_args) { if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) { ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno)); } } else { char *arg_ptrs[INIT_PARSER_MAXARGS+1]; int arg_idx = svc->nargs; char *tmp = strdup(dynamic_args); char *next = tmp; char *bword; /* Copy the static arguments */ memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *))); while((bword = strsep(&next, " "))) { arg_ptrs[arg_idx++] = bword; if (arg_idx == INIT_PARSER_MAXARGS) break; } arg_ptrs[arg_idx] = '\0'; execve(svc->args[0], (char**) arg_ptrs, (char**) ENV); } _exit(127); } #ifdef HAVE_SELINUX freecon(scon); #endif if (pid < 0) { ERROR("failed to start '%s'\n", svc->name); svc->pid = 0; return; } svc->time_started = gettime(); svc->pid = pid; svc->flags |= SVC_RUNNING; if (properties_inited()) notify_service_state(svc->name, "running"); }
int main(int argc, char **argv, char **envp) { int fd_stdin=-1, fd_stdout=-1; pid_t pid; int retries = 0; int rootmounted = 0; char *rootdev = NULL; char *rootfstype = "unknown"; char *rootfslabel = ROOTFSLABEL; char *initprg = "/sbin/init"; conf.mountflags = MS_SYNCHRONOUS; open_console(); /* create mountpoints /dev and /proc /rootfs */ mkmountpoint("/dev"); mkmountpoint("/proc"); mkmountpoint("/rootfs"); open_console(); /* make sure /dev is mounted */ if(mount("devtmpfs", "/dev", "devtmpfs", 0, "")) { if(fstdout) { if(fstdout) { fprintf(fstdout, "INIT: could not mount devtmpfs on /dev: %s\n", strerror(errno)); fprintf(fstdout, "INIT: waiting 10 seconds ..\n"); sleep(5); sleep(5); } } } open_console(); /* mount /proc */ if(mount("proc", "/proc", "proc", 0, "")) { if(fstdout) { fprintf(fstdout, "INIT: could not mount proc on /proc: %s\n", strerror(errno)); fprintf(fstdout, "INIT: waiting 10 seconds ..\n"); sleep(5); sleep(5); } } /* parse kernel commandline options */ cmdline.autoresize = 1; cmdline_parse(); if(cmdline.init) { initprg = cmdline.init; } if(cmdline.async) { conf.mountflags ^= MS_SYNCHRONOUS; } if(cmdline.usbreset) { usbreset(); sleep(1); } if(cmdline.rootdelay > 0) { if(fstdout) fprintf(fstdout, "INIT: rootdelay, waiting %d seconds\n", cmdline.rootdelay); sleep(cmdline.rootdelay); } if(cmdline.rootdev) { struct stat statb; if(fstdout) fprintf(fstdout, "INIT: rootdev %s given on commandline\n", cmdline.rootdev); rootdev = cmdline.rootdev; if(stat(rootdev, &statb)) { if(fstdout) fprintf(fstdout, "INIT: rootdev %s not found!\n", rootdev); rootdev = NULL; } } if(!rootdev) { if(cmdline.rootfslabel) { if(strncmp(cmdline.rootfslabel, "LABEL=", 6)) { rootfslabel = malloc(strlen("LABEL=") + strlen(cmdline.rootfslabel) + 1); strcpy(rootfslabel, "LABEL="); strcat(rootfslabel, cmdline.rootfslabel); } else { rootfslabel = cmdline.rootfslabel; } } if(fstdout) fprintf(fstdout, "INIT: probing for rootdev labeled '%s'\n", rootfslabel); fsprobe_init(); while( (rootdev = fsprobe_get_devname_by_spec(rootfslabel)) == NULL ) { retries++; if(retries == 15) usbreset(); if(retries > 25) break; if(fstdout) fprintf(fstdout, "INIT: waiting for rootdevice to be available\n"); sleep(2); fsprobe_init(); } } if(!rootdev) { if(fstdout) { fprintf(fstdout, "INIT: could not find rootdevice\n"); listdevices(); fprintf(fstdout, "INIT: waiting 10 seconds ..\n"); sleep(5); sleep(5); fprintf(fstdout, "INIT: guessing that rootdev is /dev/sda1\n\n"); } rootdev="/dev/sda1"; } else { if(fstdout) fprintf(fstdout, "INIT: using rootdev %s\n", rootdev); } sleep(1); /* if filesystem is of the ext family */ if((!cmdline.rootfstype) || (!strncmp(cmdline.rootfstype, "ext", 3))) { int status = 0; int fsckok = 0; /* e2fsck -y rootdev */ /* fork + exec("/e2fsck", "/e2fsck"-y", rootdev) */ if((pid=fork())==0) { execl("/e2fsck", "/e2fsck", "-y", rootdev, NULL); exit(0); } if(pid != -1) { wait(&status); if(WIFEXITED(status)) { fprintf(fstdout, "INIT: e2fsck exited normally with exit code: %d\n", WEXITSTATUS(status)); if(WEXITSTATUS(status)==0) fsckok=1; } else { fprintf(fstdout, "INIT: e2fsck exited abnormally\n"); } } if(fsckok) { if(cmdline.autoresize) { sync(); if((pid=fork())==0) { execl("/resize2fs", "/resize2fs", "-f", rootdev, NULL); exit(0); } if(pid != -1) wait(NULL); sync(); } } } /* unlink /e2sck and /resize2fs to save some memory */ if(unlink("/e2fsck")) { fprintf(fstdout, "INIT: unlink(\"/e2fsck\") failed: %s\n", strerror(errno)); } if(unlink("/resize2fs")) { fprintf(fstdout, "INIT: unlink(\"/resize2fs\") failed: %s\n", strerror(errno)); } if(cmdline.rootfstype) { /* mount /rootfs, try fstype supplied on kernel commandline */ if(mount(rootdev, "/rootfs", cmdline.rootfstype, MS_NOATIME|MS_RDONLY|conf.mountflags, "")) { if(fstdout) fprintf(fstdout, "INIT: failed to mount (%s) %s: %s\n", cmdline.rootfstype, rootdev, strerror(errno)); } else { rootmounted = 1; rootfstype = cmdline.rootfstype; } } if(!rootmounted) { /* mount /rootfs, try ext2 */ if(mount(rootdev, "/rootfs", "ext2", MS_NOATIME|MS_RDONLY|conf.mountflags, "")) { if(fstdout) fprintf(fstdout, "INIT: failed to mount (ext2) %s: %s\n", rootdev, strerror(errno)); goto forever; } rootfstype = "ext2"; } if(fstdout) fprintf(fstdout, "INIT: (%s) %s mounted.\n", rootfstype, rootdev); sleep(1); if(cmdline.install) { if(fstdout) { fprintf(fstdout, "INIT: INSTALL INVOKED!\nINSTALL: PROCEEDING IN 5 SECONDS!\n"); sleep(5); } /* unpack install archive in initramfs root */ { int fd; fd = open("/rootfs/install.tar", O_RDONLY); if(fd == -1) { if(fstdout) fprintf(fstdout, "INSTALL: failed to open '/rootfs/install.tar'\n"); } else { if(fstdout) fprintf(fstdout, "INSTALL: '/rootfs/install.tar' opened ok\n"); tar_extract(fd); close(fd); } } /* Be nice and prepare stdin and stdout for the install program */ fd_stdin = open("/dev/console", O_RDONLY|O_NOCTTY); if(fd_stdin == -1) { if(fstdout) fprintf(fstdout, "INSTALL: open(\"/dev/console\", O_RDONLY) failed: %s\n", strerror(errno)); } else { if(fd_stdin != 0) { dup2(fd_stdin, 0); close(fd_stdin); } } fd_stdout = open("/dev/console", O_WRONLY|O_NOCTTY); if(fd_stdout == -1) { if(fstdout) fprintf(fstdout, "INSTALL: open(\"/dev/console\", O_WRONLY) failed: %s\n", strerror(errno)); } else { if(fd_stdout != 1) { dup2(fd_stdout, 1); dup2(fd_stdout, 2); close(fd_stdout); } } /* exec "/install" */ { char *iv[2]; iv[0] = "/install"; iv[1] = NULL; if(fstdout) fprintf(fstdout, "INSTALL: exec(\"/install\")\n"); execve(iv[0], iv, envp); if(fstdout) fprintf(fstdout, "INSTALL: exec(\"/install\") failed\n"); sleep(10); } } if (mount("/dev", "/rootfs/dev", NULL, MS_MOVE, NULL) < 0) { if(fstdout) fprintf(fstdout, "INIT: failed to mount moving /dev to /rootfs/dev\n"); sleep(1); } if (mount("/proc", "/rootfs/proc", NULL, MS_MOVE, NULL) < 0) { if(fstdout) fprintf(fstdout, "INIT: failed to mount moving /proc to /rootfs/proc\n"); sleep(1); } /* * mkdir /sbin * copy /rootfs/sbin/init to /sbin/init * mount --bind /rootfs/sbin/init /sbin/init */ if(mkdir("/sbin", 0755)) { if(fstdout) fprintf(fstdout, "INIT: mkdir(\"/sbin\") failed: %s\n", strerror(errno)); sleep(1); } else { int fd, ofd; ssize_t siz; struct stat statb; if(stat("/rootfs/sbin/init", &statb)) { if(fstdout) fprintf(fstdout, "INIT: stat(\"/rootfs/sbin/init\") failed: %s\n", strerror(errno)); sleep(1); goto noinitcopy; } siz = statb.st_size; if(mount("tmpfs", "/sbin","tmpfs", 0,"")) { if(fstdout) fprintf(fstdout, "INIT: mount(\"tmpfs\", \"/sbin\") failed: %s\n", strerror(errno)); } if( (fd = open("/rootfs/sbin/init", O_RDONLY)) == -1) { if(fstdout) fprintf(fstdout, "INIT: open(\"/rootfs/sbin/init\", O_RDONLY) failed: %s\n", strerror(errno)); sleep(1); goto noinitcopy; } if( (ofd = open("/sbin/init", O_WRONLY|O_CREAT|O_TRUNC, 0755)) == -1) { if(fstdout) fprintf(fstdout, "INIT: open(\"/sbin/init\", O_WRONLY|O_CREAT|O_TRUNC) failed: %s\n", strerror(errno)); sleep(1); close(fd); goto noinitcopy; } if(data(fd, siz, ofd)) { if(fstdout) fprintf(fstdout, "INIT: datacopy of /sbin/init failed: %s\n", strerror(errno)); sleep(1); close(fd); close(ofd); goto noinitcopy; } close(fd); if(close(ofd)) { if(fstdout) fprintf(fstdout, "INIT: close(\"/sbin/init\") failed: %s\n", strerror(errno)); sleep(1); goto noinitcopy; } if (mount("rootfs", "/", NULL, MS_REMOUNT|MS_NOATIME|MS_RDONLY, NULL) < 0) { if(fstdout) fprintf(fstdout, "INIT: failed to remount initramfs as read-only\n"); sleep(1); } if (mount("tmpfs", "/sbin", NULL, MS_REMOUNT|MS_NOATIME|MS_RDONLY, NULL) < 0) { if(fstdout) fprintf(fstdout, "INIT: failed to remount \"/sbin\" as read-only\n"); sleep(1); } if(mount("/sbin/init", "/rootfs/sbin/init", "tmpfs", MS_BIND|MS_RDONLY, NULL)) { if(mount("/sbin/init", "/rootfs/sbin/init", "tmpfs", MS_BIND, NULL)) { if(fstdout) fprintf(fstdout, "INIT: bind mount(\"/sbin/init\", \"/rootfs/sbin/init\") failed: %s\n", strerror(errno)); sleep(1); } } } noinitcopy: if(chdir("/rootfs")) { if(fstdout) fprintf(fstdout, "INIT: chdir(\"/rootfs\") failed: %s\n", strerror(errno)); goto forever; } if (mount("/rootfs", "/", NULL, MS_MOVE, NULL) < 0) { if(fstdout) fprintf(fstdout, "INIT: failed to mount moving /rootfs to /\n"); goto forever; } /* Now, the init process may still access the old root via its executable, shared libraries, standard input/output/error, and its current root directory. All these references are dropped by the following command: # exec chroot . /sbin/init <dev/console >dev/console 2>&1 */ if(chroot(".")) { if(fstdout) { fprintf(fstdout, "INIT: chroot(\".\") failed: %s\n", strerror(errno)); sleep(5); sleep(5); } } /* check if we need to copy default versions of some config files */ { int remounted=0; chk_cfg_file(&remounted, "ssh/ssh_config"); chk_cfg_file(&remounted, "ssh/sshd_config"); chk_cfg_file(&remounted, "inetd.conf"); chk_cfg_file(&remounted, "inittab"); chk_cfg_file(&remounted, "login.defs"); chk_cfg_file(&remounted, "limits"); chk_cfg_file(&remounted, "login.access"); if(remounted) if (mount("rootfs", "/", NULL, MS_REMOUNT|MS_NOATIME|MS_RDONLY|conf.mountflags, NULL) < 0) { if(fstdout) fprintf(fstdout, "INIT: failed to remount rootfs as read-only\n"); sleep(1); } } fd_stdin = open("/dev/console", O_RDONLY|O_NOCTTY); if(fd_stdin == -1) { if(fstdout) fprintf(fstdout, "INIT: open(\"/dev/console\", O_RDONLY) failed: %s\n", strerror(errno)); } else { if(fd_stdin != 0) { dup2(fd_stdin, 0); close(fd_stdin); } } fd_stdout = open("/dev/console", O_WRONLY|O_NOCTTY); if(fd_stdout == -1) { if(fstdout) fprintf(fstdout, "INIT: open(\"/dev/console\", O_WRONLY) failed: %s\n", strerror(errno)); } else { if(fd_stdout != 1) { dup2(fd_stdout, 1); dup2(fd_stdout, 2); close(fd_stdout); } } if(fstdout) fprintf(fstdout, "INIT: now execing \"%s\"\n", initprg); sleep(1); if(fstdout) { fclose(fstdout); fstdout = NULL; } argv[0] = initprg; execve(initprg, argv, envp); open_console(); if(fstdout) { fprintf(fstdout, "INIT: execve(\"%s\") failed: %s\n", initprg, strerror(errno)); } forever: if(fstdout) { fprintf(fstdout, "INIT: pid is %u\n", getpid()); fprintf(fstdout, "INIT: boot cannot proceed from here.\n"); fprintf(fstdout, "INIT: turn off the computer.\n"); } while(1) sleep(1000); return 2; }
void service_start(struct service *svc, const char *dynamic_args) { struct stat s; pid_t pid; int needs_console; int n; /* starting a service removes it from the disabled or reset * state and immediately takes it out of the restarting * state if it was in there */ svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART)); svc->time_started = 0; /* running processes require no additional work -- if * they're in the process of exiting, we've ensured * that they will immediately restart on exit, unless * they are ONESHOT */ if (svc->flags & SVC_RUNNING) { return; } needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0; if (needs_console && (!have_console)) { ERROR("service '%s' requires console\n", svc->name); svc->flags |= SVC_DISABLED; return; } if (stat(svc->args[0], &s) != 0) { ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name); svc->flags |= SVC_DISABLED; return; } if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) { ERROR("service '%s' must be one-shot to use dynamic args, disabling\n", svc->args[0]); svc->flags |= SVC_DISABLED; return; } NOTICE("starting '%s'\n", svc->name); pid = fork(); if (pid == 0) { struct socketinfo *si; struct svcenvinfo *ei; char tmp[32]; int fd, sz; if (properties_inited()) { get_property_workspace(&fd, &sz); sprintf(tmp, "%d,%d", dup(fd), sz); add_environment("ANDROID_PROPERTY_WORKSPACE", tmp); } for (ei = svc->envvars; ei; ei = ei->next) add_environment(ei->name, ei->value); for (si = svc->sockets; si; si = si->next) { int socket_type = ( !strcmp(si->type, "stream") ? SOCK_STREAM : (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET)); int s = create_socket(si->name, socket_type, si->perm, si->uid, si->gid); if (s >= 0) { publish_socket(si->name, s); } } if (svc->ioprio_class != IoSchedClass_NONE) { if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) { ERROR("Failed to set pid %d ioprio = %d,%d: %s\n", getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno)); } } if (needs_console) { setsid(); open_console(); } else { zap_stdio(); } #if 0 for (n = 0; svc->args[n]; n++) { INFO("args[%d] = '%s'\n", n, svc->args[n]); } for (n = 0; ENV[n]; n++) { INFO("env[%d] = '%s'\n", n, ENV[n]); } #endif setpgid(0, getpid()); /* as requested, set our gid, supplemental gids, and uid */ if (svc->gid) { if (setgid(svc->gid) != 0) { ERROR("setgid failed: %s\n", strerror(errno)); _exit(127); } } if (svc->nr_supp_gids) { if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) { ERROR("setgroups failed: %s\n", strerror(errno)); _exit(127); } } if (svc->uid) { if (setuid(svc->uid) != 0) { ERROR("setuid failed: %s\n", strerror(errno)); _exit(127); } } if (!dynamic_args) { if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) { ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno)); } } else { char *arg_ptrs[INIT_PARSER_MAXARGS+1]; int arg_idx = svc->nargs; char *tmp = strdup(dynamic_args); char *next = tmp; char *bword; /* Copy the static arguments */ memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *))); while((bword = strsep(&next, " "))) { arg_ptrs[arg_idx++] = bword; if (arg_idx == INIT_PARSER_MAXARGS) break; } arg_ptrs[arg_idx] = '\0'; execve(svc->args[0], (char**) arg_ptrs, (char**) ENV); } _exit(127); } if (pid < 0) { ERROR("failed to start '%s'\n", svc->name); svc->pid = 0; return; } svc->time_started = gettime(); svc->pid = pid; svc->flags |= SVC_RUNNING; if (properties_inited()) notify_service_state(svc->name, "running"); }
/* * Bring the system up single user. */ static state_func_t single_user(void) { pid_t pid, wpid; int status; sigset_t mask; const char *shell; char *argv[2]; #ifdef SECURE struct ttyent *typ; struct passwd *pp; static const char banner[] = "Enter root password, or ^D to go multi-user\n"; char *clear, *password; #endif #ifdef DEBUGSHELL char altshell[128]; #endif if (Reboot) { /* Instead of going single user, let's reboot the machine */ sync(); reboot(howto); _exit(0); } shell = get_shell(); if ((pid = fork()) == 0) { /* * Start the single user session. */ open_console(); #ifdef SECURE /* * Check the root password. * We don't care if the console is 'on' by default; * it's the only tty that can be 'off' and 'secure'. */ typ = getttynam("console"); pp = getpwnam("root"); if (typ && (typ->ty_status & TTY_SECURE) == 0 && pp && *pp->pw_passwd) { write_stderr(banner); for (;;) { clear = getpass("Password:"******"single-user login failed\n"); } } endttyent(); endpwent(); #endif /* SECURE */ #ifdef DEBUGSHELL { char *cp = altshell; int num; #define SHREQUEST "Enter full pathname of shell or RETURN for " write_stderr(SHREQUEST); write_stderr(shell); write_stderr(": "); while ((num = read(STDIN_FILENO, cp, 1)) != -1 && num != 0 && *cp != '\n' && cp < &altshell[127]) cp++; *cp = '\0'; if (altshell[0] != '\0') shell = altshell; } #endif /* DEBUGSHELL */ /* * Unblock signals. * We catch all the interesting ones, * and those are reset to SIG_DFL on exec. */ sigemptyset(&mask); sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0); /* * Fire off a shell. * If the default one doesn't work, try the Bourne shell. */ char name[] = "-sh"; argv[0] = name; argv[1] = 0; execv(shell, argv); emergency("can't exec %s for single user: %m", shell); execv(_PATH_BSHELL, argv); emergency("can't exec %s for single user: %m", _PATH_BSHELL); sleep(STALL_TIMEOUT); _exit(1); } if (pid == -1) { /* * We are seriously hosed. Do our best. */ emergency("can't fork single-user shell, trying again"); while (waitpid(-1, (int *) 0, WNOHANG) > 0) continue; return (state_func_t) single_user; } requested_transition = 0; do { if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1) collect_child(wpid); if (wpid == -1) { if (errno == EINTR) continue; warning("wait for single-user shell failed: %m; restarting"); return (state_func_t) single_user; } if (wpid == pid && WIFSTOPPED(status)) { warning("init: shell stopped, restarting\n"); kill(pid, SIGCONT); wpid = -1; } } while (wpid != pid && !requested_transition); if (requested_transition) return (state_func_t) requested_transition; if (!WIFEXITED(status)) { if (WTERMSIG(status) == SIGKILL) { /* * reboot(8) killed shell? */ warning("single user shell terminated."); sleep(STALL_TIMEOUT); _exit(0); } else { warning("single user shell terminated, restarting"); return (state_func_t) single_user; } } runcom_mode = FASTBOOT; return (state_func_t) runcom; }
void service_start(struct service *svc, const char *dynamic_args) { // Starting a service removes it from the disabled or reset state and // immediately takes it out of the restarting state if it was in there. svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START)); svc->time_started = 0; // Running processes require no additional work --- if they're in the // process of exiting, we've ensured that they will immediately restart // on exit, unless they are ONESHOT. if (svc->flags & SVC_RUNNING) { return; } bool needs_console = (svc->flags & SVC_CONSOLE); if (needs_console && !have_console) { ERROR("service '%s' requires console\n", svc->name); svc->flags |= SVC_DISABLED; return; } struct stat sb; if (stat(svc->args[0], &sb) == -1) { ERROR("cannot find '%s' (%s), disabling '%s'\n", svc->args[0], strerror(errno), svc->name); svc->flags |= SVC_DISABLED; return; } if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) { ERROR("service '%s' must be one-shot to use dynamic args, disabling\n", svc->args[0]); svc->flags |= SVC_DISABLED; return; } char* scon = NULL; if (svc->seclabel) { scon = strdup(svc->seclabel); if (!scon) { ERROR("Out of memory while starting '%s'\n", svc->name); return; } } else { char *mycon = NULL, *fcon = NULL; INFO("computing context for service '%s'\n", svc->args[0]); int rc = getcon(&mycon); if (rc < 0) { ERROR("could not get context while starting '%s'\n", svc->name); return; } rc = getfilecon(svc->args[0], &fcon); if (rc < 0) { ERROR("could not get context while starting '%s'\n", svc->name); free(mycon); return; } rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon); if (rc == 0 && !strcmp(scon, mycon)) { ERROR("Service %s does not have a SELinux domain defined.\n", svc->name); free(mycon); free(fcon); free(scon); return; } free(mycon); free(fcon); if (rc < 0) { ERROR("could not get context while starting '%s'\n", svc->name); return; } } NOTICE("Starting service '%s'...\n", svc->name); pid_t pid = fork(); if (pid == 0) { struct socketinfo *si; struct svcenvinfo *ei; char tmp[32]; int fd, sz; umask(077); if (properties_initialized()) { get_property_workspace(&fd, &sz); snprintf(tmp, sizeof(tmp), "%d,%d", dup(fd), sz); add_environment("ANDROID_PROPERTY_WORKSPACE", tmp); } for (ei = svc->envvars; ei; ei = ei->next) add_environment(ei->name, ei->value); for (si = svc->sockets; si; si = si->next) { int socket_type = ( !strcmp(si->type, "stream") ? SOCK_STREAM : (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET)); int s = create_socket(si->name, socket_type, si->perm, si->uid, si->gid, si->socketcon ?: scon); if (s >= 0) { publish_socket(si->name, s); } } free(scon); scon = NULL; if (svc->writepid_files_) { std::string pid_str = android::base::StringPrintf("%d", pid); for (auto& file : *svc->writepid_files_) { if (!android::base::WriteStringToFile(pid_str, file)) { ERROR("couldn't write %s to %s: %s\n", pid_str.c_str(), file.c_str(), strerror(errno)); } } } if (svc->ioprio_class != IoSchedClass_NONE) { if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) { ERROR("Failed to set pid %d ioprio = %d,%d: %s\n", getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno)); } } if (needs_console) { setsid(); open_console(); } else { zap_stdio(); } if (false) { for (size_t n = 0; svc->args[n]; n++) { INFO("args[%zu] = '%s'\n", n, svc->args[n]); } for (size_t n = 0; ENV[n]; n++) { INFO("env[%zu] = '%s'\n", n, ENV[n]); } } setpgid(0, getpid()); // As requested, set our gid, supplemental gids, and uid. if (svc->gid) { if (setgid(svc->gid) != 0) { ERROR("setgid failed: %s\n", strerror(errno)); _exit(127); } } if (svc->nr_supp_gids) { if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) { ERROR("setgroups failed: %s\n", strerror(errno)); _exit(127); } } if (svc->uid) { if (setuid(svc->uid) != 0) { ERROR("setuid failed: %s\n", strerror(errno)); _exit(127); } } if (svc->seclabel) { if (setexeccon(svc->seclabel) < 0) { ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno)); _exit(127); } } if (!dynamic_args) { if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) { ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno)); } } else { char *arg_ptrs[INIT_PARSER_MAXARGS+1]; int arg_idx = svc->nargs; char *tmp = strdup(dynamic_args); char *next = tmp; char *bword; /* Copy the static arguments */ memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *))); while((bword = strsep(&next, " "))) { arg_ptrs[arg_idx++] = bword; if (arg_idx == INIT_PARSER_MAXARGS) break; } arg_ptrs[arg_idx] = NULL; execve(svc->args[0], (char**) arg_ptrs, (char**) ENV); } _exit(127); } free(scon); if (pid < 0) { ERROR("failed to start '%s'\n", svc->name); svc->pid = 0; return; } svc->time_started = gettime(); svc->pid = pid; svc->flags |= SVC_RUNNING; if ((svc->flags & SVC_EXEC) != 0) { INFO("SVC_EXEC pid %d (uid %d gid %d+%zu context %s) started; waiting...\n", svc->pid, svc->uid, svc->gid, svc->nr_supp_gids, svc->seclabel ? : "default"); waiting_for_exec = true; }