t_modcmd.c revision 1.5
1/* $NetBSD: t_modcmd.c,v 1.5 2010/03/05 18:49:30 pooka Exp $ */ 2 3/* 4 * Copyright (c) 2009 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30#include <sys/types.h> 31#include <sys/mount.h> 32 33#include <rump/rump.h> 34#include <rump/rump_syscalls.h> 35 36#include <fs/tmpfs/tmpfs_args.h> 37 38#include <atf-c.h> 39#include <dlfcn.h> 40#include <err.h> 41#include <errno.h> 42#include <stdio.h> 43#include <stdlib.h> 44#include <string.h> 45#include <unistd.h> 46#include <util.h> 47 48#include "../../h_macros.h" 49 50/* 51 * We verify that modules can be loaded and unloaded. 52 * tmpfs was chosen because it does not depend on an image. 53 */ 54 55ATF_TC(cmsg_modcmd); 56ATF_TC_HEAD(cmsg_modcmd, tc) 57{ 58 atf_tc_set_md_var(tc, "descr", "Checks that loading and unloading " 59 "a module (vfs/tmpfs) is possible"); 60} 61 62#define TMPFSMODULE "librumpfs_tmpfs.so" 63ATF_TC_BODY(cmsg_modcmd, tc) 64{ 65 struct tmpfs_args args; 66 const struct modinfo *const *mi_start, *const *mi_end; 67 void *handle; 68 int i, rv, loop = 0; 69 70 rump_init(); 71 memset(&args, 0, sizeof(args)); 72 args.ta_version = TMPFS_ARGS_VERSION; 73 args.ta_root_mode = 0777; 74 75 if (rump_sys_mkdir("/mp", 0777) == -1) 76 atf_tc_fail_errno("mkdir mountpoint"); 77 if (rump_sys_mount(MOUNT_TMPFS, "/mp", 0, &args, sizeof(args)) != -1) 78 atf_tc_fail("mount unexpectedly succeeded"); 79 80 handle = dlopen(TMPFSMODULE, RTLD_GLOBAL); 81 if (handle == NULL) { 82 const char *dlmsg = dlerror(); 83 atf_tc_fail("cannot open %s: %s", TMPFSMODULE, dlmsg); 84 } 85 86 again: 87 mi_start = dlsym(handle, "__start_link_set_modules"); 88 mi_end = dlsym(handle, "__stop_link_set_modules"); 89 if (mi_start == NULL || mi_end == NULL) 90 atf_tc_fail("cannot find module info"); 91 if ((rv = rump_pub_module_init(mi_start, (size_t)(mi_end-mi_start)))!=0) 92 atf_tc_fail("module init failed: %d (%s)", rv, strerror(rv)); 93 if ((rv = rump_pub_module_init(mi_start, (size_t)(mi_end-mi_start)))==0) 94 atf_tc_fail("module double init succeeded"); 95 96 if (rump_sys_mount(MOUNT_TMPFS, "/mp", 0, &args, sizeof(args)) == -1) 97 atf_tc_fail_errno("still cannot mount"); 98 if (rump_sys_unmount("/mp", 0) == -1) 99 atf_tc_fail("cannot unmount"); 100 for (i = 0; i < (int)(mi_end-mi_start); i++) { 101 if ((rv = rump_pub_module_fini(mi_start[i])) != 0) 102 atf_tc_fail("module fini failed: %d (%s)", 103 rv, strerror(rv)); 104 } 105 for (i = 0; i < (int)(mi_end-mi_start); i++) { 106 if ((rv = rump_pub_module_fini(mi_start[i])) == 0) 107 atf_tc_fail("module double fini succeeded"); 108 } 109 if (loop++ == 0) 110 goto again; 111 112 if (dlclose(handle)) { 113 const char *dlmsg = dlerror(); 114 atf_tc_fail("cannot close %s: %s", TMPFSMODULE, dlmsg); 115 } 116 117 if (rump_sys_mount(MOUNT_TMPFS, "/mp", 0, &args, sizeof(args)) != -1) 118 atf_tc_fail("mount unexpectedly succeeded"); 119} 120 121ATF_TP_ADD_TCS(tp) 122{ 123 ATF_TP_ADD_TC(tp, cmsg_modcmd); 124 125 return atf_no_error(); 126} 127