【问题标题】:get list of volumes mounted on a solaris 11.2 machine获取安装在 solaris 11.2 机器上的卷列表
【发布时间】:2014-12-02 07:41:50
【问题描述】:

我使用以下代码作为参考从 Solaris 11.2 机器获取卷列表:

#include <time.h>
#include <sys/mntent.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <errno.h>


struct mntent
  {
char *mnt_fsname;       /* Device or server for filesystem.  */
char *mnt_dir;      /* Directory mounted on.  */
char *mnt_type;     /* Type of filesystem: ufs, nfs, etc.  */
char *mnt_opts;     /* Comma-separated options for fs.  */
int mnt_freq;       /* Dump frequency (in days).  */
int mnt_passno;     /* Pass number for `fsck'.  */
};

char ** getVolumeList(int * size)
{
char ** volList = NULL;
int listLen = 0;
char tmp[1024];
struct mntent* vp ; 
FILE *fp=fopen("/etc/mnttab", "r");
int result;
printf("in getVolumeList\n");
printf("coming here 1\n");
volList = (char **) malloc(sizeof(char*) *  (*size));
printf("coming here \n");

vp = getmntent(fp);
    if (vp == NULL)
      printf("vp is null");
  result = getmntent(fp,vp);
  if(result == -1)
{
  printf ("getmntent returned:%d. Hence breaking from loop\n", result);
}

/*
   ignore all entries for which mount options is set to ignore
   these don't show up in dk -k also although dk -a shows them
  */

  if(vp->mnt_dir != NULL)
    {
      strcpy(tmp,vp->mnt_dir);
                   volList[listLen] = (char*) malloc(sizeof(char) * (strlen(tmp)+1));
                    strcpy(volList[listLen], tmp);
    listLen++;
  }
  else
{
  printf("\nmountp is NULL \n");
}



printf("freeing vp\n");

    if (vp != NULL)
    {
  free(vp);
    }
    if (fp != NULL)
    {
  fclose(fp);
    }

*size = listLen;
 printf("returning from getVolumeList\n");

return volList;

}

但这似乎引发了以下段错误:

     #0  0xfe663923 in getmntent_compat () from /lib/libc.so.1

我猜这是因为 solaris 11.2 上的 mntent.h 在头文件中没有定义 struct mntent 或以下方法签名: getmntent(); getmntent(fp,vp)

是否有任何其他等效的方法来确定安装在 Solaris 机器上的卷,或者更好的独立于操作系统的方法来确定当前安装的卷?

附:我是 C 新手。

【问题讨论】:

  • 1:包含 文件以获取结构定义。 2> getmntent 只接受一个参数,你至少传递两个参数一次。编译时启用警告。 3> 不要释放mntent。 4>不要使用fopen,使用setmntent。 5> tmp 至少应为 PATH_MAX 长。 6> 使用 endmntent 停止读取文件。可能还有其他问题。 :)
  • 另外,你根本不需要 tmp 变量,另外,如果你使用 strdup,就不需要 malloc+strcpy。
  • 或者简单地说:system("mount");

标签: c filesystems solaris mount


【解决方案1】:

代码中有几个不同的错误。这些方面的东西可能会起作用:

#include <stdio.h>
#include <mntent.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

/* Return a NULL-terminated list of filesystem mountpoints, or NULL on error.
 * The individual strings in the result need to be freed,
 * as does the result array.
 */
char **getVolumeList(void)
{
    char **res = NULL;
    int allocated = 1;
    int size = 0;
    struct mntent *ent;

    FILE *fp = setmntent("/etc/mnttab", "r");

    if( !fp ) return NULL;

    while( (ent=getmntent(fp)) )
    {
       if(allocated <= size+1)
       {
         allocated *= 2;
         res = realloc(res, sizeof(char*)*allocated );
         if( !res ) return NULL;
       }
       res[size++] = strdup( ent->mnt_dir );
       res[size] = NULL;
    }
    endmntent(fp);
    return res;
}

此函数返回一个动态大小的空终止数组,它至少从代码中移除了硬编码限制。

崩溃的实际原因可能是您没有使用正确的方法打开文件,getmntent() 需要一些额外的存储空间才能完成工作(与 getmntent_r 不同,您负责创建存储空间)

此外,您不应释放 getmntent 返回的存储空间,因为它被记录为静态的。

上面的函数将按照以下方式使用:

int main(int argc, char **argv)
{
    int i;
    char ** list = getVolumeList();
    if(!list)
    {
       fprintf(stderr, "failed to get list\n");
       return 1;
    }

    for(int i=0; list[i]; i++ )
    {
      fprintf( stdout, "%d: %s\n", i, list[i]; );
      free( list[i] );
    }
    free( list );
}

【讨论】:

    猜你喜欢
    • 2017-12-01
    • 2015-10-21
    • 1970-01-01
    • 2014-10-08
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多