1

As part of my C application running on Linux, I call a script using the system( ) library function. This script does a umount of a connected USB flash drive. Very rarely (1 in 7-8k) iterations, I see a segmentation fault,

/bin/sh: line 1: 18083 Segmentation fault (core dumped)

I have verified with debug prints that the failure appears due to the system(). This appears to be a shell crash. I need to understand why a segmentation fault occurred.

My script contains the following code:

mount | grep "USB"
ret=$?
if [ $ret == "0" ]; then
    umount -l /mnt/USB
    ret=$?;
    if [ $ret -eq 0 ]; then
        rmdir /mnt/USB
    fi
fi

gdb isn't available on that platform and considering this issue occurs very rarely, it is tough to debug it.

  • 2
    I'd suspect a dodgy USB driver. Can you put lots of logging into the shell script to see exactly which command is failing? (or maybe just run it -x) – Philip Kendall Feb 11 at 7:02
  • 1
    Your script can be shortened to mount | grep -qF 'USB' && umount -l /mnt/USB && rmdir /mnt/USB, but I suspect you also want to grep for /mnt/USB instead of just USB. – Kusalananda Feb 11 at 7:36
  • 3
    Have you fed the core dump into the debugger? You can see where it went wrong. You can do it on a different machine. Are you checking return codes? – ctrl-alt-delor Feb 11 at 8:42
  • 1
    if the segfault comes from sh, it's not an issue with your program. But that's a simple enough script, it would be unlikely to trigger any issues in any common shell, either. Could it be a hardware problem? – ilkkachu Feb 11 at 8:52
  • 1
    Why don't you do this from C? – Kusalananda Feb 11 at 12:28
1

strace could help. AFAIK it's available in the default yum/apt/whatever repos.

strace is a diagnostic, debugging and instructional userspace utility for Linux. It is used to monitor and tamper with interactions between processes and the Linux kernel, which include system calls, signal deliveries, and changes of process state.

System administrators, diagnosticians and trouble-shooters will find it invaluable for solving problems with programs for which the source is not readily available since they do not need to be recompiled in order to trace them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.