//---------------------------------------------------------------------------------------- int get_umask() { #ifdef _MSC_VER const int mask(_umask(0)); _umask(mask); #else const int mask(umask(0)); umask(mask); #endif return mask; }
bool file_write (const char *filename, const char *filedata, const size_t filesize) { FILE *fstream; /* set umask so that files are group-writable */ #if !defined(_WIN32_WCE) #ifdef WIN32 _umask (0002); #else umask (0002); #endif #endif fstream = fopen (filename, "wb"); if (fstream == NULL) { #if defined(_WIN32_WCE) LOG_ERR ("fopen (%s) failed", filename); #else LOG_ERR ("fopen (%s) return: %s", filename, strerror (errno)); #endif return FALSE; } if (fwrite (filedata, sizeof (char), filesize, fstream) != filesize) { #if defined(_WIN32_WCE) LOG_ERR ("fwrite (%s) failed", filename); #else LOG_ERR ("fwrite (%s) return: %s", filename, strerror (errno)); #endif fclose (fstream); return FALSE; } if (fclose (fstream) != 0) { #if defined(_WIN32_WCE) LOG_ERR ("close (%s) failed", filename); #else LOG_ERR ("close (%s) return: %s", filename, strerror (errno)); #endif return FALSE; } return TRUE; }
int main (int argc, char *argv[]) { static const char *options = "rv?"; static const struct option long_options[] = { { "help", 0, 0, '?' }, { "verbose", 0, 0, 'v' }, { 0, 0, 0, 0 } }; int c; char *cmdline; ignore_value (chdir ("/")); if (winsock_init () == -1) error (EXIT_FAILURE, 0, "winsock initialization failed"); #ifdef HAVE_REGISTER_PRINTF_SPECIFIER /* http://udrepper.livejournal.com/20948.html */ register_printf_specifier ('Q', print_shell_quote, print_arginfo); register_printf_specifier ('R', print_sysroot_shell_quote, print_arginfo); #else #ifdef HAVE_REGISTER_PRINTF_FUNCTION register_printf_function ('Q', print_shell_quote, print_arginfo); register_printf_function ('R', print_sysroot_shell_quote, print_arginfo); #else #error "HAVE_REGISTER_PRINTF_{SPECIFIER|FUNCTION} not defined" #endif #endif struct stat statbuf; if (stat ("/", &statbuf) == 0) root_device = statbuf.st_dev; for (;;) { c = getopt_long (argc, argv, options, long_options, NULL); if (c == -1) break; switch (c) { /* The -r flag is used when running standalone. It changes * several aspects of the daemon. */ case 'r': sysroot = ""; sysroot_len = 0; autosync_umount = 0; break; case 'v': verbose = 1; break; case '?': usage (); exit (EXIT_SUCCESS); default: fprintf (stderr, "guestfsd: unexpected command line option 0x%x\n", c); exit (EXIT_FAILURE); } } if (optind < argc) { usage (); exit (EXIT_FAILURE); } cmdline = read_cmdline (); /* Set the verbose flag. */ verbose = verbose || (cmdline && strstr (cmdline, "guestfs_verbose=1") != NULL); if (verbose) printf ("verbose daemon enabled\n"); if (verbose) { if (cmdline) printf ("linux commmand line: %s\n", cmdline); else printf ("could not read linux command line\n"); } #ifndef WIN32 /* Make sure SIGPIPE doesn't kill us. */ struct sigaction sa; memset (&sa, 0, sizeof sa); sa.sa_handler = SIG_IGN; sa.sa_flags = 0; if (sigaction (SIGPIPE, &sa, NULL) == -1) perror ("sigaction SIGPIPE"); /* but try to continue anyway ... */ #endif #ifdef WIN32 # define setenv(n,v,f) _putenv(n "=" v) #endif /* Set up a basic environment. After we are called by /init the * environment is essentially empty. * https://bugzilla.redhat.com/show_bug.cgi?id=502074#c5 * * NOTE: if you change $PATH, you must also change 'prog_exists' * function below. */ setenv ("PATH", "/sbin:/usr/sbin:/bin:/usr/bin", 1); setenv ("SHELL", "/bin/sh", 1); setenv ("LC_ALL", "C", 1); setenv ("TERM", "dumb", 1); #ifndef WIN32 /* We document that umask defaults to 022 (it should be this anyway). */ umask (022); #else /* This is the default for Windows anyway. It's not even clear if * Windows ever uses this -- the MSDN documentation for the function * contains obvious errors. */ _umask (0); #endif /* Make a private copy of /etc/lvm so we can change the config (see * daemon/lvm-filter.c). */ copy_lvm (); /* Connect to virtio-serial channel. */ int sock = open (VIRTIO_SERIAL_CHANNEL, O_RDWR | O_CLOEXEC); if (sock == -1) { fprintf (stderr, "\n" "Failed to connect to virtio-serial channel.\n" "\n" "This is a fatal error and the appliance will now exit.\n" "\n" "Usually this error is caused by either QEMU or the appliance\n" "kernel not supporting the vmchannel method that the\n" "libguestfs library chose to use. Please run\n" "'libguestfs-test-tool' and provide the complete, unedited\n" "output to the libguestfs developers, either in a bug report\n" "or on the libguestfs redhat com mailing list.\n" "\n"); perror (VIRTIO_SERIAL_CHANNEL); exit (EXIT_FAILURE); } /* Send the magic length message which indicates that * userspace is up inside the guest. */ char lenbuf[4]; XDR xdr; uint32_t len = GUESTFS_LAUNCH_FLAG; xdrmem_create (&xdr, lenbuf, sizeof lenbuf, XDR_ENCODE); xdr_u_int (&xdr, &len); if (xwrite (sock, lenbuf, sizeof lenbuf) == -1) { perror ("xwrite"); exit (EXIT_FAILURE); } xdr_destroy (&xdr); /* Enter the main loop, reading and performing actions. */ main_loop (sock); exit (EXIT_SUCCESS); }
int umask(int md) { return _umask(md); }
File my_win_sopen(const char *path, int oflag, int shflag, int pmode) { int fh; /* handle of opened file */ int mask; HANDLE osfh; /* OS handle of opened file */ DWORD fileaccess; /* OS file access (requested) */ DWORD fileshare; /* OS file sharing mode */ DWORD filecreate; /* OS method of opening/creating */ DWORD fileattrib; /* OS file attribute flags */ SECURITY_ATTRIBUTES SecurityAttributes; DBUG_ENTER("my_win_sopen"); if (check_if_legal_filename(path)) { errno= EACCES; DBUG_RETURN(-1); } SecurityAttributes.nLength= sizeof(SecurityAttributes); SecurityAttributes.lpSecurityDescriptor= NULL; SecurityAttributes.bInheritHandle= !(oflag & _O_NOINHERIT); /* decode the access flags */ switch (oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) { case _O_RDONLY: /* read access */ fileaccess= GENERIC_READ; break; case _O_WRONLY: /* write access */ fileaccess= GENERIC_WRITE; break; case _O_RDWR: /* read and write access */ fileaccess= GENERIC_READ | GENERIC_WRITE; break; default: /* error, bad oflag */ errno= EINVAL; DBUG_RETURN(-1); } /* decode sharing flags */ switch (shflag) { case _SH_DENYRW: /* exclusive access except delete */ fileshare= FILE_SHARE_DELETE; break; case _SH_DENYWR: /* share read and delete access */ fileshare= FILE_SHARE_READ | FILE_SHARE_DELETE; break; case _SH_DENYRD: /* share write and delete access */ fileshare= FILE_SHARE_WRITE | FILE_SHARE_DELETE; break; case _SH_DENYNO: /* share read, write and delete access */ fileshare= FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; break; case _SH_DENYRWD: /* exclusive access */ fileshare= 0L; break; case _SH_DENYWRD: /* share read access */ fileshare= FILE_SHARE_READ; break; case _SH_DENYRDD: /* share write access */ fileshare= FILE_SHARE_WRITE; break; case _SH_DENYDEL: /* share read and write access */ fileshare= FILE_SHARE_READ | FILE_SHARE_WRITE; break; default: /* error, bad shflag */ errno= EINVAL; DBUG_RETURN(-1); } /* decode open/create method flags */ switch (oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) { case 0: case _O_EXCL: /* ignore EXCL w/o CREAT */ filecreate= OPEN_EXISTING; break; case _O_CREAT: filecreate= OPEN_ALWAYS; break; case _O_CREAT | _O_EXCL: case _O_CREAT | _O_TRUNC | _O_EXCL: filecreate= CREATE_NEW; break; case _O_TRUNC: case _O_TRUNC | _O_EXCL: /* ignore EXCL w/o CREAT */ filecreate= TRUNCATE_EXISTING; break; case _O_CREAT | _O_TRUNC: filecreate= CREATE_ALWAYS; break; default: /* this can't happen ... all cases are covered */ errno= EINVAL; DBUG_RETURN(-1); } /* decode file attribute flags if _O_CREAT was specified */ fileattrib= FILE_ATTRIBUTE_NORMAL; /* default */ if (oflag & _O_CREAT) { _umask((mask= _umask(0))); if (!((pmode & ~mask) & _S_IWRITE)) fileattrib= FILE_ATTRIBUTE_READONLY; } /* Set temporary file (delete-on-close) attribute if requested. */ if (oflag & _O_TEMPORARY) { fileattrib|= FILE_FLAG_DELETE_ON_CLOSE; fileaccess|= DELETE; } /* Set temporary file (delay-flush-to-disk) attribute if requested.*/ if (oflag & _O_SHORT_LIVED) fileattrib|= FILE_ATTRIBUTE_TEMPORARY; /* Set sequential or random access attribute if requested. */ if (oflag & _O_SEQUENTIAL) fileattrib|= FILE_FLAG_SEQUENTIAL_SCAN; else if (oflag & _O_RANDOM) fileattrib|= FILE_FLAG_RANDOM_ACCESS; /* try to open/create the file */ if ((osfh= CreateFile(path, fileaccess, fileshare, &SecurityAttributes, filecreate, fileattrib, NULL)) == INVALID_HANDLE_VALUE) { /* OS call to open/create file failed! map the error, release the lock, and return -1. note that it's not necessary to call _free_osfhnd (it hasn't been used yet). */ my_osmaperr(GetLastError()); /* map error */ DBUG_RETURN(-1); /* return error to caller */ } if ((fh= my_open_osfhandle(osfh, oflag & (_O_APPEND | _O_RDONLY | _O_TEXT))) == -1) { CloseHandle(osfh); } DBUG_RETURN(fh); /* return handle */ }
int main (int argc, char *argv[]) { static const char *options = "rv?"; static const struct option long_options[] = { { "help", 0, 0, '?' }, { "verbose", 0, 0, 'v' }, { 0, 0, 0, 0 } }; int c; char *cmdline; ignore_value (chdir ("/")); if (winsock_init () == -1) error (EXIT_FAILURE, 0, "winsock initialization failed"); #ifdef HAVE_REGISTER_PRINTF_SPECIFIER /* http://udrepper.livejournal.com/20948.html */ register_printf_specifier ('Q', print_shell_quote, print_arginfo); register_printf_specifier ('R', print_sysroot_shell_quote, print_arginfo); #else #ifdef HAVE_REGISTER_PRINTF_FUNCTION register_printf_function ('Q', print_shell_quote, print_arginfo); register_printf_function ('R', print_sysroot_shell_quote, print_arginfo); #else #error "HAVE_REGISTER_PRINTF_{SPECIFIER|FUNCTION} not defined" #endif #endif /* XXX The appliance /init script sets LD_PRELOAD=../libSegFault.so. * However if we CHROOT_IN to the sysroot that file might not exist, * resulting in all commands failing. What we'd really like to do * is to have LD_PRELOAD only set while outside the chroot. I * suspect the proper way to solve this is to remove the * CHROOT_IN/_OUT hack and replace it properly (fork), but that is * for another day. */ unsetenv ("LD_PRELOAD"); struct stat statbuf; if (stat ("/", &statbuf) == 0) root_device = statbuf.st_dev; for (;;) { c = getopt_long (argc, argv, options, long_options, NULL); if (c == -1) break; switch (c) { /* The -r flag is used when running standalone. It changes * several aspects of the daemon. */ case 'r': sysroot = ""; sysroot_len = 0; autosync_umount = 0; break; case 'v': verbose = 1; break; case '?': usage (); exit (EXIT_SUCCESS); default: fprintf (stderr, "guestfsd: unexpected command line option 0x%x\n", c); exit (EXIT_FAILURE); } } if (optind < argc) { usage (); exit (EXIT_FAILURE); } cmdline = read_cmdline (); /* Set the verbose flag. */ verbose = verbose || (cmdline && strstr (cmdline, "guestfs_verbose=1") != NULL); if (verbose) printf ("verbose daemon enabled\n"); if (verbose) { if (cmdline) printf ("linux commmand line: %s\n", cmdline); else printf ("could not read linux command line\n"); } enable_network = cmdline && strstr (cmdline, "guestfs_network=1") != NULL; #ifndef WIN32 /* Make sure SIGPIPE doesn't kill us. */ struct sigaction sa; memset (&sa, 0, sizeof sa); sa.sa_handler = SIG_IGN; sa.sa_flags = 0; if (sigaction (SIGPIPE, &sa, NULL) == -1) perror ("sigaction SIGPIPE"); /* but try to continue anyway ... */ #endif #ifdef WIN32 # define setenv(n,v,f) _putenv(n "=" v) #endif /* Set up a basic environment. After we are called by /init the * environment is essentially empty. * https://bugzilla.redhat.com/show_bug.cgi?id=502074#c5 * * NOTE: if you change $PATH, you must also change 'prog_exists' * function below. */ setenv ("PATH", "/sbin:/usr/sbin:/bin:/usr/bin", 1); setenv ("SHELL", "/bin/sh", 1); setenv ("LC_ALL", "C", 1); setenv ("TERM", "dumb", 1); #ifndef WIN32 /* We document that umask defaults to 022 (it should be this anyway). */ umask (022); #else /* This is the default for Windows anyway. It's not even clear if * Windows ever uses this -- the MSDN documentation for the function * contains obvious errors. */ _umask (0); #endif /* Make a private copy of /etc/lvm so we can change the config (see * daemon/lvm-filter.c). */ copy_lvm (); /* Connect to virtio-serial channel. */ char *channel, *p; if (cmdline && (p = strstr (cmdline, "guestfs_channel=")) != NULL) { p += 16; channel = strndup (p, strcspn (p, " \n")); } else channel = strdup (VIRTIO_SERIAL_CHANNEL); if (!channel) { perror ("strdup"); exit (EXIT_FAILURE); } if (verbose) printf ("trying to open virtio-serial channel '%s'\n", channel); int sock = open (channel, O_RDWR|O_CLOEXEC); if (sock == -1) { fprintf (stderr, "\n" "Failed to connect to virtio-serial channel.\n" "\n" "This is a fatal error and the appliance will now exit.\n" "\n" "Usually this error is caused by either QEMU or the appliance\n" "kernel not supporting the vmchannel method that the\n" "libguestfs library chose to use. Please run\n" "'libguestfs-test-tool' and provide the complete, unedited\n" "output to the libguestfs developers, either in a bug report\n" "or on the libguestfs redhat com mailing list.\n" "\n"); perror (channel); exit (EXIT_FAILURE); } /* If it's a serial-port like device then it probably has echoing * enabled. Put it into complete raw mode. */ if (STRPREFIX (channel, "/dev/ttyS")) makeraw (channel, sock); /* cmdline, channel not used after this point */ free (cmdline); free (channel); /* Wait for udev devices to be created. If you start libguestfs, * especially with disks that contain complex (eg. mdadm) data * already, then it is possible for the 'mdadm' and LVM commands * that the init script runs to have not completed by the time the * daemon starts executing library commands. (This is very rare and * hard to test however, but we have seen it in 'brew'). Run * udev_settle, but do it as late as possible to minimize the chance * that we'll have to do any waiting here. */ udev_settle (); /* Send the magic length message which indicates that * userspace is up inside the guest. */ char lenbuf[4]; XDR xdr; uint32_t len = GUESTFS_LAUNCH_FLAG; xdrmem_create (&xdr, lenbuf, sizeof lenbuf, XDR_ENCODE); xdr_u_int (&xdr, &len); if (xwrite (sock, lenbuf, sizeof lenbuf) == -1) { perror ("xwrite"); exit (EXIT_FAILURE); } xdr_destroy (&xdr); /* Enter the main loop, reading and performing actions. */ main_loop (sock); exit (EXIT_SUCCESS); }