sarugaku / shellingham

Tool to Detect Surrounding Shell

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[WinError 122] The data area passed to a system call is too small.

rlshuhart opened this issue · comments

Hello,

I seem to be running into an issue with Windows 10. In the below example, I'm using ipython in the standard Windows Command Prompt. I am not sure if this is relevant, but I am using a corporate computer, where running applications in administrative is disable. I would be glad to provide additional details.

Python 3.6.5 |Anaconda custom (64-bit)| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import shellingham

In [2]: shellingham.detect_shell()
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-2-fdb1f77cdcba> in <module>()
----> 1 shellingham.detect_shell()

~\AppData\Local\Continuum\anaconda3\lib\site-packages\shellingham\__init__.py in detect_shell(pid, max_depth)
     20     except AttributeError:
     21         raise RuntimeError('get_shell not implemented for {0!r}'.format(name))
---> 22     shell = get_shell(pid, max_depth=max_depth)
     23     if shell:
     24         return shell

~\AppData\Local\Continuum\anaconda3\lib\site-packages\shellingham\nt.py in get_shell(pid, max_depth)
    115     if not pid:
    116         pid = os.getpid()
--> 117     processes = get_all_processes()
    118
    119     def check_parent(pid, lvl=0):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\shellingham\nt.py in get_all_processes()
     95         if pe.th32ParentProcessID:
     96             pids[pe.th32ProcessID]['parent_pid'] = pe.th32ParentProcessID
---> 97         pe = Process32Next(h_process, pe)
     98
     99     return pids

~\AppData\Local\Continuum\anaconda3\lib\site-packages\shellingham\nt.py in Process32Next(hSnapshot, pe)
     72         if windll.kernel32.GetLastError() == ERROR_NO_MORE_FILES:
     73             return
---> 74         raise WinError()
     75     return pe
     76

OSError: [WinError 122] The data area passed to a system call is too small.

In [3]: shellingham.__version__
Out[3]: '1.2.3'

Without looking into it too deeply, this is likely caused by a very long file path. Do you have long paths enabled? Can you think of a possible candidate executable that has a very long path in your system? That would help explain the phenomenom.

(This is more a note to self) I think it would likely be sufficient if we just ignore that entry and keep looking. It is highly unlikely the offending process is the shell we are looking for anyway—and we can always deal with it if someone ever needs to detect that process.

If you want to try whether my guess is correct right now, modify nt.py and change Process32Next like this:

def Process32Next(hSnapshot, pe=None):
    if pe is None:
        pe = PROCESSENTRY32()
    pe.dwSize = sizeof(PROCESSENTRY32)
    success = windll.kernel32.Process32Next(hSnapshot, byref(pe))
    if not success:
        if windll.kernel32.GetLastError() == ERROR_NO_MORE_FILES:
            return
        if windll.kernel32.GetLastError() == 122:  # ERROR_INSUFFICIENT_BUFFER.
            return pe   # If this does not work, try "return".
        raise WinError()
    return pe

and see if that works.

Seems likely to work ok at a glance, and a good safety measure to build in anyway

Thanks for taking a look! Unfortunately, I might not have a moment to give it a try until next week.

No hurry, this is not pressing at the moment :)

I edited Process32Next as instructed and success!!!

Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import shellingham
>>> shellingham.detect_shell()
('cmd', 'cmd.exe')

In addition, this was related to attempting to using pipenv shell and having the WinError. Making this same correction in the shellingham used in Pipenv resolved the problem. I suppose Pipenv should be advised to include this correction as well if it passes. Glad to help any anyway, but don't want to overstep.

Before change to .\pipenv\vendor\shellingham\nt.py Process32Next:

Traceback (most recent call last):
  File "c:\users\rshuhart001\appdata\local\continuum\anaconda3\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\rshuhart001\appdata\local\continuum\anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\rshuhart001\AppData\Local\Continuum\anaconda3\Scripts\pipenv.exe\__main__.py", line 9, in <module>
  File "c:\users\rshuhart001\appdata\local\continuum\anaconda3\lib\site-packages\pipenv\vendor\click\core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "c:\users\rshuhart001\appdata\local\continuum\anaconda3\lib\site-packages\pipenv\vendor\click\core.py", line 697, in main
    rv = self.invoke(ctx)
  File "c:\users\rshuhart001\appdata\local\continuum\anaconda3\lib\site-packages\pipenv\vendor\click\core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "c:\users\rshuhart001\appdata\local\continuum\anaconda3\lib\site-packages\pipenv\vendor\click\core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "c:\users\rshuhart001\appdata\local\continuum\anaconda3\lib\site-packages\pipenv\vendor\click\core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "c:\users\rshuhart001\appdata\local\continuum\anaconda3\lib\site-packages\pipenv\cli.py", line 664, in shell
    three=three, python=python, fancy=fancy, shell_args=shell_args, pypi_mirror=pypi_mirror
  File "c:\users\rshuhart001\appdata\local\continuum\anaconda3\lib\site-packages\pipenv\core.py", line 2149, in do_shell
    shell = choose_shell()
  File "c:\users\rshuhart001\appdata\local\continuum\anaconda3\lib\site-packages\pipenv\shells.py", line 217, in choose_shell
    type_, command = detect_info()
  File "c:\users\rshuhart001\appdata\local\continuum\anaconda3\lib\site-packages\pipenv\shells.py", line 25, in detect_info
    return shellingham.detect_shell()
  File "c:\users\rshuhart001\appdata\local\continuum\anaconda3\lib\site-packages\pipenv\vendor\shellingham\__init__.py", line 24, in detect_shell
    shell = get_shell(pid, max_depth=max_depth)
  File "c:\users\rshuhart001\appdata\local\continuum\anaconda3\lib\site-packages\pipenv\vendor\shellingham\nt.py", line 117, in get_shell
    processes = get_all_processes()
  File "c:\users\rshuhart001\appdata\local\continuum\anaconda3\lib\site-packages\pipenv\vendor\shellingham\nt.py", line 97, in get_all_processes
    pe = Process32Next(h_process, pe)
  File "c:\users\rshuhart001\appdata\local\continuum\anaconda3\lib\site-packages\pipenv\vendor\shellingham\nt.py", line 74, in Process32Next
    raise WinError()
OSError: [WinError 122] The data area passed to a system call is too small.

After change to .\pipenv\vendor\shellingham\nt.py Process32Next:

Launching subshell in virtual environment…
Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.

(shellingham_test-5dV6dxIr) C:\Users\rshuhart001\Documents\...\Other\shellingham_test>

Ryan

We maintain pipenv also (this code was written for pipenv and most of the tooling in this organization is for pipenv) so this will land in the next release