Commit 9d728e8c4ed000b1d6a77230d11b3761a7c8b5a1

Authored by bellard
1 parent 36d54d15

smb support


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@1059 c046a42c-6fe2-441c-8c8c-71466251a162
Showing 1 changed file with 81 additions and 0 deletions
... ... @@ -38,6 +38,7 @@
38 38 #include <sys/mman.h>
39 39 #include <sys/ioctl.h>
40 40 #include <sys/socket.h>
  41 +#include <dirent.h>
41 42 #ifdef _BSD
42 43 #include <sys/stat.h>
43 44 #ifndef __APPLE__
... ... @@ -1453,6 +1454,80 @@ static void net_slirp_redir(const char *redir_str)
1453 1454 fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
1454 1455 exit(1);
1455 1456 }
  1457 +
  1458 +char smb_dir[1024];
  1459 +
  1460 +static void smb_exit(void)
  1461 +{
  1462 + DIR *d;
  1463 + struct dirent *de;
  1464 + char filename[1024];
  1465 +
  1466 + /* erase all the files in the directory */
  1467 + d = opendir(smb_dir);
  1468 + for(;;) {
  1469 + de = readdir(d);
  1470 + if (!de)
  1471 + break;
  1472 + if (strcmp(de->d_name, ".") != 0 &&
  1473 + strcmp(de->d_name, "..") != 0) {
  1474 + snprintf(filename, sizeof(filename), "%s/%s",
  1475 + smb_dir, de->d_name);
  1476 + unlink(filename);
  1477 + }
  1478 + }
  1479 + rmdir(smb_dir);
  1480 +}
  1481 +
  1482 +/* automatic user mode samba server configuration */
  1483 +void net_slirp_smb(const char *exported_dir)
  1484 +{
  1485 + char smb_conf[1024];
  1486 + char smb_cmdline[1024];
  1487 + FILE *f;
  1488 +
  1489 + if (!slirp_inited) {
  1490 + slirp_inited = 1;
  1491 + slirp_init();
  1492 + }
  1493 +
  1494 + /* XXX: better tmp dir construction */
  1495 + snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
  1496 + if (mkdir(smb_dir, 0700) < 0) {
  1497 + fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
  1498 + exit(1);
  1499 + }
  1500 + snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
  1501 +
  1502 + f = fopen(smb_conf, "w");
  1503 + if (!f) {
  1504 + fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
  1505 + exit(1);
  1506 + }
  1507 + fprintf(f,
  1508 + "[global]\n"
  1509 + "pid directory=%s\n"
  1510 + "lock directory=%s\n"
  1511 + "log file=%s/log.smbd\n"
  1512 + "smb passwd file=%s/smbpasswd\n"
  1513 + "[qemu]\n"
  1514 + "path=%s\n"
  1515 + "read only=no\n"
  1516 + "guest ok=yes\n",
  1517 + smb_dir,
  1518 + smb_dir,
  1519 + smb_dir,
  1520 + smb_dir,
  1521 + exported_dir
  1522 + );
  1523 + fclose(f);
  1524 + atexit(smb_exit);
  1525 +
  1526 + snprintf(smb_cmdline, sizeof(smb_cmdline), "/usr/sbin/smbd -s %s",
  1527 + smb_conf);
  1528 +
  1529 + slirp_add_exec(0, smb_cmdline, 4, 139);
  1530 +}
1456 1531  
1457 1532 #endif /* CONFIG_SLIRP */
1458 1533  
... ... @@ -2407,6 +2482,7 @@ void help(void)
2407 2482 #ifdef CONFIG_SLIRP
2408 2483 "-user-net use user mode network stack [default if no tap/tun script]\n"
2409 2484 "-tftp prefix allow tftp access to files starting with prefix [-user-net]\n"
  2485 + "-smb dir allow SMB access to files in 'dir' [-user-net]\n"
2410 2486 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
2411 2487 " redirect TCP or UDP connections from host to guest [-user-net]\n"
2412 2488 #endif
... ... @@ -2484,6 +2560,7 @@ enum {
2484 2560 QEMU_OPTION_tun_fd,
2485 2561 QEMU_OPTION_user_net,
2486 2562 QEMU_OPTION_tftp,
  2563 + QEMU_OPTION_smb,
2487 2564 QEMU_OPTION_redir,
2488 2565 QEMU_OPTION_dummy_net,
2489 2566  
... ... @@ -2538,6 +2615,7 @@ const QEMUOption qemu_options[] = {
2538 2615 #ifdef CONFIG_SLIRP
2539 2616 { "user-net", 0, QEMU_OPTION_user_net },
2540 2617 { "tftp", HAS_ARG, QEMU_OPTION_tftp },
  2618 + { "smb", HAS_ARG, QEMU_OPTION_smb },
2541 2619 { "redir", HAS_ARG, QEMU_OPTION_redir },
2542 2620 #endif
2543 2621 { "dummy-net", 0, QEMU_OPTION_dummy_net },
... ... @@ -2834,6 +2912,9 @@ int main(int argc, char **argv)
2834 2912 case QEMU_OPTION_tftp:
2835 2913 tftp_prefix = optarg;
2836 2914 break;
  2915 + case QEMU_OPTION_smb:
  2916 + net_slirp_smb(optarg);
  2917 + break;
2837 2918 case QEMU_OPTION_user_net:
2838 2919 net_if_type = NET_IF_USER;
2839 2920 break;
... ...