pastebin - collaborative debugging tool
nrubsig.kpaste.net RSS


winlocktest1
Posted by Anonymous on Fri 17th Nov 2023 14:39
raw | new post

  1. #!/usr/bin/ksh93
  2.  
  3. #
  4. # winlocktest1 - test whether Win32 locks work across NFSv4 mounts
  5. #
  6. # Written by Roland Mainz <roland.mainz@nrubsig.org>
  7. #
  8.  
  9. function pw2u
  10. {
  11.         printf '%s\n' "$(cygpath -w "$1")"
  12. }
  13.  
  14. function cygdrive_nfs_mount_dir
  15. {
  16.         typeset hostname=$2
  17.         typeset mountpath=$3
  18.         nameref mntpoint=$1
  19.         integer retval=0
  20.  
  21.         #nfs_mount -p -o sec=sys T "derfwnb4966_ipv6:/net_tmpfs2"
  22.  
  23.         stdout="${ nfs_mount -o sec=sys '*' "${hostname}:${mountpath}" ; (( retval=$? )) ;}"
  24.         #cat <<<"$stdout"
  25.  
  26.         if (( retval == 0 )) ; then
  27.                 # Parse stdout for drive letter
  28.                 dummy="${stdout/~(E)Successfully mounted (.+) to drive (?:\'|)(.+):(?:\'|)/dummy}"
  29.  
  30.                 # fixme: we should test whether c.windows_drive_letter is empty or not
  31.                 windows_drive_letter="${.sh.match[2]}"
  32.  
  33.                 mntpoint="/cygdrive/$windows_drive_letter/"
  34.                 return 0
  35.         fi
  36.  
  37.         return 1
  38. }
  39.  
  40. function cygdrive_nfs_umount_dir
  41. {
  42.         typeset mntpath="$1"
  43.  
  44.         net use "${mntpath/~(Elr)\/cygdrive\/(.)(\/.*|)/\1}:" /delete || true
  45. }
  46.  
  47. function compile_test_programm
  48. {
  49.         rm -f 'tmp_winlocktest1.c'
  50.         cat >'tmp_winlocktest1.c' <<EOF
  51. /*
  52. * compile with
  53. * $ gcc -g -Wall winlocktest1.c -o winlocktest1
  54. */
  55.  
  56. #define UNICODE 1
  57.  
  58. #include <windows.h>
  59. #include <stdio.h>
  60.  
  61. int main(int ac, char *av[])
  62. {
  63.         OVERLAPPED ovl = { 0 };
  64.         const char *taskname;
  65.         const char *filename;
  66.         const char *text_to_write;
  67.         int sleep_before_lock;
  68.         int sleep_while_lock_held;
  69.  
  70.         if (ac != 6) {
  71.                 (void)fprintf(stderr, "%s: Usage: "
  72.                         "%s "
  73.                         "<taskname> "
  74.                         "<filename> "
  75.                         "<text-to-write> "
  76.                         "<sec-to-sleep-before-lock> "
  77.                         "<sec-to-sleep-while-lock-is-held>\n",
  78.                         av[0], av[0]);
  79.                 return 1;
  80.         }
  81.  
  82.         taskname = av[1];
  83.         filename = av[2];
  84.         text_to_write = av[3];
  85.         sleep_before_lock = atoi(av[4]);
  86.         sleep_while_lock_held = atoi(av[5]);
  87.  
  88.         (void)printf("# %s: start\n", taskname);
  89.  
  90.         // Open the file for read and write access.
  91.         HANDLE hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  92.         if (hFile == INVALID_HANDLE_VALUE) {
  93.                 perror("Error opening file.\n");
  94.                 return 1;
  95.         }
  96.  
  97.         (void)printf("# %s: sleeping\n", taskname);
  98.         Sleep(sleep_before_lock*1000);
  99.  
  100.         (void)printf("# %s: before lock\n", taskname);
  101.         // Lock the file from offset 10 to offset 20.
  102.         if (LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK, 0, 10, 20, &ovl) == 0) {
  103.                 fprintf(stderr, "Error locking file.\n");
  104.                 CloseHandle(hFile);
  105.                 return 1;
  106.         }
  107.         (void)printf("# %s: file locked\n", taskname);
  108.  
  109.         Sleep(sleep_while_lock_held*1000);
  110.  
  111.         // Perform some operations on the locked region of the file.
  112.         DWORD bytesWritten;
  113.         if (WriteFile(hFile, text_to_write, strlen(text_to_write), &bytesWritten, NULL) == 0) {
  114.                 fprintf(stderr, "Error writing to file.\n");
  115.                 UnlockFileEx(hFile, 0, 10, 20, &ovl);
  116.                 CloseHandle(hFile);
  117.                 return 1;
  118.         }
  119.  
  120.         // Unlock the file.
  121.         if (UnlockFileEx(hFile, 0, 10, 20, &ovl) == 0) {
  122.                 fprintf(stderr, "Error unlocking file.\n");
  123.                 CloseHandle(hFile);
  124.                 return 1;
  125.         }
  126.         (void)printf("# %s: file unlocked\n", taskname);
  127.  
  128.         // Close the file.
  129.         CloseHandle(hFile);
  130.         (void)printf("# %s: done\n", taskname);
  131.  
  132.         return 0;
  133. }
  134. EOF
  135.         rm -f winlocktest1.exe
  136.         gcc -g -Wall tmp_winlocktest1.c -o winlocktest1.exe
  137. }
  138.  
  139. #
  140. # main
  141. #
  142. set -o xtrace
  143. set -o errexit
  144. set -o nounset
  145.  
  146. PATH+=':/home/roland_mainz/work/msnfs41_uidmapping/ms-nfs41-client/destdir/cygdrive/c/cygwin64/sbin/'
  147.  
  148.  
  149. # td==testdata
  150. compound td
  151.  
  152. td.hostname="derfwnb4966_ipv6"
  153. td.mntpoint="/net_tmpfs2"
  154.  
  155. compile_test_programm || exit 1
  156.  
  157. cygdrive_nfs_mount_dir td.basedir "${td.hostname}" "${td.mntpoint}" || exit 1
  158.  
  159. mkdir -p "${td.basedir}/lockdir2/test1"
  160.  
  161. cygdrive_nfs_mount_dir td.dir1 "${td.hostname}" "${td.mntpoint}/lockdir2" || exit 1
  162. td.dir1+="/test1"
  163. cygdrive_nfs_mount_dir td.dir2 "${td.hostname}" "${td.mntpoint}/lockdir2/test1" || exit 1
  164.  
  165. print -v td
  166.  
  167. printf 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n' >"${td.dir1}/example.txt"
  168.  
  169.  
  170. ./winlocktest1.exe "T1" "$(pw2u "${td.dir1}/example.txt")" ".......... test 12345" 1 12 &
  171. ./winlocktest1.exe "T2" "$(pw2u "${td.dir2}/example.txt")" ".......... test ABCDE" 4 1 &
  172. sleep 2
  173.  
  174. # this should fail, because T1 has locked "${td.dir1}/example.txt"
  175. if [[ "$( { cat "${td.dir1}/example.txt" || true ; } 2>&1)" == *busy* ]] ; then
  176.         print OK
  177. fi
  178. # this should fail, because T1 has locked "${td.dir1}/example.txt"
  179. if [[ "$( { cat "${td.dir2}/example.txt" || true ; } 2>&1)" == *busy* ]] ; then
  180.         print OK
  181. fi
  182.  
  183. wait
  184. cat -n "${td.dir1}/example.txt"
  185. cat -n "${td.dir2}/example.txt"
  186.  
  187. #
  188. # cleanup - must be successful, no leftover files
  189. #
  190. rm "${td.basedir}/lockdir2/test1/example.txt"
  191. rmdir "${td.basedir}/lockdir2/test1"
  192. rmdir "${td.basedir}/lockdir2"
  193.  
  194. #
  195. # unmount test dirs
  196. #
  197. cygdrive_nfs_umount_dir "${td.dir2}" || true
  198. cygdrive_nfs_umount_dir "${td.dir1}" || true
  199. cygdrive_nfs_umount_dir "${td.basedir}" || true
  200.  
  201. print "#### Test OK ####"
  202.  
  203. # EOF.

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at