Пример #1
0
int main(int argc, char **argv, char **envp) {
	int retval = 0;

	/* console */
	int ttyfd;
	if ((ttyfd =open_write("/dev/console")) != 1) {
		dup2(ttyfd, 0); dup2(ttyfd, 1); dup2(ttyfd, 2);
		if (ttyfd > 2) close(ttyfd);
	}

	printf("Booting!\n");

	CHECK(mdev(envp));
	CHECK(mount(ROOT_DEV, ROOT_PATH, ROOT_FS, 0, ""));

	// if kernel doesn't exists
	CHECK(chdir(ROOT_PATH));
	CHECK(chroot(ROOT_PATH));

	execve(INIT, NULL, envp);
	// else
	//CHECK(kexec(ROOT_PATH));

cleanup:
	chdir("/");
	umount(ROOT_DEV);
	remove(ROOT_DEV);
	FAIL(execve(INIT_ANDROID, argv, envp));

	return retval;
}
Пример #2
0
int main()
{
	// TODO: this needs to be initialized by user input
	float fArray[count];
	float number;

	// TODO: fix this stupid thing
	// Not sure why this doesn't work...
	// I want to replace the for(int i...) below...
/*	for (float &fa : fArray)
	{
		number = get_rand();
		fArray[&fa] = number;
		std::cout << number << " ";
	}*/

	// Retrieve random numbers, shove them
	// into the array, then print them out,
	// one at a time.
	for (int i = 0; i < count; ++i)
	{
		number = get_rand();
		fArray[i] = number;
		std::cout << number << " ";
	}
	std::cout << std::endl;

	// sorting the array contents
	std::sort(std::begin(fArray), std::end(fArray));

	// Yet this one does work...
	for (float &s : fArray)
	{
		std::cout << s << " ";
	}
	std::cout << std::endl;

	// Output what we need to know about the array
	std::cout << "minimum value:   " << min(fArray) << std::endl;
	std::cout << "maximum value:   " << max(fArray) << std::endl;
	std::cout << "average value:   " << avg(fArray) << std::endl;
	std::cout << "mean deviation:  " << mdev(fArray) << std::endl;

	return 0;
}
Пример #3
0
int main(int argc, char **argv, char **envp)
{
	FILE *log;
	char *line,          // where we place the readed line
             *start,         // where our args start
             *root,          // directory to chroot
             *blkdev,        // block device to mount on newroot
             **new_argv;     // init args
	int i,mounted_twice; // general purpose integer

	line = blkdev = root = NULL;
	new_argv = NULL;
	i=mounted_twice=0;

#define EXIT_SILENT     fclose(log); \
                        fatal(argv,envp); \
                        exit(EXIT_FAILURE);
#define EXIT_ERROR(err) fprintf(log, err " - %s\n", strerror(errno)); \
                        EXIT_SILENT
			
	if((log = fopen(LOG,"w")) == NULL)
	{
		fatal(argv,envp);
		exit(EXIT_FAILURE);
	}
	// mount /proc
	if(mount("proc", "/proc", "proc", MS_RELATIME, ""))
	{
		EXIT_ERROR("unable to mount /proc");
	}
	// alloc line
	if((line = malloc(COMMAND_LINE_SIZE*sizeof(char))) == NULL)
	{
		umount("/proc");
		EXIT_ERROR("malloc");
	}
	// read cmdline
	if(read_our_cmdline(line))
	{
		umount("/proc");
		free(line);
		EXIT_ERROR("unable to read /proc/cmdline");
	}
	umount("/proc");
	if (!(start=strstr(line,CMDLINE_OPTION)))
	{
		fprintf(log,"unable to find \"%s\" in \"%s\"\n",CMDLINE_OPTION,line);
		free(line);
		EXIT_SILENT;
	}
	start+=CMDLINE_OPTION_LEN;
	if(parser(start,&blkdev,&root,&new_argv))
	{
		free(line);
		EXIT_ERROR("parsing failed");
	}
	free(line);
	if(mount("sysfs","/sys","sysfs",MS_RELATIME,""))
	{
		EXIT_ERROR("unable to mount /sys");
	}
	mdev(envp);
	// make sure this was made
	for(i=1;access(blkdev, R_OK) && i < TIMEOUT;i++)
	{
		sleep(1);
		mdev(envp);
	}
	umount("/sys");
	//mount blkdev on NEWROOT
	if(mount(blkdev,NEWROOT,"ext4",0,""))
	{
		fprintf(log,"unable to mount \"%s\" on %s - %s\n",blkdev,NEWROOT,strerror(errno));
		free(blkdev);
		free(root);
		for(i=0;new_argv[i];i++)
			free(new_argv[i]);
		EXIT_SILENT;
	}
	free(blkdev);
	blkdev = root; // keep a track of the old pointer
	//if root is an ext image mount it on NEWROOT
	//if root is an initrd(.gz)? file extract it into NEWROOT
	if(!try_loop_mount(&root,NEWROOT) && !try_initrd_mount(&root,NEWROOT))
	{
		if(blkdev != root) // NEWROOT has been mounted again
			mounted_twice=1;
		//check for init existence
		i=strlen(root) + strlen(new_argv[0]);
		if((line=malloc((i+1)*sizeof(char))))
		{
			strncpy(line,root,i);
			strncat(line,new_argv[0],i);
			line[i]='\0';
			if(!access(line,R_OK|X_OK))
			{
				if(!chdir(root) && !chroot(root))
				{
					free(root);
					fclose(log);
					execve(new_argv[0],new_argv,envp);
				}
				else
				{
					fprintf(log,"cannot chroot/chdir to \"%s\" - %s\n",root,strerror(errno));
					free(root);
					for(i=0;new_argv[i];i++)
						free(new_argv[i]);
					fclose(log);
					umount(NEWROOT);
					if(mounted_twice)
						umount(NEWROOT);
				}
			}
			else
			{
				fprintf(log,"cannot execute \"%s\" - %s\n",line,strerror(errno));
				free(line);
				free(root);
				for(i=0;new_argv[i];i++)
					free(new_argv[i]);
				fclose(log);
				umount(NEWROOT);
				if(mounted_twice)
					umount(NEWROOT);
			}
		}
		else
		{
			fprintf(log,"malloc - %s\n",strerror(errno));
			free(root);
			for(i=0;new_argv[i];i++)
				free(new_argv[i]);
			fclose(log);
			umount(NEWROOT);
			if(mounted_twice)
				umount(NEWROOT);
		}
	}
	else
	{
		if(root) // try_loop_mount reallocate root, a malloc problem can be happend
		{
			if(blkdev!=root)
				umount(NEWROOT);
			fprintf(log,"try_(loop/initrd)_mount \"%s\" on %s - %s\n",root,NEWROOT,strerror(errno));
			free(root);
		}
		else
			fprintf(log,"try_loop_mount NULL root - %s\n",strerror(errno));
		for(i=0;new_argv[i];i++)
			free(new_argv[i]);
		fclose(log);
		umount(NEWROOT);
	}
	fatal(argv,envp);
	exit(EXIT_FAILURE);
}
Пример #4
0
int main(int argc, char **argv, char **envp) //TODO: should declare it as void ?
{
    FILE *log,*input;
    char 	*line, // where we place the readed line
            *root, // directory to chroot
            *blkdev, // block device to mount on newroot
            *pos, // current position
            **new_argv; // init args
    int i; // used for loops


#ifdef ADB
    //provide ADB access
    if(!fork())
        fatal(argv,envp);
#endif

    if((log = fopen(LOG,"w")) != NULL)
    {
        if(!mount("sysfs","/sys","sysfs",MS_RELATIME,"")) // mount sys
        {
            mdev(envp);
            umount("/sys");
            //mount DATA_DEV partition into /data
            if(!mount(DATA_DEV,"/data","ext4",0,""))
            {
                if((input = fopen(BOOT_FILE,"r")) != NULL)
                {
                    if((line = malloc(MAX_LINE)) != NULL && (blkdev = malloc(MAX_LINE)) != NULL && (root = malloc(MAX_LINE)) != NULL && (new_argv = malloc(2*sizeof(char*))) != NULL && (new_argv[0] = malloc(MAX_LINE)) != NULL)
                    {
                        if(fgets(line,MAX_LINE,input))
                        {
                            strcpy(root,"/newroot/");
                            for(i=0,pos=line; *pos!=':'&&*pos!='\0'&&*pos!='\n'; pos++)
                                blkdev[i++] = *pos;
                            blkdev[i] ='\0';
                            if(*pos==':')
                                pos++;
                            if(*pos=='/')
                                pos++;
                            for(i=9; *pos!=':'&&*pos!='\0'&&*pos!='\n'; pos++)
                                root[i++] = *pos;
                            root[i] = '\0';
                            if(*pos==':')
                                pos++;
                            for(i=0; *pos!='\0'&&*pos!='\n'; pos++)
                                new_argv[0][i++] = *pos;
                            new_argv[1] = NULL;
                            free(line);
                            fclose(input);
                            umount("/data");
                            //check if blkdev exist, otherwise wait until TIMEOUT
                            if(access(blkdev,R_OK) && !mount("sysfs","/sys","sysfs",MS_RELATIME,""))
                            {
                                sleep(1);
                                mdev(envp);
                                for(i=1; access(blkdev,R_OK) && i < TIMEOUT; i++)
                                {
                                    sleep(1);
                                    mdev(envp);
                                }
                                umount("/sys");
                            }
                            if(!mount(blkdev,NEWROOT,"ext4",0,""))
                            {
                                free(blkdev);
                                if(!chdir(root) && !chroot(root))
                                {
                                    free(root);
                                    if(!access(new_argv[0],X_OK))
                                    {
                                        fclose(log);
                                        execve(new_argv[0],new_argv,envp);
                                    }
                                    else
                                    {
                                        fprintf(log,"cannot execute \"%s\" - %s\n",new_argv[0],strerror(errno));
                                        free(new_argv[0]);
                                        free(new_argv);
                                        fclose(log);
                                        umount(NEWROOT);
                                    }
                                }
                                else
                                {
                                    fprintf(log,"unable to chdir/chroot to \"%s\" - %s\n",root,strerror(errno));
                                    free(root);
                                    free(new_argv[0]);
                                    free(new_argv);
                                    fclose(log);
                                    umount(NEWROOT);
                                }
                            }
                            else
                            {
                                fprintf(log,"unable to mount \"%s\" on %s - %s\n",blkdev,NEWROOT,strerror(errno));
                                free(blkdev);
                                free(root);
                                free(new_argv[0]);
                                free(new_argv);
                                fclose(log);
                            }
                        }
                        else
                        {
                            fprintf(log,"while reading \"%s\" - %s\n",BOOT_FILE,strerror(errno));
                            free(line);
                            free(blkdev);
                            free(root);
                            free(new_argv[0]);
                            free(new_argv);
                            fclose(input);
                            umount("/data");
                            fclose(log);
                        }
                    }
                    else
                    {
                        //TODO: check for single malloc calls and free the 'done well' calls results.
                        fprintf(log,"malloc - %s\n",strerror(errno));
                        fclose(input);
                        umount("/data");
                        fclose(log);
                    }
                }
                else
                {
                    fprintf(log,"unable to open \"%s\" - %s\n",BOOT_FILE,strerror(errno));
                    umount("/data");
                    fclose(log);
                }
            }
            else
            {
                fprintf(log,"unable to mount %s on /data - %s\n",DATA_DEV,strerror(errno));
                fclose(log);
            }
        }
        else
        {
            fprintf(log, "unable to mount /sys - %s\n",strerror(errno));
            fclose(log);
        }
    }
    fatal(argv,envp);
    return EXIT_FAILURE; // just for make gcc don't fire up warnings
}