错误:无效参数ORBIT_SOCKETDIR = / tmp / orbit-root

时间:2014-03-25 05:23:18

标签: c linux sockets

我试图通过传递一些命令行参数与硬件进行通信。我有错误检查,如果我传递错误的参数。但每当我通过一些无法识别的论证时,我都会收到错误ORBIT_SOCKETDIR=/tmp/orbit-root。我打开了这个位置,它有一些套接字文件。此外,我的代码中的错误检查工作正常,我遇到错误的参数时退出。 我尝试在Google上搜索,但找不到令人满意的解释。 任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

今天发生在我身上。

传递给程序的参数存储在称为进程环境块的内容中。 当没有任何参数实际传递给main并且它试图在argv中访问它时它只读取块中的下一个元素(我在这里找到了更多信息:https://stackoverflow.com/a/26417129/3922692

然后我写了这个:

 ⚡⚡⚡  more test.c 
#include <stdio.h>

int main(int argc, char **argv)
{
    int i;
    for(i = 0; i < 100; i++)
    {
        printf("argv[%d] contains %s \n", i, argv[i]);
    }
    return 0;
}

当我没有参数运行它时:

 ⚡⚡⚡  ./a.out 
argv[0] contains ./a.out 
argv[1] contains (null) 
argv[2] contains ORBIT_SOCKETDIR=/tmp/orbit-ehwas 
argv[3] contains SSH_AGENT_PID=2443 
argv[4] contains TERM=xterm-256color 
argv[5] contains SHELL=/bin/bash 
argv[6] contains XDG_SESSION_COOKIE=3f606f46028152e69e3d858900000009-1422008859.843197-1908595864 
argv[7] contains WINDOWID=39845891 
argv[8] contains GNOME_KEYRING_CONTROL=/tmp/keyring-nNCd02 
argv[9] contains GTK_MODULES=canberra-gtk-module 
argv[10] contains USER=ehwas 
argv[11] contains LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36: 
argv[12] contains SSH_AUTH_SOCK=/tmp/ssh-ELberD2392/agent.2392 
argv[13] contains USERNAME=ehwas 
argv[14] contains DESKTOP_SESSION=openbox 
argv[15] contains PATH=/home/ehwas/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games 
argv[16] contains GDM_XSERVER_LOCATION=local 
argv[17] contains PWD=/home/ehwas 
argv[18] contains LANG=en_US.UTF-8 
argv[19] contains GNOME_KEYRING_PID=2382 
argv[20] contains GDM_LANG=en_US.UTF-8 
argv[21] contains GDMSESSION=openbox 
argv[22] contains SHLVL=1 
argv[23] contains HOME=/home/ehwas 
argv[24] contains LOGNAME=ehwas 
argv[25] contains DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-sP7BGFlECg,guid=fd8e2dfe17f3d2888c471c0e00000026 
argv[26] contains XDG_DATA_DIRS=/usr/local/share/:/usr/share/:/usr/share/gdm/ 
argv[27] contains WINDOWPATH=7 
argv[28] contains DISPLAY=:0.0 
argv[29] contains COLORTERM=gnome-terminal 
argv[30] contains XAUTHORITY=/home/ehwas/.Xauthority 
argv[31] contains _=./a.out 
argv[32] contains (null) 
Segmentation fault

并附带论据:

 ⚡⚡⚡  ./a.out test test test test
argv[0] contains ./a.out 
argv[1] contains test 
argv[2] contains test 
argv[3] contains test 
argv[4] contains test 
argv[5] contains (null) 
argv[6] contains ORBIT_SOCKETDIR=/tmp/orbit-ehwas 
argv[7] contains SSH_AGENT_PID=2443 
argv[8] contains TERM=xterm-256color 
argv[9] contains SHELL=/bin/bash 
argv[10] contains XDG_SESSION_COOKIE=3f606f46028152e69e3d858900000009-1422008859.843197-1908595864 
argv[11] contains WINDOWID=39845891 
argv[12] contains GNOME_KEYRING_CONTROL=/tmp/keyring-nNCd02 
argv[13] contains GTK_MODULES=canberra-gtk-module 
argv[14] contains USER=ehwas 
argv[15] contains LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36: 
argv[16] contains SSH_AUTH_SOCK=/tmp/ssh-ELberD2392/agent.2392 
argv[17] contains USERNAME=ehwas 
argv[18] contains DESKTOP_SESSION=openbox 
argv[19] contains PATH=/home/ehwas/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games 
argv[20] contains GDM_XSERVER_LOCATION=local 
argv[21] contains PWD=/home/ehwas 
argv[22] contains LANG=en_US.UTF-8 
argv[23] contains GNOME_KEYRING_PID=2382 
argv[24] contains GDM_LANG=en_US.UTF-8 
argv[25] contains GDMSESSION=openbox 
argv[26] contains SHLVL=1 
argv[27] contains HOME=/home/ehwas 
argv[28] contains LOGNAME=ehwas 
argv[29] contains DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-sP7BGFlECg,guid=fd8e2dfe17f3d2888c471c0e00000026 
argv[30] contains XDG_DATA_DIRS=/usr/local/share/:/usr/share/:/usr/share/gdm/ 
argv[31] contains WINDOWPATH=7 
argv[32] contains DISPLAY=:0.0 
argv[33] contains COLORTERM=gnome-terminal 
argv[34] contains XAUTHORITY=/home/ehwas/.Xauthority 
argv[35] contains _=./a.out 
argv[36] contains (null) 
Segmentation fault

TLDR: 在使用argv之前,应检查argc中的内容或至少是否 argv [1]为null(表示没有传递参数)